testing/xpcshell/example/unit/test_do_check_matches.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/example/unit/test_do_check_matches.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,37 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function run_test() {
     1.8 +  do_check_matches({x:1}, {x:1});      // pass
     1.9 +  todo_check_matches({x:1}, {});       // fail: all pattern props required
    1.10 +  todo_check_matches({x:1}, {x:2});    // fail: values must match
    1.11 +  do_check_matches({x:1}, {x:1, y:2}); // pass: extra props tolerated
    1.12 +
    1.13 +  // Property order is irrelevant.
    1.14 +  do_check_matches({x:"foo", y:"bar"}, {y:"bar", x:"foo"});// pass
    1.15 +
    1.16 +  do_check_matches({x:undefined}, {x:1});// pass: 'undefined' is wildcard
    1.17 +  do_check_matches({x:undefined}, {x:2});
    1.18 +  todo_check_matches({x:undefined}, {y:2});// fail: 'x' must still be there
    1.19 +
    1.20 +  // Patterns nest.
    1.21 +  do_check_matches({a:1, b:{c:2,d:undefined}}, {a:1, b:{c:2,d:3}});
    1.22 +
    1.23 +  // 'length' property counts, even if non-enumerable.
    1.24 +  do_check_matches([3,4,5], [3,4,5]);    // pass
    1.25 +  todo_check_matches([3,4,5], [3,5,5]);  // fail; value doesn't match
    1.26 +  todo_check_matches([3,4,5], [3,4,5,6]);// fail; length doesn't match
    1.27 +
    1.28 +  // functions in patterns get applied.
    1.29 +  do_check_matches({foo:function (v) v.length == 2}, {foo:"hi"});// pass
    1.30 +  todo_check_matches({foo:function (v) v.length == 2}, {bar:"hi"});// fail
    1.31 +  todo_check_matches({foo:function (v) v.length == 2}, {foo:"hello"});// fail
    1.32 +
    1.33 +  // We don't check constructors, prototypes, or classes. However, if
    1.34 +  // pattern has a 'length' property, we require values to match that as
    1.35 +  // well, even if 'length' is non-enumerable in the pattern. So arrays
    1.36 +  // are useful as patterns.
    1.37 +  do_check_matches({0:0, 1:1, length:2}, [0,1]); // pass
    1.38 +  do_check_matches({0:1}, [1,2]);                // pass
    1.39 +  do_check_matches([0], {0:0, length:1});        // pass
    1.40 +}

mercurial