source: ether_iasi/trunk/web/resources/js/OpenLayers-2.12/tests/Filter/Comparison.html @ 739

Last change on this file since 739 was 739, checked in by vmipsl, 11 years ago

OpenLayers?

File size: 10.6 KB
Line 
1<html> 
2<head> 
3    <script src="../OLLoader.js"></script> 
4    <script type="text/javascript">
5
6    function test_initialize(t) { 
7        t.plan(3); 
8         
9        var options = {'foo': 'bar'}; 
10        var filter = new OpenLayers.Filter.Comparison(options); 
11        t.ok(filter instanceof OpenLayers.Filter.Comparison, 
12             "new OpenLayers.Filter.Comparison returns object" ); 
13        t.eq(filter.foo, "bar", "constructor sets options correctly"); 
14        t.eq(typeof filter.evaluate, "function", "filter has an evaluate function"); 
15    }
16
17    function test_destroy(t) {
18        t.plan(1);
19       
20        var filter = new OpenLayers.Filter.Comparison();
21        filter.destroy();
22        t.eq(filter.symbolizer, null, "symbolizer hash nulled properly");
23    }
24   
25    function test_value2regex(t) {
26        t.plan(4);
27       
28        var filter = new OpenLayers.Filter.Comparison({
29                property: "foo",
30                value: "*b?r\\*\\?*",
31                type: OpenLayers.Filter.Comparison.LIKE});
32        filter.value2regex("*", "?", "\\");
33        t.eq(filter.value, ".*b.r\\*\\?.*", "Regular expression generated correctly.");
34       
35        filter.value = "%b.r!%!.%";
36        filter.value2regex("%", ".", "!");
37        t.eq(filter.value, ".*b.r\\%\\..*", "Regular expression with different wildcard and escape chars generated correctly.");
38   
39        filter.value = "!!";
40        filter.value2regex();
41        t.eq(filter.value, "\\!", "!! successfully unescaped to \\!");
42       
43        // Big one.
44        filter.value = "!!c!!!d!e";
45        filter.value2regex();
46        t.eq(filter.value, "\\!c\\!\\d\\e", "!!c!!!d!e successfully unescaped to \\!c\\!\\d\\e");
47    }
48   
49    function test_regex2value(t) {
50        t.plan(8);
51       
52        function r2v(regex) {
53            return OpenLayers.Filter.Comparison.prototype.regex2value.call(
54                {value: regex}
55            );
56        }
57       
58        t.eq(r2v("foo"), "foo", "doesn't change string without special chars");
59        t.eq(r2v("foo.*foo"), "foo*foo", "wildCard replaced");
60        t.eq(r2v("foo.foo"), "foo.foo", "singleChar replaced");
61        t.eq(r2v("foo\\\\foo"), "foo\\foo", "escape removed");
62        t.eq(r2v("foo!foo"), "foo!!foo", "escapes !");
63        t.eq(r2v("foo\\*foo"), "foo!*foo", "replaces escape on *");
64        t.eq(r2v("foo\\.foo"), "foo!.foo", "replaces escape on .");
65        t.eq(r2v("foo\\\\.foo"), "foo\\.foo", "unescapes only \\ before .");
66       
67    }
68   
69    function test_evaluate(t) {
70       
71        var cases = [{
72            filter: new OpenLayers.Filter.Comparison({
73                type: OpenLayers.Filter.Comparison.BETWEEN,
74                property: "area",
75                lowerBoundary: 1000,
76                upperBoundary: 4999
77            }),
78            context: {area: 999},
79            expect: false
80        }, {
81            filter: new OpenLayers.Filter.Comparison({
82                type: OpenLayers.Filter.Comparison.BETWEEN,
83                property: "area",
84                lowerBoundary: 1000,
85                upperBoundary: 4999
86            }),
87            context: {area: 1000},
88            expect: true
89        }, {
90            filter: new OpenLayers.Filter.Comparison({
91                type: OpenLayers.Filter.Comparison.BETWEEN,
92                property: "area",
93                lowerBoundary: 1000,
94                upperBoundary: 4999
95            }),
96            context: {area: 4999},
97            expect: true
98        }, {
99            filter: new OpenLayers.Filter.Comparison({
100                type: OpenLayers.Filter.Comparison.BETWEEN,
101                property: "area",
102                lowerBoundary: 1000,
103                upperBoundary: 4999
104            }),
105            context: {area: 5000},
106            expect: false
107        }, {
108            filter: new OpenLayers.Filter.Comparison({
109                type: OpenLayers.Filter.Comparison.BETWEEN,
110                property: "area",
111                lowerBoundary: 1000,
112                upperBoundary: 4999
113            }),
114            context: {area: 999},
115            expect: false
116        }, {
117            filter: new OpenLayers.Filter.Comparison({
118                type: OpenLayers.Filter.Comparison.EQUAL_TO,
119                property: "prop",
120                value: "Foo"
121            }),
122            context: {prop: "Foo"},
123            expect: true
124        }, {
125            filter: new OpenLayers.Filter.Comparison({
126                type: OpenLayers.Filter.Comparison.EQUAL_TO,
127                property: "prop",
128                value: "Foo"
129            }),
130            context: {prop: "foo"},
131            expect: false
132        }, {
133            filter: new OpenLayers.Filter.Comparison({
134                type: OpenLayers.Filter.Comparison.EQUAL_TO,
135                matchCase: true,
136                property: "prop",
137                value: "Foo"
138            }),
139            context: {prop: "foo"},
140            expect: false
141        }, {
142            filter: new OpenLayers.Filter.Comparison({
143                type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
144                property: "prop",
145                value: "foo"
146            }),
147            context: {prop: "FOO"},
148            expect: true
149        }, {
150            filter: new OpenLayers.Filter.Comparison({
151                type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
152                matchCase: true,
153                property: "prop",
154                value: "foo"
155            }),
156            context: {prop: "FOO"},
157            expect: true
158        }, {
159            filter: new OpenLayers.Filter.Comparison({
160                type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
161                matchCase: false,
162                property: "prop",
163                value: "foo"
164            }),
165            context: {prop: "FOO"},
166            expect: false
167        }];
168       
169        t.plan(cases.length);
170       
171        var c;
172        for(var i=0; i<cases.length; ++i) {
173            c = cases[i];
174            t.eq(c.filter.evaluate(c.context), c.expect, "case " + i + ": " + c.filter.type);
175        }
176       
177    }
178
179    function test_evaluate_feature(t) {
180       
181        var cases = [{
182            filter: new OpenLayers.Filter.Comparison({
183                type: OpenLayers.Filter.Comparison.BETWEEN,
184                property: "area",
185                lowerBoundary: 1000,
186                upperBoundary: 4999
187            }),
188            context: new OpenLayers.Feature.Vector(null, {area: 999}),
189            expect: false
190        }, {
191            filter: new OpenLayers.Filter.Comparison({
192                type: OpenLayers.Filter.Comparison.BETWEEN,
193                property: "area",
194                lowerBoundary: 1000,
195                upperBoundary: 4999
196            }),
197            context: new OpenLayers.Feature.Vector(null, {area: 1000}),
198            expect: true
199        }, {
200            filter: new OpenLayers.Filter.Comparison({
201                type: OpenLayers.Filter.Comparison.BETWEEN,
202                property: "area",
203                lowerBoundary: 1000,
204                upperBoundary: 4999
205            }),
206            context: new OpenLayers.Feature.Vector(null, {area: 4999}),
207            expect: true
208        }, {
209            filter: new OpenLayers.Filter.Comparison({
210                type: OpenLayers.Filter.Comparison.BETWEEN,
211                property: "area",
212                lowerBoundary: 1000,
213                upperBoundary: 4999
214            }),
215            context: new OpenLayers.Feature.Vector(null, {area: 5000}),
216            expect: false
217        }, {
218            filter: new OpenLayers.Filter.Comparison({
219                type: OpenLayers.Filter.Comparison.BETWEEN,
220                property: "area",
221                lowerBoundary: 1000,
222                upperBoundary: 4999
223            }),
224            context: new OpenLayers.Feature.Vector(null, {area: 999}),
225            expect: false
226        }, {
227            filter: new OpenLayers.Filter.Comparison({
228                type: OpenLayers.Filter.Comparison.EQUAL_TO,
229                property: "prop",
230                value: "Foo"
231            }),
232            context: new OpenLayers.Feature.Vector(null, {prop: "Foo"}),
233            expect: true
234        }, {
235            filter: new OpenLayers.Filter.Comparison({
236                type: OpenLayers.Filter.Comparison.EQUAL_TO,
237                property: "prop",
238                value: "Foo"
239            }),
240            context: new OpenLayers.Feature.Vector(null, {prop: "foo"}),
241            expect: false
242        }, {
243            filter: new OpenLayers.Filter.Comparison({
244                type: OpenLayers.Filter.Comparison.EQUAL_TO,
245                matchCase: true,
246                property: "prop",
247                value: "Foo"
248            }),
249            context: new OpenLayers.Feature.Vector(null, {prop: "foo"}),
250            expect: false
251        }, {
252            filter: new OpenLayers.Filter.Comparison({
253                type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
254                property: "prop",
255                value: "foo"
256            }),
257            context: {prop: "FOO"},
258            expect: true
259        }, {
260            filter: new OpenLayers.Filter.Comparison({
261                type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
262                matchCase: true,
263                property: "prop",
264                value: "foo"
265            }),
266            context: new OpenLayers.Feature.Vector(null, {prop: "FOO"}),
267            expect: true
268        }, {
269            filter: new OpenLayers.Filter.Comparison({
270                type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
271                matchCase: false,
272                property: "prop",
273                value: "foo"
274            }),
275            context: new OpenLayers.Feature.Vector(null, {prop: "FOO"}),
276            expect: false
277        }];
278       
279        t.plan(cases.length);
280
281        var c;
282        for(var i=0; i<cases.length; ++i) {
283            c = cases[i];
284            t.eq(c.filter.evaluate(c.context), c.expect, "case " + i + ": " + c.filter.type);
285        }
286       
287    }
288       
289    function test_clone(t) {
290       
291        t.plan(3);
292       
293        var filter = new OpenLayers.Filter.Comparison({
294            type: OpenLayers.Filter.Comparison.EQUAL_TO,
295            property: "prop",
296            value: "val"
297        });
298       
299        var clone = filter.clone();
300       
301        // modify the original
302        filter.type = OpenLayers.Filter.Comparison.NOT_EQUAL_TO;
303       
304        t.eq(clone.type, OpenLayers.Filter.Comparison.EQUAL_TO, "clone has proper type");
305        t.eq(clone.property, "prop", "clone has proper property");
306        t.eq(clone.value, "val", "clone has proper value");
307       
308        filter.destroy();
309
310    }
311
312
313    </script> 
314</head> 
315<body> 
316</body> 
317</html> 
Note: See TracBrowser for help on using the repository browser.