~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/tests/BaseTypes.html

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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(5);
 
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 = " ";
 
57
        t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
 
58
    }
 
59
        
 
60
    function test_String_camelize(t) {
 
61
        t.plan(7);
 
62
        
 
63
        var str = "chickenhead";
 
64
        t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
 
65
 
 
66
        str = "chicken-head";
 
67
        t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
 
68
 
 
69
        str = "chicken-head-man";
 
70
        t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
 
71
 
 
72
        str = "-chickenhead";
 
73
        t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
 
74
 
 
75
        str = "-chicken-head-man";
 
76
        t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
 
77
 
 
78
        str = "chicken-";
 
79
        t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
 
80
 
 
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)");
 
83
 
 
84
 
 
85
    }
 
86
    
 
87
    function test_String_format(t) {
 
88
        var unchanged = [
 
89
            "", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
 
90
            "}", "${${} }"
 
91
        ]
 
92
        t.plan(6 + unchanged.length);
 
93
 
 
94
        var format = OpenLayers.String.format;
 
95
        
 
96
        var expected;
 
97
        for(var i=0; i<unchanged.length; ++i) {
 
98
            expected = unchanged[i];
 
99
            t.eq(format(expected), expected,
 
100
                 "'" + expected + "' left unchanged");
 
101
        }
 
102
 
 
103
        t.eq(format("${foo} none"),
 
104
             "undefined none", "undefined properties don't bomb");
 
105
 
 
106
        window.foo = "bar";
 
107
        t.eq(format("${foo} none"),
 
108
             "bar none", "window context used if none passed");
 
109
        
 
110
        var context = {bar: "foo"};
 
111
        t.eq(format("${bar} foo", context), "foo foo",
 
112
             "properties accessed from context");
 
113
        
 
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");
 
117
        
 
118
        // test context with properties that are functions
 
119
        var context = {
 
120
            bar: "church",
 
121
            getDrunk: function() {
 
122
                return arguments[0];
 
123
            }
 
124
        };
 
125
        t.eq(
 
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"
 
129
        );
 
130
        
 
131
        // test that things don't break
 
132
        var context = {
 
133
            meaning: function(truth) {
 
134
                return truth;
 
135
            }
 
136
        };
 
137
        t.eq(
 
138
            format("In life, truth is ${meaning}.", context),
 
139
            "In life, truth is undefined.",
 
140
            "still works if arguments are not supplied"
 
141
        );
 
142
 
 
143
    }
 
144
 
 
145
    function test_String_isNumeric(t) {
 
146
        var cases = [
 
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}
 
167
        ];
 
168
        t.plan(cases.length);
 
169
        
 
170
        var func = OpenLayers.String.isNumeric;
 
171
        var obj, val, got, exp;
 
172
        for(var i=0; i<cases.length; ++i) {
 
173
            obj = cases[i];
 
174
            val = obj.value;
 
175
            exp = obj.expect;
 
176
            got = func(val);
 
177
            t.eq(got, exp, "'" + val + "' returns " + exp);
 
178
        }
 
179
        
 
180
    }
 
181
   
 
182
   
 
183
    function test_Number_limitSigDigs(t) {
 
184
        t.plan(9);
 
185
      
 
186
        var num = 123456789; 
 
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");
 
190
        
 
191
        t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
 
192
        
 
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");
 
197
 
 
198
        num = 1234.56789;
 
199
        t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
 
200
        
 
201
    }
 
202
    
 
203
    function test_Number_format(t) {
 
204
        t.plan(9);
 
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");
 
210
 
 
211
        var num = 12345.6789
 
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");
 
219
    }
 
220
 
 
221
    function test_Function_bind(t) {
 
222
        t.plan(12);
 
223
 
 
224
        g_obj = {};
 
225
        g_Arg1 = {};
 
226
        g_Arg2 = {};
 
227
        g_Arg3 = {};
 
228
        g_Arg4 = {};
 
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))");
 
236
        };
 
237
 
 
238
        var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
 
239
 
 
240
        newFoo(g_Arg3, g_Arg4);
 
241
        
 
242
        //run again to make sure the arguments are handled correctly
 
243
        newFoo(g_Arg3, g_Arg4);
 
244
    }
 
245
 
 
246
    function test_Function_bindAsEventListener(t) {
 
247
        t.plan(4);
 
248
 
 
249
        g_obj = {};
 
250
        g_Event = {};
 
251
        g_WindowEvent = {};
 
252
 
 
253
        var foo = function(x) {
 
254
            t.ok(this == g_obj, "context correctly set");
 
255
            g_X = x;
 
256
        };
 
257
 
 
258
        var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
 
259
        
 
260
 
 
261
        g_X = null;
 
262
        newFoo(g_Event);
 
263
        t.ok(g_X == g_Event, "event properly passed as first argument when event specified");
 
264
    
 
265
        g_X = null;
 
266
        newFoo();
 
267
        t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
 
268
    }
 
269
 
 
270
    function test_Array_filter(t) {
 
271
        
 
272
        t.plan(8);
 
273
 
 
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");
 
279
        }, {"foo": "bar"});
 
280
 
 
281
        var array = [0, 1, 2, 3];
 
282
        var select = OpenLayers.Array.filter(array, function(value) {
 
283
            return value > 1;
 
284
        });
 
285
        t.eq(select, [2, 3], "filter works for basic callback");
 
286
        t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
 
287
        
 
288
        var obj = {
 
289
            test: function(value) {
 
290
                if(value > 1) {
 
291
                    return true;
 
292
                }
 
293
            }
 
294
        };
 
295
        var select = OpenLayers.Array.filter(array, function(value) {
 
296
            return this.test(value);
 
297
        }, obj);
 
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");
 
300
        
 
301
        
 
302
    }
 
303
 
 
304
        
 
305
 
 
306
  </script>
 
307
</head>
 
308
<body>
 
309
</body>
 
310
</html>