| 1 |
<html> |
|---|
| 2 |
<head> |
|---|
| 3 |
<script src="../../lib/OpenLayers.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 |
t.plan(5); |
|---|
| 71 |
|
|---|
| 72 |
var filter = new OpenLayers.Filter.Comparison({ |
|---|
| 73 |
property: "area", |
|---|
| 74 |
lowerBoundary: 1000, |
|---|
| 75 |
upperBoundary: 4999, |
|---|
| 76 |
type: OpenLayers.Filter.Comparison.BETWEEN}); |
|---|
| 77 |
|
|---|
| 78 |
var features = [ |
|---|
| 79 |
new OpenLayers.Feature.Vector(null, { |
|---|
| 80 |
area: 999}), |
|---|
| 81 |
new OpenLayers.Feature.Vector(null, { |
|---|
| 82 |
area: 1000}), |
|---|
| 83 |
new OpenLayers.Feature.Vector(null, { |
|---|
| 84 |
area: 4999}), |
|---|
| 85 |
new OpenLayers.Feature.Vector(null, { |
|---|
| 86 |
area: 5000})]; |
|---|
| 87 |
// PropertyIsBetween filter: lower and upper boundary are inclusive |
|---|
| 88 |
var filterResults = { |
|---|
| 89 |
0: false, |
|---|
| 90 |
1: true, |
|---|
| 91 |
2: true, |
|---|
| 92 |
3: false}; |
|---|
| 93 |
for (var i in filterResults) { |
|---|
| 94 |
var result = filter.evaluate(features[i].attributes); |
|---|
| 95 |
t.eq(result, filterResults[i], "feature "+i+ |
|---|
| 96 |
" evaluates to "+result.toString()+" correctly."); |
|---|
| 97 |
} |
|---|
| 98 |
var context = { |
|---|
| 99 |
area: 4998 |
|---|
| 100 |
}; |
|---|
| 101 |
var result = filter.evaluate(context); |
|---|
| 102 |
t.eq(result, true, "evaluation against custom filter context works."); |
|---|
| 103 |
} |
|---|
| 104 |
</script> |
|---|
| 105 |
</head> |
|---|
| 106 |
<body> |
|---|
| 107 |
</body> |
|---|
| 108 |
</html> |
|---|