3
<script src="../lib/OpenLayers.js"></script>
4
<script type="text/javascript">
6
function test_String_startsWith(t) {
9
var str = "chickenHead";
11
var test1 = "chicken";
14
t.ok(OpenLayers.String.startsWith(str, "chicken"),
15
"'chickenHead' starts with 'chicken'");
16
t.ok(!OpenLayers.String.startsWith(str, "Head"),
17
"'chickenHead' does not start with 'Head'");
18
t.ok(!OpenLayers.String.startsWith(str, "beet"),
19
"'chickenHead' doesnt start with 'beet'");
22
function test_String_contains(t) {
25
var str = "chickenHead";
27
t.ok(OpenLayers.String.contains(str, "chicken"),
28
"(beginning) 'chickenHead' contains with 'chicken'");
29
t.ok(OpenLayers.String.contains(str, "ick"),
30
"(middle) 'chickenHead' contains with 'ick'");
31
t.ok(OpenLayers.String.contains(str, "Head"),
32
"(end) 'chickenHead' contains with 'Head'");
33
t.ok(!OpenLayers.String.startsWith(str, "beet"),
34
"'chickenHead' doesnt start with 'beet'");
37
function test_String_trim(t) {
40
var str = "chickenHead";
41
t.eq(OpenLayers.String.trim(str),
42
"chickenHead", "string with no extra whitespace is left alone");
45
t.eq(OpenLayers.String.trim(str),
46
"chickenHead", "string with extra whitespace at beginning is trimmed correctly");
49
t.eq(OpenLayers.String.trim(str),
50
"chickenHead", "string with extra whitespace at end is trimmed correctly");
52
str = " chickenHead ";
53
t.eq(OpenLayers.String.trim(str),
54
"chickenHead", "string with extra whitespace at beginning and end is trimmed correctly");
57
t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
60
function test_String_camelize(t) {
63
var str = "chickenhead";
64
t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
67
t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
69
str = "chicken-head-man";
70
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
73
t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
75
str = "-chicken-head-man";
76
t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
79
t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
81
str = "chicken-head-man-";
82
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
87
function test_String_format(t) {
89
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
92
t.plan(6 + unchanged.length);
94
var format = OpenLayers.String.format;
97
for(var i=0; i<unchanged.length; ++i) {
98
expected = unchanged[i];
99
t.eq(format(expected), expected,
100
"'" + expected + "' left unchanged");
103
t.eq(format("${foo} none"),
104
"undefined none", "undefined properties don't bomb");
107
t.eq(format("${foo} none"),
108
"bar none", "window context used if none passed");
110
var context = {bar: "foo"};
111
t.eq(format("${bar} foo", context), "foo foo",
112
"properties accessed from context");
114
var context = {bar: "foo", foo: "bar"};
115
t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar",
116
"multiple properties replaced correctly");
118
// test context with properties that are functions
121
getDrunk: function() {
126
format("I go to the ${bar} to ${getDrunk}.", context, ["eat pretzels"]),
127
"I go to the church to eat pretzels.",
128
"function correctly called in context with arguments"
131
// test that things don't break
133
meaning: function(truth) {
138
format("In life, truth is ${meaning}.", context),
139
"In life, truth is undefined.",
140
"still works if arguments are not supplied"
145
function test_String_isNumeric(t) {
147
{value: "3", expect: true},
148
{value: "+3", expect: true},
149
{value: "-3", expect: true},
150
{value: "3.0", expect: true},
151
{value: "+3.0", expect: true},
152
{value: "-3.0", expect: true},
153
{value: "6.02e23", expect: true},
154
{value: "+1.0e-100", expect: true},
155
{value: "-1.0e+100", expect: true},
156
{value: "1E100", expect: true},
157
{value: null, expect: false},
158
{value: true, expect: false},
159
{value: false, expect: false},
160
{value: undefined, expect: false},
161
{value: "", expect: false},
162
{value: "3 ", expect: false},
163
{value: " 3", expect: false},
164
{value: "1e", expect: false},
165
{value: "1+e", expect: false},
166
{value: "1-e", expect: false}
168
t.plan(cases.length);
170
var func = OpenLayers.String.isNumeric;
171
var obj, val, got, exp;
172
for(var i=0; i<cases.length; ++i) {
177
t.eq(got, exp, "'" + val + "' returns " + exp);
183
function test_Number_limitSigDigs(t) {
187
t.eq(OpenLayers.Number.limitSigDigs(num), 0, "passing 'null' as sig returns 0");
188
t.eq(OpenLayers.Number.limitSigDigs(num, -1), 0, "passing -1 as sig returns 0");
189
t.eq(OpenLayers.Number.limitSigDigs(num, 0), 0, "passing 0 as sig returns 0");
191
t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
193
t.eq(OpenLayers.Number.limitSigDigs(num, 1), 100000000, "passing sig 1 works");
194
t.eq(OpenLayers.Number.limitSigDigs(num, 3), 123000000, "passing middle sig works (rounds down)");
195
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 123460000, "passing middle sig works (rounds up)");
196
t.eq(OpenLayers.Number.limitSigDigs(num, 9), 123456789, "passing sig equal to num digits in number works");
199
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
203
function test_Number_format(t) {
205
var format = OpenLayers.Number.format;
206
t.eq(format(12345), "12,345", "formatting an integer number works");
207
t.eq(format(12345, 3), "12,345.000", "zero padding an integer works");
208
t.eq(format(12345, null, ","), "12,345", "adding thousands separator to an integer works");
209
t.eq(format(12345, 0, ","), "12,345", "adding thousands separator to an integer with defined 0 decimal places works");
212
t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works");
213
t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works");
214
t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works");
215
t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works");
216
OpenLayers.Number.thousandsSeparator = ".";
217
OpenLayers.Number.decimalSeparator = ",";
218
t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works");
221
function test_Function_bind(t) {
229
var foo = function(x,y,z,a) {
230
t.ok(this == g_obj, "context correctly set");
231
t.ok(x == g_Arg1, "arg1 passed correctly");
232
t.ok(y == g_Arg2, "arg2 passed correctly");
233
t.ok(z == g_Arg3, "arg3 passed correctly");
234
t.ok(a == g_Arg4, "arg4 passed correctly");
235
t.eq(arguments.length, 4, "correct number of arguments ((regression test for #876))");
238
var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
240
newFoo(g_Arg3, g_Arg4);
242
//run again to make sure the arguments are handled correctly
243
newFoo(g_Arg3, g_Arg4);
246
function test_Function_bindAsEventListener(t) {
253
var foo = function(x) {
254
t.ok(this == g_obj, "context correctly set");
258
var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
263
t.ok(g_X == g_Event, "event properly passed as first argument when event specified");
267
t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
270
function test_Array_filter(t) {
274
OpenLayers.Array.filter(["foo"], function(item, index, array) {
275
t.eq(item, "foo", "callback called with proper item");
276
t.eq(index, 0, "callback called with proper index");
277
t.eq(array, ["foo"], "callback called with proper array");
278
t.eq(this, {"foo": "bar"}, "callback called with this set properly");
281
var array = [0, 1, 2, 3];
282
var select = OpenLayers.Array.filter(array, function(value) {
285
t.eq(select, [2, 3], "filter works for basic callback");
286
t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
289
test: function(value) {
295
var select = OpenLayers.Array.filter(array, function(value) {
296
return this.test(value);
298
t.eq(select, [2, 3], "filter works for callback and caller");
299
t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");