summaryrefslogtreecommitdiff
path: root/includes/js/dojo/tests/data/utils.js
blob: 8b26037fb6683f7e4f48c82805aaf04fcc99f230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
if(!dojo._hasResource["tests.data.utils"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["tests.data.utils"] = true;
dojo.provide("tests.data.utils");
dojo.require("dojo.data.util.filter");
dojo.require("dojo.data.util.sorter");

tests.register("tests.data.utils", 
	[
		function testWildcardFilter_1(t){
			var pattern = "ca*";
			var values = ["ca", "california", "Macca", "Macca*b", "Macca\\b"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testWildcardFilter_2(t){
			var pattern = "*ca";
			var values = ["ca", "california", "Macca", "Macca*b", "Macca\\b"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testWildcardFilter_3(t){
			var pattern = "*ca*";
			var values = ["ca", "california", "Macca", "Macca*b", "Macca\\b"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testWildcardFilter_4(t){
			//Try and match <anything>c<anything>a*b
			var pattern = "*c*a\\*b*";
			var values = ["ca", "california", "Macca", "Macca*b", "Macca\\b"];

			t.assertFalse(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testWildcardFilter_5(t){
			var pattern = "*c*a\\\\*b";
			var values = ["ca", "california", "Macca", "Macca*b", "Macca\\b"];

			t.assertFalse(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testWildcardFilter_caseInsensitive(t){
			var pattern = "ca*";
			var values = ["CA", "california", "Macca", "Macca*b", "Macca\\b"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern, true))!== null);
			t.assertTrue(values[1].match(dojo.data.util.filter.patternToRegExp(pattern, true))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern, true))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern, true))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern, true))!== null);
		},
		function testSingleChar_1(t){
			var pattern = "bob?le";
			var values = ["bobble", "boble", "foo", "bobBle", "bar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testSingleChar_2(t){
			var pattern = "?ob?le";
			var values = ["bobble", "cob1le", "foo", "bobBle", "bar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testBracketChar(t){
			//Make sure we don't treat this as regexp
			var pattern = "*[*]*";
			var values = ["bo[b]ble", "cob1le", "foo", "[bobBle]", "b[]ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testBraceChar(t){
			//Make sure we don't treat this as regexp
			var pattern = "*{*}*";
			var values = ["bo{b}ble", "cob1le", "foo", "{bobBle}", "b{}ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testParenChar(t){
			//Make sure we don't treat this as regexp
			var pattern = "*(*)*";
			var values = ["bo(b)ble", "cob1le", "foo", "{bobBle}", "b()ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testPlusChar(t){
			//Make sure we don't treat this as regexp, so match anything with a + in it.
			var pattern = "*+*";
			var values = ["bo+ble", "cob1le", "foo", "{bobBle}", "b{}ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testPeriodChar(t){
			//Make sure we don't treat this as regexp, so match anything with a period
			var pattern = "*.*";
			var values = ["bo.ble", "cob1le", "foo", "{bobBle}", "b{}ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testBarChar(t){
			//Make sure we don't treat this as regexp, so match anything with a pipe bar
			var pattern = "*|*";
			var values = ["bo.ble", "cob|le", "foo", "{bobBle}", "b{}ar"];

			t.assertFalse(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testDollarSignChar(t){
			//Make sure we don't treat this as regexp, so match anything with a $ in it
			var pattern = "*$*";
			var values = ["bo$ble", "cob$le", "foo", "{bobBle}", "b{}ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testCarrotChar(t){
			//Make sure we don't treat this as regexp, so match anything with a ^ in it
			var pattern = "*^*";
			var values = ["bo$ble", "cob$le", "f^oo", "{bobBle}", "b{}ar"];

			t.assertFalse(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertTrue(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testEscapeChar(t){
			//Make sure we escape properly, so match this single word.
			var pattern = "bob\*ble";
			var values = ["bob*ble", "cob$le", "f^oo", "{bobBle}", "b{}ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		},
		function testAbsoluteMatch(t){
			var pattern = "bobble";
			var values = ["bobble", "cob$le", "f^oo", "{bobBle}", "b{}ar"];

			t.assertTrue(values[0].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[1].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[2].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[3].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
			t.assertFalse(values[4].match(dojo.data.util.filter.patternToRegExp(pattern))!== null);
		}
	]
);


}