js/src/tests/ecma_5/Date/equality-to-boolean.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_5/Date/equality-to-boolean.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,39 @@
     1.4 +// Any copyright is dedicated to the Public Domain.
     1.5 +// http://creativecommons.org/licenses/publicdomain/
     1.6 +
     1.7 +//-----------------------------------------------------------------------------
     1.8 +print("Test for correct implementation of |Date == boolean| and vice versa");
     1.9 +
    1.10 +/**************
    1.11 + * BEGIN TEST *
    1.12 + **************/
    1.13 +
    1.14 +Date.prototype.toString = function() { return 1; };
    1.15 +Date.prototype.valueOf = function() { return 0; };
    1.16 +
    1.17 +/*
    1.18 + * ES5 11.9.3 doesn't directly handle obj == boolean.  Instead it translates it
    1.19 + * as follows:
    1.20 + *
    1.21 + *   obj == boolean
    1.22 + *   ↳ obj == ToNumber(boolean), per step 7
    1.23 + *     ↳ ToPrimitive(obj) == ToNumber(boolean), per step 9
    1.24 + *
    1.25 + * ToPrimitive calls [[DefaultValue]] with no hint.  For Date objects this is
    1.26 + * treated as if it were instead called with hint String.  That calls toString,
    1.27 + * which returns 1, so Date objects here should compare equal to true and
    1.28 + * unequal to false.
    1.29 + */
    1.30 +assertEq(new Date == true, true);
    1.31 +assertEq(new Date == false, false);
    1.32 +
    1.33 +/* == is symmetric. */
    1.34 +assertEq(true == new Date, true);
    1.35 +assertEq(false == new Date, false);
    1.36 +
    1.37 +/******************************************************************************/
    1.38 +
    1.39 +if (typeof reportCompare === "function")
    1.40 +  reportCompare(true, true);
    1.41 +
    1.42 +print("Tests complete");

mercurial