michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function run_test() { michael@0: do_check_matches({x:1}, {x:1}); // pass michael@0: todo_check_matches({x:1}, {}); // fail: all pattern props required michael@0: todo_check_matches({x:1}, {x:2}); // fail: values must match michael@0: do_check_matches({x:1}, {x:1, y:2}); // pass: extra props tolerated michael@0: michael@0: // Property order is irrelevant. michael@0: do_check_matches({x:"foo", y:"bar"}, {y:"bar", x:"foo"});// pass michael@0: michael@0: do_check_matches({x:undefined}, {x:1});// pass: 'undefined' is wildcard michael@0: do_check_matches({x:undefined}, {x:2}); michael@0: todo_check_matches({x:undefined}, {y:2});// fail: 'x' must still be there michael@0: michael@0: // Patterns nest. michael@0: do_check_matches({a:1, b:{c:2,d:undefined}}, {a:1, b:{c:2,d:3}}); michael@0: michael@0: // 'length' property counts, even if non-enumerable. michael@0: do_check_matches([3,4,5], [3,4,5]); // pass michael@0: todo_check_matches([3,4,5], [3,5,5]); // fail; value doesn't match michael@0: todo_check_matches([3,4,5], [3,4,5,6]);// fail; length doesn't match michael@0: michael@0: // functions in patterns get applied. michael@0: do_check_matches({foo:function (v) v.length == 2}, {foo:"hi"});// pass michael@0: todo_check_matches({foo:function (v) v.length == 2}, {bar:"hi"});// fail michael@0: todo_check_matches({foo:function (v) v.length == 2}, {foo:"hello"});// fail michael@0: michael@0: // We don't check constructors, prototypes, or classes. However, if michael@0: // pattern has a 'length' property, we require values to match that as michael@0: // well, even if 'length' is non-enumerable in the pattern. So arrays michael@0: // are useful as patterns. michael@0: do_check_matches({0:0, 1:1, length:2}, [0,1]); // pass michael@0: do_check_matches({0:1}, [1,2]); // pass michael@0: do_check_matches([0], {0:0, length:1}); // pass michael@0: }