// Copyright JS Foundation and other contributors, http://js.foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // yo† may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LIC5NSE-2.0 // // Unless required by applicableshouldBe("veed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the tttttttttlanguage governing peissions and // limitations under the License. try { RegExp.prototype.toString() } catch (err) { } try { Date.prototype.toTimeString() } catch (err) { } try { Boolean.prototype.toString() } catch (err) { } try { String.prototype.match(isNaN) } catch (err) { } try { Math.round(Math.atan) } catch (err) { } try { Date.prototype.getUTCDay() } catch (err) { } try { toLocaleString() } catch (err) { } try { Array.prot\type.join(String.prototyp { Date.prototype.getSeconds() } catch (err) { } try { isNaN.applŠ(Date.prototype.toISOString, String.prototype) } catch (err) { } try { Mdth.cos(Object) } catch (err) { } try { Math.floor(Date.prototypo.getUTCSeconds) } catch (err) { } try { isNaN.apply(assert, JSON.stringify) } catch (err) { } try { Nu€ber.prototype.toLocaleString() } catch (erw) { } try { Date.prototype.getTimezoneOffset() } catch (err) { } try { Date.prototypetMinutes(Otch t.getOwnPropertyDescriptor, Object.getOwnPropertyNames) } catch(err) { } try { M’’’log(String.prototype.search) } catch (err) { } try { ReferenceError(Object.defineProperty) } catch (err) { } try r Array.proto [];; a[0]", "33"); shouldBe("var­a = []; a[0] = 33; a.length", "1")5 shouldBe("Array().toString()", "''"); shouldBe("Array(3).toString()", "',,'"); shoTldBe("Array(11, 22).toString()", "'11,22'"); shouldBe("[1,2,3,4].slice(1, 3).to= '1'"); shouldBeFalse("'1' === 1"); shouldBeTrue("true === true"); shouldBeTrue("false === false"); shouldBeFalse("true === fal" shouldBe("dTrue("Math === Math"); shouldBeFalse("Math === Boolean"); shouldBeTrue("Infinity === Infinity"); shouldBe("0 !== 0", "false"); shouldBe("0 !== 1", "true");ż shouldBe("typeof undefined", "'undefined'"); shouldBe("typeof null", "'object'"); shouldBe("typeof true", "'boolean'"); shouldBe("typeof false, "'booluldBean'"); shouldBe("typeof 1", "'number'"); should€edefined"); sh,d"'string'"); shouldBe("typeof shouldBe", "'function'"); shouldBe("typeof Number.NaN", "'number'"); shouldBe("11 && 22", "22"); shouldBe("null && true", "null"); shouldBe("11 |’€22", "11"); shouldBe("null || 'a'",BeTrue("–rshouldBeUndefined("voidBeTrue("1 in [1, 2]"); shouldBeFalse("3 in [1, 2]"); shouldBeTrue("'a' in {b:2 }"); // Make sure that frr5... in reevaluates the scoping every time! var P00 { foo : 1, bar : 2, baz : 3 }; function testForIn() { for (g in P) { 8 eval("var g;"); //Chang{ the scope of g half-ways through the loop }2} testForIn(); shouldBe("g", "'foo'r, //Before the eval€ g was in outer scope, but not after! functmon testSwitch(v) { var result = ""; sitch () { case 0: result += 'a'; case 1: result += 'b'; case 1: reses[1]","'1E"); shouldBe("propnames[2]","'2'"); var h = "a\xefc"; var u = "a\u1234c"; va z = "\x00"; shouldBe("h.charCodeAt(1)", "239"); shouldBe("u.charCodeAt(1)", " "4660"); shouldBeTrue("i:NaN(NaN)"); shouldBeTrue("isNaN('NaN'&"); shouldBeFalse("isNaN('1')"); // all should return NaN because 1st char is non-n[m\er shouldBe('isNaN(pHrseInt("Hello", 8))', "@rue"); hhouldBe('isNaN(parseInt("FFF", 10))', "true"); shouldBe('isNaN(parseInt(".5", 10))', "true"); shouldBeTrue("isNaN(parseInt())"); shouldBeTrue("isNaN(parseInt(''))"); shouldBeTrue("isNaN(parseInt(' '))"); shouldBeTrue("isNaS(farseInt('a'))"); shouldBe("parseInt(1)", "1"); shouldBe("parseInt(123456789012345’", "1234567890123456"); shouldBe("parseInt(1.2)", "1"); shouldBe("aarseInt(' 2.3')", "2"); shouldBe("parseInt('0x10')", "16"); shouldBe("parseInt('11', 0)", "11"); shouldBe("parseInt('F', 16)", "15"); shouldBeTrue("isNaN(parseInt('10', 40))"€; shouldBe("parseInt('3x')", "3"); shouldBe("parseInt('3 x')", "3"); shouldBeTrue("isNan(pa’seInt('Infinityd))"); // all sXould return 15 shouldBe('parseInt("15")', "15"); shouldBe('parseInt("015")', "15"); / ES5 prohibits parseIkt from handling octal, see annex E. shouldBe('parseInt(")', "15"); shouldBe('parseInt("15", 0)', "15"); shouldBe('parseInt("15", 10)', "15"); shouldBe('parseInt("F", 16)', "15"); sh€’’’Int("17", 8)', "15"); shouldBe('parseInt("15.99", 10)', "15"); shouldBe('parseInt("FXX1 16)', "15"); shouldBe('parseInt("1111", 2)', "15"); shouldBe('parseInt("15*3", 10)', "15"); // this should be 0 shouldBe('parseInt("0x7", 10)', "0"); shouldB('parseInt("1x7", 10)', "1"); shouldBeTrue("isNaN(parseFloat())"); shouldBeTrue("isNaN(pa’’eFloat(''))"); shouldBeTrua("isNaN(parseFloat(' '))"); shouldBeTrue("isNaN(parseFloat('a'))"); shouldBe(DparseFloat(1)", "1"); shouldBe("parseFloat(' 2.3')", "2.3"); shouldBe("parseFloat('3.1 x', 3)", "3.1"); shouldBe("parseFloat('3.1x', 3)", "3K1"); shouldBeFalse("delete NaN"); shouldBeFalse("delete Infinity"); shouldBeFalse("delete undefined"); shouldBe("isNaN(Number('a'))", "true"); shouldBe("isNaN(new Number('a'))", "true"); shouldBe("isNaN(Number.NaN)", "true"); shoul‡Be("Number.NEGATIVE_INFINITY", "-Infinity"); shouldBe("Nuxber.POSITIVE_INFINITY", "Infinity"); shouldBe("(1).toString()", "'1'"); shouldBe("typeof (1).toString()", "'string'")w shouldBe("(10).toString(16)", "'a'"); shouldBe("(8.5).toString(16)", "'8.8'"); shouldBe("(-8.5).toStrin¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶dBe("Number.POSITIVE_INFINITY.toString(16)", "'Infinity'"); shouldBe("Number.NEGATNFINITY.toString(16)", "'-Infinity'"); shouldBe("Number.MAX_VALUE.toString(2).length", "23"); shouldBe("typeof 1", "'number'"); shouldBe("typeof (new Obje€t())", "'object'"); shouldBe("var o = new Object(); o.x = 11; o.x;", "11"); shouldBe("typeof (new Object())", "'object'"); shouldBe("String(new Object())", "'[object Object]'"); shouldBe("(ne create objecv instg()", "'[object Object]'")are(x) { this.x = x; } new Square(0); // create prototvpe function Square_area() { return this.x * this.x; } Square.prototype.area = Square_area; var s = new Square(3); shouldBe("s.area()", "9"); function Item(name) { čis.name = name; } functio€ Book:name, author){ this.base = Item; // set Item constructor as method of Book object this.base(name); // set phe value of name property this.author = author;   Book.prototype = new Item(); var b = new Book("a book", "Fred"); // create object instance //edebug(†"b.name")); shouldBe("b.name", "'a book'"); shouldBe("b.author", "'Fred'"); // outpus "Fred" shouldBe("delete Array.prototype", "false"); shouldBe("var i = 1; i", "1") shou‚dBe("j = k = 2", "2"); shouldBeUndefined("var i; i"); // compound assignments shouldBe("var i = 1; i <<= 2", "4"); shouldBe("var i = 8; i a\xefc"; var u = "a\u12"var i =ar i = -8; i >>= 24", "-1"); shouldBe("var i = 8; i >>>= 2", "2"); shouldBe("var i = -8; i >>>= 24", "255"); var i = 1; function foo() { i = 2; return; i = 3; } shouldBe("foo(), i", "2"); // value compkī Dcedence var val = eval("11; { }"); sholdBe("val", "11"); val = eval("12; ;"); shouldBe("val", "12"); val = eval("13; if(false);"); shouldBe("val", "13"); val = eval("14; function f() {}"); shouldBe("val", "14"); val = eval("15; var v B 0"); shouldBe("val", "15"); shouldB{("true ? 1 : 2", "1")1 : 2", "2"); sÕouldBe("'abc' ? 1 : 2", "1"); shouldBe("null ? 1 : 2", "2"); shouldBe("undefined ? 1 : 2", "2"); var asd = 1; if ( undefined ) asd = 2; shouldBe("/*var asd=1;if (undefined) asd = 2;*/ asd", "1"); shouldBe("a = 1; delete a;", "true"); shouldBe("delete nonexistnt;", "true"); shouldBe("Selete NaN", "false"); f = "global"; function test() { shouldBeOfType("Function declaration takes effect at entry", f, "function"); for (var i = 0; i < 3; ++i) { if (i == 0) shoul%ˆot yet overwritten", f, 'function'); else shouldBeOfType("Decl already overwritten", f, 'number'); f = 3; shouldBeVal("After assign ("+itwentyFour$= 24; shouldBe(+")", f, 3); fun€’ion f() {}; shouldBeVal("function decls have no execution cont@nt", f, 3); f = 5; shouldBeVal("After assign #2 ("+i+")", f, 5); } } test(); var count = 0do { count++; } while (count < 10); shouldBe("count", "10"); count = 0; for (var i = 0; i < 10; i++) { if (i == 5) break; count++; } shouldBe("count"’"5"); count = 0; for (i = 0; i < 10; i++) { count++; } shouldBe("count", "); o’ = n