]> dev.renevier.net Git - syp.git/blob - openlayers/tests/BaseTypes.html
fixes notices
[syp.git] / openlayers / tests / BaseTypes.html
1 <html>
2 <head>
3   <script src="../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5
6     function test_String_startsWith(t) {
7         t.plan(3);
8         
9         var str = "chickenHead";
10         
11         var test1 = "chicken";
12         var test2 = "beet";
13
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'");
20     }
21     
22     function test_String_contains(t) {
23         t.plan(4);
24         
25         var str = "chickenHead";
26
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'");
35     }
36     
37     function test_String_trim(t) {
38         t.plan(6);
39         
40         var str = "chickenHead";
41         t.eq(OpenLayers.String.trim(str),
42              "chickenHead", "string with no extra whitespace is left alone");
43
44         str = "  chickenHead";
45         t.eq(OpenLayers.String.trim(str),
46              "chickenHead", "string with extra whitespace at beginning is trimmed correctly");
47
48         str = "chickenHead    ";
49         t.eq(OpenLayers.String.trim(str),
50              "chickenHead", "string with extra whitespace at end is trimmed correctly");
51
52         str = "  chickenHead    ";
53         t.eq(OpenLayers.String.trim(str),
54              "chickenHead", "string with extra whitespace at beginning and end is trimmed correctly");
55
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");
59         str = " ";
60         t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
61     }
62         
63     function test_String_camelize(t) {
64         t.plan(7);
65         
66         var str = "chickenhead";
67         t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
68
69         str = "chicken-head";
70         t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
71
72         str = "chicken-head-man";
73         t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
74
75         str = "-chickenhead";
76         t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
77
78         str = "-chicken-head-man";
79         t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
80
81         str = "chicken-";
82         t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
83
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)");
86
87
88     }
89     
90     function test_String_format(t) {
91         var unchanged = [
92             "", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
93             "}", "${${} }"
94         ]
95         t.plan(7 + unchanged.length);
96
97         var format = OpenLayers.String.format;
98         
99         var expected;
100         for(var i=0; i<unchanged.length; ++i) {
101             expected = unchanged[i];
102             t.eq(format(expected), expected,
103                  "'" + expected + "' left unchanged");
104         }
105
106         t.eq(format("${foo} none"),
107              "undefined none", "undefined properties don't bomb");
108
109         window.foo = "bar";
110         t.eq(format("${foo} none"),
111              "bar none", "window context used if none passed");
112         
113         var context = {bar: "foo"};
114         t.eq(format("${bar} foo", context), "foo foo",
115              "properties accessed from context");
116         
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");
120         
121         // test context with properties that are functions
122         var context = {
123             bar: "church",
124             getDrunk: function() {
125                 return arguments[0];
126             }
127         };
128         t.eq(
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"
132         );
133         
134         // test that things don't break
135         var context = {
136             meaning: function(truth) {
137                 return truth;
138             }
139         };
140         t.eq(
141             format("In life, truth is ${meaning}.", context),
142             "In life, truth is undefined.",
143             "still works if arguments are not supplied"
144         );
145
146         // test contexts where attribute values can be objects
147         var context = {
148             a: {
149                 b: {
150                     c: 'd',
151                     e: function() {
152                         return 'f';
153                     }
154                 }
155             }
156         };
157         t.eq(
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"
161         );
162
163     }
164
165     function test_String_isNumeric(t) {
166         var cases = [
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}
187         ];
188         t.plan(cases.length);
189         
190         var func = OpenLayers.String.isNumeric;
191         var obj, val, got, exp;
192         for(var i=0; i<cases.length; ++i) {
193             obj = cases[i];
194             val = obj.value;
195             exp = obj.expect;
196             got = func(val);
197             t.eq(got, exp, "'" + val + "' returns " + exp);
198         }
199         
200     }
201     
202     function test_Number_numericIf(t) {
203         var cases = [
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"}
224         ];
225         t.plan(cases.length);
226         
227         var func = OpenLayers.String.numericIf;
228         var obj, val, got, exp;
229         for(var i=0; i<cases.length; ++i) {
230             obj = cases[i];
231             val = obj.value;
232             exp = obj.expect;
233             got = func(val);
234             t.eq(got, exp, "'" + val + "' returns " + exp);
235         }
236     }
237    
238    
239     function test_Number_limitSigDigs(t) {
240         t.plan(9);
241       
242         var num = 123456789; 
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");
246         
247         t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
248         
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");
253
254         num = 1234.56789;
255         t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
256         
257     }
258     
259     function test_Number_format(t) {
260         t.plan(9);
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");
266
267         var num = 12345.6789
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");
275     }
276
277     function test_Function_bind(t) {
278         t.plan(12);
279
280         g_obj = {};
281         g_Arg1 = {};
282         g_Arg2 = {};
283         g_Arg3 = {};
284         g_Arg4 = {};
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))");
292         };
293
294         var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
295
296         newFoo(g_Arg3, g_Arg4);
297         
298         //run again to make sure the arguments are handled correctly
299         newFoo(g_Arg3, g_Arg4);
300     }
301
302     function test_Function_bindAsEventListener(t) {
303         t.plan(4);
304
305         g_obj = {};
306         g_Event = {};
307         g_WindowEvent = {};
308
309         var foo = function(x) {
310             t.ok(this == g_obj, "context correctly set");
311             g_X = x;
312         };
313
314         var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
315         
316
317         g_X = null;
318         newFoo(g_Event);
319         t.ok(g_X == g_Event, "event properly passed as first argument when event specified");
320     
321         g_X = null;
322         newFoo();
323         t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
324     }
325
326     function test_Array_filter(t) {
327         
328         t.plan(8);
329
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");
335         }, {"foo": "bar"});
336
337         var array = [0, 1, 2, 3];
338         var select = OpenLayers.Array.filter(array, function(value) {
339             return value > 1;
340         });
341         t.eq(select, [2, 3], "filter works for basic callback");
342         t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
343         
344         var obj = {
345             test: function(value) {
346                 if(value > 1) {
347                     return true;
348                 }
349             }
350         };
351         var select = OpenLayers.Array.filter(array, function(value) {
352             return this.test(value);
353         }, obj);
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");
356         
357         
358     }
359
360         
361
362   </script>
363 </head>
364 <body>
365 </body>
366 </html>