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");
56 str = "chicken\nHead ";
57 t.eq(OpenLayers.String.trim(str),
58 "chicken\nHead", "multi-line string with extra whitespace at end is trimmed correctly");
60 t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
63 function test_String_camelize(t) {
66 var str = "chickenhead";
67 t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
70 t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
72 str = "chicken-head-man";
73 t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
76 t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
78 str = "-chicken-head-man";
79 t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
82 t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
84 str = "chicken-head-man-";
85 t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
90 function test_String_format(t) {
92 "", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
95 t.plan(7 + unchanged.length);
97 var format = OpenLayers.String.format;
100 for(var i=0; i<unchanged.length; ++i) {
101 expected = unchanged[i];
102 t.eq(format(expected), expected,
103 "'" + expected + "' left unchanged");
106 t.eq(format("${foo} none"),
107 "undefined none", "undefined properties don't bomb");
110 t.eq(format("${foo} none"),
111 "bar none", "window context used if none passed");
113 var context = {bar: "foo"};
114 t.eq(format("${bar} foo", context), "foo foo",
115 "properties accessed from context");
117 var context = {bar: "foo", foo: "bar"};
118 t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar",
119 "multiple properties replaced correctly");
121 // test context with properties that are functions
124 getDrunk: function() {
129 format("I go to the ${bar} to ${getDrunk}.", context, ["eat pretzels"]),
130 "I go to the church to eat pretzels.",
131 "function correctly called in context with arguments"
134 // test that things don't break
136 meaning: function(truth) {
141 format("In life, truth is ${meaning}.", context),
142 "In life, truth is undefined.",
143 "still works if arguments are not supplied"
146 // test contexts where attribute values can be objects
158 format("${a.b.c} ${a.b.e} ${a.b.q} ${a} ${a...b...c}", context),
159 "d f undefined [object Object] d",
160 "attribute values that are objects are supported"
165 function test_String_isNumeric(t) {
167 {value: "3", expect: true},
168 {value: "+3", expect: true},
169 {value: "-3", expect: true},
170 {value: "3.0", expect: true},
171 {value: "+3.0", expect: true},
172 {value: "-3.0", expect: true},
173 {value: "6.02e23", expect: true},
174 {value: "+1.0e-100", expect: true},
175 {value: "-1.0e+100", expect: true},
176 {value: "1E100", expect: true},
177 {value: null, expect: false},
178 {value: true, expect: false},
179 {value: false, expect: false},
180 {value: undefined, expect: false},
181 {value: "", expect: false},
182 {value: "3 ", expect: false},
183 {value: " 3", expect: false},
184 {value: "1e", expect: false},
185 {value: "1+e", expect: false},
186 {value: "1-e", expect: false}
188 t.plan(cases.length);
190 var func = OpenLayers.String.isNumeric;
191 var obj, val, got, exp;
192 for(var i=0; i<cases.length; ++i) {
197 t.eq(got, exp, "'" + val + "' returns " + exp);
202 function test_Number_numericIf(t) {
204 {value: "3", expect: 3},
205 {value: "+3", expect: 3},
206 {value: "-3", expect: -3},
207 {value: "3.0", expect: 3},
208 {value: "+3.0", expect: 3},
209 {value: "-3.0", expect: -3},
210 {value: "6.02e23", expect: 6.02e23},
211 {value: "+1.0e-100", expect: 1e-100},
212 {value: "-1.0e+100", expect: -1e100},
213 {value: "1E100", expect: 1e100},
214 {value: null, expect: null},
215 {value: true, expect: true},
216 {value: false, expect: false},
217 {value: undefined, expect: undefined},
218 {value: "", expect: ""},
219 {value: "3 ", expect: "3 "},
220 {value: " 3", expect: " 3"},
221 {value: "1e", expect: "1e"},
222 {value: "1+e", expect: "1+e"},
223 {value: "1-e", expect: "1-e"}
225 t.plan(cases.length);
227 var func = OpenLayers.String.numericIf;
228 var obj, val, got, exp;
229 for(var i=0; i<cases.length; ++i) {
234 t.eq(got, exp, "'" + val + "' returns " + exp);
239 function test_Number_limitSigDigs(t) {
243 t.eq(OpenLayers.Number.limitSigDigs(num), 0, "passing 'null' as sig returns 0");
244 t.eq(OpenLayers.Number.limitSigDigs(num, -1), 0, "passing -1 as sig returns 0");
245 t.eq(OpenLayers.Number.limitSigDigs(num, 0), 0, "passing 0 as sig returns 0");
247 t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
249 t.eq(OpenLayers.Number.limitSigDigs(num, 1), 100000000, "passing sig 1 works");
250 t.eq(OpenLayers.Number.limitSigDigs(num, 3), 123000000, "passing middle sig works (rounds down)");
251 t.eq(OpenLayers.Number.limitSigDigs(num, 5), 123460000, "passing middle sig works (rounds up)");
252 t.eq(OpenLayers.Number.limitSigDigs(num, 9), 123456789, "passing sig equal to num digits in number works");
255 t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
259 function test_Number_format(t) {
261 var format = OpenLayers.Number.format;
262 t.eq(format(12345), "12,345", "formatting an integer number works");
263 t.eq(format(12345, 3), "12,345.000", "zero padding an integer works");
264 t.eq(format(12345, null, ","), "12,345", "adding thousands separator to an integer works");
265 t.eq(format(12345, 0, ","), "12,345", "adding thousands separator to an integer with defined 0 decimal places works");
268 t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works");
269 t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works");
270 t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works");
271 t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works");
272 OpenLayers.Number.thousandsSeparator = ".";
273 OpenLayers.Number.decimalSeparator = ",";
274 t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works");
277 function test_Function_bind(t) {
285 var foo = function(x,y,z,a) {
286 t.ok(this == g_obj, "context correctly set");
287 t.ok(x == g_Arg1, "arg1 passed correctly");
288 t.ok(y == g_Arg2, "arg2 passed correctly");
289 t.ok(z == g_Arg3, "arg3 passed correctly");
290 t.ok(a == g_Arg4, "arg4 passed correctly");
291 t.eq(arguments.length, 4, "correct number of arguments ((regression test for #876))");
294 var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
296 newFoo(g_Arg3, g_Arg4);
298 //run again to make sure the arguments are handled correctly
299 newFoo(g_Arg3, g_Arg4);
302 function test_Function_bindAsEventListener(t) {
309 var foo = function(x) {
310 t.ok(this == g_obj, "context correctly set");
314 var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
319 t.ok(g_X == g_Event, "event properly passed as first argument when event specified");
323 t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
326 function test_Array_filter(t) {
330 OpenLayers.Array.filter(["foo"], function(item, index, array) {
331 t.eq(item, "foo", "callback called with proper item");
332 t.eq(index, 0, "callback called with proper index");
333 t.eq(array, ["foo"], "callback called with proper array");
334 t.eq(this, {"foo": "bar"}, "callback called with this set properly");
337 var array = [0, 1, 2, 3];
338 var select = OpenLayers.Array.filter(array, function(value) {
341 t.eq(select, [2, 3], "filter works for basic callback");
342 t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
345 test: function(value) {
351 var select = OpenLayers.Array.filter(array, function(value) {
352 return this.test(value);
354 t.eq(select, [2, 3], "filter works for callback and caller");
355 t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");