summaryrefslogtreecommitdiffstats
path: root/toolkit/components/utils/test/unit/test_FilterExpressions.js
blob: 3fd09a95d3579af9e228ff20ff25b107982f99b8 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  FilterExpressions:
    "resource://gre/modules/components-utils/FilterExpressions.sys.mjs",
});

// Basic JEXL tests
add_task(async function () {
  let val;
  // Test that basic expressions work
  val = await FilterExpressions.eval("2+2");
  equal(val, 4, "basic expression works");

  // Test that multiline expressions work
  val = await FilterExpressions.eval(`
    2
    +
    2
  `);
  equal(val, 4, "multiline expression works");

  // Test that it reads from the context correctly.
  val = await FilterExpressions.eval("first + second + 3", {
    first: 1,
    second: 2,
  });
  equal(val, 6, "context is available to filter expressions");
});

// Date tests
add_task(async function () {
  let val;
  // Test has a date transform
  val = await FilterExpressions.eval('"2016-04-22"|date');
  const d = new Date(Date.UTC(2016, 3, 22)); // months are 0 based
  equal(val.toString(), d.toString(), "Date transform works");

  // Test dates are comparable
  const context = { someTime: Date.UTC(2016, 0, 1) };
  val = await FilterExpressions.eval('"2015-01-01"|date < someTime', context);
  ok(val, "dates are comparable with less-than");
  val = await FilterExpressions.eval('"2017-01-01"|date > someTime', context);
  ok(val, "dates are comparable with greater-than");
});

// Sampling tests
add_task(async function () {
  let val;
  // Test stable sample returns true for matching samples
  val = await FilterExpressions.eval('["test"]|stableSample(1)');
  ok(val, "Stable sample returns true for 100% sample");

  // Test stable sample returns true for matching samples
  val = await FilterExpressions.eval('["test"]|stableSample(0)');
  ok(!val, "Stable sample returns false for 0% sample");

  // Test stable sample for known samples
  val = await FilterExpressions.eval('["test-1"]|stableSample(0.5)');
  ok(val, "Stable sample returns true for a known sample");
  val = await FilterExpressions.eval('["test-4"]|stableSample(0.5)');
  ok(!val, "Stable sample returns false for a known sample");

  // Test bucket sample for known samples
  val = await FilterExpressions.eval('["test-1"]|bucketSample(0, 5, 10)');
  ok(val, "Bucket sample returns true for a known sample");
  val = await FilterExpressions.eval('["test-4"]|bucketSample(0, 5, 10)');
  ok(!val, "Bucket sample returns false for a known sample");
});

// Preference tests
add_task(async function () {
  let val;
  // Compare the value of the preference
  Services.prefs.setIntPref("normandy.test.value", 3);
  registerCleanupFunction(() =>
    Services.prefs.clearUserPref("normandy.test.value")
  );

  val = await FilterExpressions.eval(
    '"normandy.test.value"|preferenceValue == 3'
  );
  ok(val, "preferenceValue expression compares against preference values");
  val = await FilterExpressions.eval(
    '"normandy.test.value"|preferenceValue == "test"'
  );
  ok(!val, "preferenceValue expression fails value checks appropriately");

  // preferenceValue can take a default value as an optional argument, which
  // defaults to `undefined`.
  val = await FilterExpressions.eval(
    '"normandy.test.default"|preferenceValue(false) == false'
  );
  ok(
    val,
    "preferenceValue takes optional 'default value' param for prefs without set values"
  );
  val = await FilterExpressions.eval(
    '"normandy.test.value"|preferenceValue(5) == 5'
  );
  ok(
    !val,
    "preferenceValue default param is not returned for prefs with set values"
  );

  // Compare if the preference is user set
  val = await FilterExpressions.eval(
    '"normandy.test.isSet"|preferenceIsUserSet == true'
  );
  ok(
    !val,
    "preferenceIsUserSet expression determines if preference is set at all"
  );
  val = await FilterExpressions.eval(
    '"normandy.test.value"|preferenceIsUserSet == true'
  );
  ok(
    val,
    "preferenceIsUserSet expression determines if user's preference has been set"
  );

  // Compare if the preference has _any_ value, whether it's user-set or default,
  val = await FilterExpressions.eval(
    '"normandy.test.nonexistant"|preferenceExists == true'
  );
  ok(
    !val,
    "preferenceExists expression determines if preference exists at all"
  );
  val = await FilterExpressions.eval(
    '"normandy.test.value"|preferenceExists == true'
  );
  ok(val, "preferenceExists expression fails existence check appropriately");
});

// keys tests
add_task(async function testKeys() {
  let val;

  // Test an object defined in JEXL
  val = await FilterExpressions.eval("{foo: 1, bar: 2}|keys");
  Assert.deepEqual(
    new Set(val),
    new Set(["foo", "bar"]),
    "keys returns the keys from an object in JEXL"
  );

  // Test an object in the context
  let context = { ctxObject: { baz: "string", biff: NaN } };
  val = await FilterExpressions.eval("ctxObject|keys", context);

  Assert.deepEqual(
    new Set(val),
    new Set(["baz", "biff"]),
    "keys returns the keys from an object in the context"
  );

  // Test that values from the prototype are not included
  context = { ctxObject: Object.create({ fooProto: 7 }) };
  context.ctxObject.baz = 8;
  context.ctxObject.biff = 5;
  equal(
    await FilterExpressions.eval("ctxObject.fooProto", context),
    7,
    "Prototype properties are accessible via property access"
  );
  val = await FilterExpressions.eval("ctxObject|keys", context);
  Assert.deepEqual(
    new Set(val),
    new Set(["baz", "biff"]),
    "keys does not return properties from the object's prototype chain"
  );

  // Return undefined for non-objects
  equal(
    await FilterExpressions.eval("ctxObject|keys", { ctxObject: 45 }),
    undefined,
    "keys returns undefined for numbers"
  );
  equal(
    await FilterExpressions.eval("ctxObject|keys", { ctxObject: null }),
    undefined,
    "keys returns undefined for null"
  );

  // Object properties are not cached
  let pong = 0;
  context = {
    ctxObject: {
      get ping() {
        return ++pong;
      },
    },
  };
  await FilterExpressions.eval(
    "ctxObject.ping == 0 || ctxObject.ping == 1",
    context
  );
  equal(pong, 2, "Properties are not reifed");
});

add_task(async function testLength() {
  equal(
    await FilterExpressions.eval("[1, null, {a: 2, b: 3}, Infinity]|length"),
    4,
    "length returns the length of the array it's applied to"
  );

  equal(
    await FilterExpressions.eval("[]|length"),
    0,
    "length is zero for an empty array"
  );

  // Should be undefined for non-Arrays
  equal(
    await FilterExpressions.eval("5|length"),
    undefined,
    "length is undefined when applied to numbers"
  );
  equal(
    await FilterExpressions.eval("null|length"),
    undefined,
    "length is undefined when applied to null"
  );
  equal(
    await FilterExpressions.eval("undefined|length"),
    undefined,
    "length is undefined when applied to undefined"
  );
  equal(
    await FilterExpressions.eval("{a: 1, b: 2, c: 3}|length"),
    undefined,
    "length is undefined when applied to non-Array objects"
  );
});

add_task(async function testMapToProperty() {
  Assert.deepEqual(
    await FilterExpressions.eval(
      '[{a: 1}, {a: {b: 10}}, {a: [5,6,7,8]}]|mapToProperty("a")'
    ),
    [1, { b: 10 }, [5, 6, 7, 8]],
    "mapToProperty returns an array of values when applied to an array of objects all with the property defined"
  );

  Assert.deepEqual(
    await FilterExpressions.eval('[]|mapToProperty("a")'),
    [],
    "mapToProperty returns an empty array when applied to an empty array"
  );

  Assert.deepEqual(
    await FilterExpressions.eval('[{a: 1}, {b: 2}, {a: 3}]|mapToProperty("a")'),
    [1, undefined, 3],
    "mapToProperty returns an array with undefined entries where the property is undefined"
  );

  // Should be undefined for non-Arrays
  equal(
    await FilterExpressions.eval('5|mapToProperty("a")'),
    undefined,
    "mapToProperty returns undefined when applied numbers"
  );
  equal(
    await FilterExpressions.eval('null|mapToProperty("a")'),
    undefined,
    "mapToProperty returns undefined when applied null"
  );
  equal(
    await FilterExpressions.eval('undefined|mapToProperty("a")'),
    undefined,
    "mapToProperty returns undefined when applied undefined"
  );
  equal(
    await FilterExpressions.eval('{a: 1, b: 2, c: 3}|mapToProperty("a")'),
    undefined,
    "mapToProperty returns undefined when applied non-Array objects"
  );
});

// intersect tests
add_task(async function testIntersect() {
  let val;

  val = await FilterExpressions.eval("[1, 2, 3] intersect [4, 2, 6, 7, 3]");
  Assert.deepEqual(
    new Set(val),
    new Set([2, 3]),
    "intersect finds the common elements between two lists in JEXL"
  );

  const context = { left: [5, 7], right: [4, 5, 3] };
  val = await FilterExpressions.eval("left intersect right", context);
  Assert.deepEqual(
    new Set(val),
    new Set([5]),
    "intersect finds the common elements between two lists in the context"
  );

  val = await FilterExpressions.eval(
    "['string', 2] intersect [4, 'string', 'other', 3]"
  );
  Assert.deepEqual(
    new Set(val),
    new Set(["string"]),
    "intersect can compare strings"
  );

  // Return undefined when intersecting things that aren't lists.
  equal(
    await FilterExpressions.eval("5 intersect 7"),
    undefined,
    "intersect returns undefined for numbers"
  );
  equal(
    await FilterExpressions.eval("val intersect other", {
      val: null,
      other: null,
    }),
    undefined,
    "intersect returns undefined for null"
  );
  equal(
    await FilterExpressions.eval("5 intersect [1, 2, 5]"),
    undefined,
    "intersect returns undefined if only one operand is a list"
  );
});

add_task(async function test_regExpMatch() {
  let val;

  val = await FilterExpressions.eval('"foobar"|regExpMatch("^foo(.+?)$")');
  Assert.deepEqual(
    new Set(val),
    new Set(["foobar", "bar"]),
    "regExpMatch returns the matches in an array"
  );

  val = await FilterExpressions.eval('"FOObar"|regExpMatch("^foo(.+?)$", "i")');
  Assert.deepEqual(
    new Set(val),
    new Set(["FOObar", "bar"]),
    "regExpMatch accepts flags for matching"
  );

  val = await FilterExpressions.eval('"F00bar"|regExpMatch("^foo(.+?)$", "i")');
  Assert.equal(val, null, "regExpMatch returns null if there are no matches");
});

add_task(async function test_versionCompare() {
  let val;

  // 1.0.0 === 1
  val = await FilterExpressions.eval('"1.0.0"|versionCompare("1")');
  Assert.strictEqual(val, 0);

  // 1.0.0 < 1.1
  val = await FilterExpressions.eval('"1.0.0"|versionCompare("1.1")');
  Assert.less(val, 0);

  // 1.0.0 > 0.1
  val = await FilterExpressions.eval('"1.0.0"|versionCompare("0.1")');
  Assert.greater(val, 0);

  // 111.0.1 < 110
  val = await FilterExpressions.eval(`'111.0.1'|versionCompare('110') < 0`);
  Assert.strictEqual(val, false);

  // 111.0.1 < 111
  val = await FilterExpressions.eval(`'111.0.1'|versionCompare('111') < 0`);
  Assert.strictEqual(val, false);

  // 111.0.1 < 111.0.1
  val = await FilterExpressions.eval(`'111.0.1'|versionCompare('111.0.1') < 0`);
  Assert.strictEqual(val, false);

  // 111.0.1 < 111.0.2
  val = await FilterExpressions.eval(`'111.0.1'|versionCompare('111.0.2') < 0`);
  Assert.strictEqual(val, true);

  // 111.0.1 is < 112
  val = await FilterExpressions.eval(`'111.0.1'|versionCompare('112') < 0`);
  Assert.strictEqual(val, true);

  // 113.0a1 < 113
  val = await FilterExpressions.eval(`'113.0a1'|versionCompare('113') < 0`);
  Assert.strictEqual(val, true);

  // 113.0a1 < 113.0a1
  val = await FilterExpressions.eval(`'113.0a1'|versionCompare('113.0a1') < 0`);
  Assert.strictEqual(val, false);

  // 113.0a1 > 113.0a0
  val = await FilterExpressions.eval(`'113.0a1'|versionCompare('113.0a0') > 0`);
  Assert.strictEqual(val, true);

  // 113 > 113.0a0
  val = await FilterExpressions.eval(`'113'|versionCompare('113.0a0') > 0`);
  Assert.strictEqual(val, true);

  // 114 > 113.0a0
  val = await FilterExpressions.eval(`'114'|versionCompare('113.0a0') > 0`);
  Assert.strictEqual(val, true);

  // 112 > 113.0a0
  val = await FilterExpressions.eval(`'112'|versionCompare('113.0a0') > 0`);
  Assert.strictEqual(val, false);
});