1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
<html>
<head>
<script src="OLLoader.js"></script>
<script type="text/javascript">
function test_String_startsWith(t) {
t.plan(3);
var str = "chickenHead";
var test1 = "chicken";
var test2 = "beet";
t.ok(OpenLayers.String.startsWith(str, "chicken"),
"'chickenHead' starts with 'chicken'");
t.ok(!OpenLayers.String.startsWith(str, "Head"),
"'chickenHead' does not start with 'Head'");
t.ok(!OpenLayers.String.startsWith(str, "beet"),
"'chickenHead' doesnt start with 'beet'");
}
function test_String_contains(t) {
t.plan(4);
var str = "chickenHead";
t.ok(OpenLayers.String.contains(str, "chicken"),
"(beginning) 'chickenHead' contains with 'chicken'");
t.ok(OpenLayers.String.contains(str, "ick"),
"(middle) 'chickenHead' contains with 'ick'");
t.ok(OpenLayers.String.contains(str, "Head"),
"(end) 'chickenHead' contains with 'Head'");
t.ok(!OpenLayers.String.startsWith(str, "beet"),
"'chickenHead' doesnt start with 'beet'");
}
function test_String_trim(t) {
t.plan(6);
var str = "chickenHead";
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with no extra whitespace is left alone");
str = " chickenHead";
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with extra whitespace at beginning is trimmed correctly");
str = "chickenHead ";
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with extra whitespace at end is trimmed correctly");
str = " chickenHead ";
t.eq(OpenLayers.String.trim(str),
"chickenHead", "string with extra whitespace at beginning and end is trimmed correctly");
str = "chicken\nHead ";
t.eq(OpenLayers.String.trim(str),
"chicken\nHead", "multi-line string with extra whitespace at end is trimmed correctly");
str = " ";
t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
}
function test_String_camelize(t) {
t.plan(7);
var str = "chickenhead";
t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
str = "chicken-head";
t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
str = "chicken-head-man";
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
str = "-chickenhead";
t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
str = "-chicken-head-man";
t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
str = "chicken-";
t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
str = "chicken-head-man-";
t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
}
function test_String_format(t) {
var unchanged = [
"", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
"}", "${${} }"
]
t.plan(7 + unchanged.length);
var format = OpenLayers.String.format;
var expected;
for(var i=0; i<unchanged.length; ++i) {
expected = unchanged[i];
t.eq(format(expected), expected,
"'" + expected + "' left unchanged");
}
t.eq(format("${foo} none"),
"undefined none", "undefined properties don't bomb");
window.foo = "bar";
t.eq(format("${foo} none"),
"bar none", "window context used if none passed");
var context = {bar: "foo"};
t.eq(format("${bar} foo", context), "foo foo",
"properties accessed from context");
var context = {bar: "foo", foo: "bar"};
t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar",
"multiple properties replaced correctly");
// test context with properties that are functions
var context = {
bar: "church",
getDrunk: function() {
return arguments[0];
}
};
t.eq(
format("I go to the ${bar} to ${getDrunk}.", context, ["eat pretzels"]),
"I go to the church to eat pretzels.",
"function correctly called in context with arguments"
);
// test that things don't break
var context = {
meaning: function(truth) {
return truth;
}
};
t.eq(
format("In life, truth is ${meaning}.", context),
"In life, truth is undefined.",
"still works if arguments are not supplied"
);
// test contexts where attribute values can be objects
var context = {
a: {
b: {
c: 'd',
e: function() {
return 'f';
}
}
}
};
t.eq(
format("${a.b.c} ${a.b.e} ${a.b.q} ${a} ${a...b...c} ${aa.b} ${a.bb.c}", context),
"d f undefined [object Object] d undefined undefined",
"attribute values that are objects are supported"
);
}
function test_String_isNumeric(t) {
var cases = [
{value: "3", expect: true},
{value: "+3", expect: true},
{value: "-3", expect: true},
{value: "3.0", expect: true},
{value: "+3.0", expect: true},
{value: "-3.0", expect: true},
{value: "6.02e23", expect: true},
{value: "+1.0e-100", expect: true},
{value: "-1.0e+100", expect: true},
{value: "1E100", expect: true},
{value: null, expect: false},
{value: true, expect: false},
{value: false, expect: false},
{value: undefined, expect: false},
{value: "", expect: false},
{value: "3 ", expect: false},
{value: " 3", expect: false},
{value: "1e", expect: false},
{value: "1+e", expect: false},
{value: "1-e", expect: false}
];
t.plan(cases.length);
var func = OpenLayers.String.isNumeric;
var obj, val, got, exp;
for(var i=0; i<cases.length; ++i) {
obj = cases[i];
val = obj.value;
exp = obj.expect;
got = func(val);
t.eq(got, exp, "'" + val + "' returns " + exp);
}
}
function test_Number_numericIf(t) {
var cases = [
{value: "3", expect: 3, expectWithTrim: 3},
{value: "+3", expect: 3, expectWithTrim: 3},
{value: "-3", expect: -3, expectWithTrim: -3},
{value: "3.0", expect: 3, expectWithTrim: 3},
{value: "+3.0", expect: 3, expectWithTrim: 3},
{value: "-3.0", expect: -3, expectWithTrim: -3},
{value: "6.02e23", expect: 6.02e23, expectWithTrim: 6.02e23},
{value: "+1.0e-100", expect: 1e-100, expectWithTrim: 1e-100},
{value: "-1.0e+100", expect: -1e100, expectWithTrim: -1e100},
{value: "1E100", expect: 1e100, expectWithTrim: 1e100},
{value: null, expect: null, expectWithTrim: null},
{value: true, expect: true, expectWithTrim: true},
{value: false, expect: false, expectWithTrim: false},
{value: undefined, expect: undefined, expectWithTrim: undefined},
{value: "", expect: "", expectWithTrim: ""},
{value: "3 ", expect: "3 ", expectWithTrim: 3},
{value: " 3", expect: " 3", expectWithTrim: 3},
{value: "1e", expect: "1e", expectWithTrim: "1e"},
{value: "1+e", expect: "1+e", expectWithTrim: "1+e"},
{value: "1-e", expect: "1-e", expectWithTrim: "1-e"},
{value: " 27 ", expect: " 27 ", expectWithTrim: 27},
{value: " abc ", expect: " abc ", expectWithTrim: " abc "}
];
t.plan(cases.length*2);
var func = OpenLayers.String.numericIf;
var obj, val, got, exp;
for(var i=0; i<cases.length; ++i) {
obj = cases[i];
val = obj.value;
exp = obj.expect;
got = func(val);
t.eq(got, exp, "'" + val + "' returns " + exp);
got = func(val, true);
exp = obj.expectWithTrim;
t.eq(got, exp, "'" + val + "' returns " + exp + " with trimWhitespace true");
}
}
function test_Number_limitSigDigs(t) {
t.plan(9);
var num = 123456789;
t.eq(OpenLayers.Number.limitSigDigs(num), 0, "passing 'null' as sig returns 0");
t.eq(OpenLayers.Number.limitSigDigs(num, -1), 0, "passing -1 as sig returns 0");
t.eq(OpenLayers.Number.limitSigDigs(num, 0), 0, "passing 0 as sig returns 0");
t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
t.eq(OpenLayers.Number.limitSigDigs(num, 1), 100000000, "passing sig 1 works");
t.eq(OpenLayers.Number.limitSigDigs(num, 3), 123000000, "passing middle sig works (rounds down)");
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 123460000, "passing middle sig works (rounds up)");
t.eq(OpenLayers.Number.limitSigDigs(num, 9), 123456789, "passing sig equal to num digits in number works");
num = 1234.56789;
t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
}
function test_Number_format(t) {
t.plan(9);
var format = OpenLayers.Number.format;
t.eq(format(12345), "12,345", "formatting an integer number works");
t.eq(format(12345, 3), "12,345.000", "zero padding an integer works");
t.eq(format(12345, null, ","), "12,345", "adding thousands separator to an integer works");
t.eq(format(12345, 0, ","), "12,345", "adding thousands separator to an integer with defined 0 decimal places works");
var num = 12345.6789
t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works");
t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works");
t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works");
t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works");
OpenLayers.Number.thousandsSeparator = ".";
OpenLayers.Number.decimalSeparator = ",";
t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works");
}
function test_Number_zeroPad(t) {
t.plan(6);
var pad = OpenLayers.Number.zeroPad;
t.eq(pad(15, 4), "0015", "left padding works");
t.eq(pad(15, 2), "15", "no left padding when equal to number of digits");
t.eq(pad(15, 1), "15", "no left padding when less than number of digits");
t.eq(pad(10, 5, 2), "01010", "radix modified and padding works");
t.eq(pad(10, 5, 8), "00012", "radix modified and padding works");
t.eq(pad(10, 5, 36), "0000a", "radix modified and padding works");
}
function test_Function_bind(t) {
t.plan(12);
g_obj = {};
g_Arg1 = {};
g_Arg2 = {};
g_Arg3 = {};
g_Arg4 = {};
var foo = function(x,y,z,a) {
t.ok(this == g_obj, "context correctly set");
t.ok(x == g_Arg1, "arg1 passed correctly");
t.ok(y == g_Arg2, "arg2 passed correctly");
t.ok(z == g_Arg3, "arg3 passed correctly");
t.ok(a == g_Arg4, "arg4 passed correctly");
t.eq(arguments.length, 4, "correct number of arguments ((regression test for #876))");
};
var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
newFoo(g_Arg3, g_Arg4);
//run again to make sure the arguments are handled correctly
newFoo(g_Arg3, g_Arg4);
}
function test_Function_Void(t) {
t.plan(1);
t.eq(OpenLayers.Function.Void(), undefined, "returns undefined");
}
function test_Function_bindAsEventListener(t) {
t.plan(4);
g_obj = {};
g_Event = {};
g_WindowEvent = {};
var foo = function(x) {
t.ok(this == g_obj, "context correctly set");
g_X = x;
};
var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
g_X = null;
newFoo(g_Event);
t.ok(g_X == g_Event, "event properly passed as first argument when event specified");
g_X = null;
newFoo();
t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
}
function test_Array_filter(t) {
t.plan(8);
OpenLayers.Array.filter(["foo"], function(item, index, array) {
t.eq(item, "foo", "callback called with proper item");
t.eq(index, 0, "callback called with proper index");
t.eq(array, ["foo"], "callback called with proper array");
t.eq(this, {"foo": "bar"}, "callback called with this set properly");
}, {"foo": "bar"});
var array = [0, 1, 2, 3];
var select = OpenLayers.Array.filter(array, function(value) {
return value > 1;
});
t.eq(select, [2, 3], "filter works for basic callback");
t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
var obj = {
test: function(value) {
if(value > 1) {
return true;
}
}
};
var select = OpenLayers.Array.filter(array, function(value) {
return this.test(value);
}, obj);
t.eq(select, [2, 3], "filter works for callback and caller");
t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
}
</script>
</head>
<body>
</body>
</html>
|