|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function run_test() { |
|
5 do_check_matches({x:1}, {x:1}); // pass |
|
6 todo_check_matches({x:1}, {}); // fail: all pattern props required |
|
7 todo_check_matches({x:1}, {x:2}); // fail: values must match |
|
8 do_check_matches({x:1}, {x:1, y:2}); // pass: extra props tolerated |
|
9 |
|
10 // Property order is irrelevant. |
|
11 do_check_matches({x:"foo", y:"bar"}, {y:"bar", x:"foo"});// pass |
|
12 |
|
13 do_check_matches({x:undefined}, {x:1});// pass: 'undefined' is wildcard |
|
14 do_check_matches({x:undefined}, {x:2}); |
|
15 todo_check_matches({x:undefined}, {y:2});// fail: 'x' must still be there |
|
16 |
|
17 // Patterns nest. |
|
18 do_check_matches({a:1, b:{c:2,d:undefined}}, {a:1, b:{c:2,d:3}}); |
|
19 |
|
20 // 'length' property counts, even if non-enumerable. |
|
21 do_check_matches([3,4,5], [3,4,5]); // pass |
|
22 todo_check_matches([3,4,5], [3,5,5]); // fail; value doesn't match |
|
23 todo_check_matches([3,4,5], [3,4,5,6]);// fail; length doesn't match |
|
24 |
|
25 // functions in patterns get applied. |
|
26 do_check_matches({foo:function (v) v.length == 2}, {foo:"hi"});// pass |
|
27 todo_check_matches({foo:function (v) v.length == 2}, {bar:"hi"});// fail |
|
28 todo_check_matches({foo:function (v) v.length == 2}, {foo:"hello"});// fail |
|
29 |
|
30 // We don't check constructors, prototypes, or classes. However, if |
|
31 // pattern has a 'length' property, we require values to match that as |
|
32 // well, even if 'length' is non-enumerable in the pattern. So arrays |
|
33 // are useful as patterns. |
|
34 do_check_matches({0:0, 1:1, length:2}, [0,1]); // pass |
|
35 do_check_matches({0:1}, [1,2]); // pass |
|
36 do_check_matches([0], {0:0, length:1}); // pass |
|
37 } |