testing/xpcshell/example/unit/test_do_check_matches.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     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
    10   // Property order is irrelevant.
    11   do_check_matches({x:"foo", y:"bar"}, {y:"bar", x:"foo"});// pass
    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
    17   // Patterns nest.
    18   do_check_matches({a:1, b:{c:2,d:undefined}}, {a:1, b:{c:2,d:3}});
    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
    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
    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 }

mercurial