File indexing completed on 2024-04-28 15:28:39
0001 /* Copyright (C) 2018 froglogic GmbH */ 0002 0003 ///////////// String.prototype.startsWith() //////////////// 0004 0005 shouldBe("String.prototype.startsWith.length", "1"); 0006 0007 // simple 0008 shouldBeTrue("'abc'.startsWith('a')"); 0009 shouldBeTrue("'abc'.startsWith('ab')"); 0010 shouldBeFalse("'abc'.startsWith('b')"); 0011 shouldBeFalse("'abc'.startsWith('x')"); 0012 shouldBeFalse("'abc'.startsWith('abcd')"); 0013 0014 // with start position 0015 shouldBeTrue("'abc'.startsWith('a', 0)"); 0016 shouldBeTrue("'abc'.startsWith('a', -1)"); 0017 shouldBeTrue("'abc'.startsWith('b', 1)"); 0018 shouldBeFalse("'abc'.startsWith('c', 4)"); 0019 shouldBeTrue("'abc'.startsWith('a', -Infinity)"); 0020 shouldBeFalse("'abc'.startsWith('a', +Infinity)"); 0021 0022 // non-numerical position parameter 0023 shouldBeTrue("'abc'.startsWith('a', undefined)"); 0024 shouldBeTrue("'abc'.startsWith('a', null)"); 0025 shouldBeTrue("'abc'.startsWith('a', NaN)"); 0026 shouldBeTrue("'abc'.startsWith('a', '0')"); 0027 shouldBeFalse("'abc'.startsWith('a', '1')"); 0028 0029 // non-string objects 0030 shouldBeTrue("'123'.startsWith(1)"); 0031 shouldBeFalse("'123'.startsWith(4)"); 0032 var arr = ['a', 'b']; 0033 arr.startsWith = String.prototype.startsWith; 0034 shouldBeTrue("arr.startsWith('a')"); 0035 var obj = { toString: function() { return "xyz"; } }; 0036 obj.startsWith = String.prototype.startsWith; 0037 shouldBeTrue("obj.startsWith('xyz')"); 0038 0039 // invalid 0040 shouldThrow("'abc'.startsWith(/a/)"); 0041 0042 ///////////// String.prototype.endsWith() //////////////// 0043 0044 shouldBe("String.prototype.endsWith.length", "1"); 0045 0046 // simple 0047 shouldBeTrue("'abc'.endsWith('c')"); 0048 shouldBeTrue("'abc'.endsWith('bc')"); 0049 shouldBeFalse("'abc'.endsWith('b')"); 0050 shouldBeFalse("'abc'.endsWith('x')"); 0051 shouldBeFalse("'abc'.endsWith('abcd')"); 0052 0053 // with start position 0054 shouldBeTrue("'abc'.endsWith('c', 3)"); 0055 shouldBeTrue("'abc'.endsWith('c', 4)"); 0056 shouldBeTrue("'abc'.endsWith('b', 2)"); 0057 shouldBeFalse("'abc'.endsWith('a', 0)"); 0058 shouldBeFalse("'abc'.endsWith('c', -1)"); 0059 shouldBeFalse("'abc'.endsWith('c', -Infinity)"); 0060 shouldBeTrue("'abc'.endsWith('c', +Infinity)"); 0061 0062 // non-numerical position parameter 0063 shouldBeTrue("'abc'.endsWith('c', undefined)"); 0064 shouldBeFalse("'abc'.endsWith('a', null)"); 0065 shouldBeFalse("'abc'.endsWith('c', null)"); 0066 shouldBeFalse("'abc'.endsWith('a', NaN)"); 0067 shouldBeFalse("'abc'.endsWith('c', NaN)"); 0068 shouldBeTrue("'abc'.endsWith('c', '4')"); 0069 shouldBeFalse("'abc'.endsWith('b', '1')"); 0070 0071 // non-string objects 0072 shouldBeTrue("'123'.endsWith(3)"); 0073 shouldBeFalse("'123'.endsWith(4)"); 0074 var arr = ['a', 'b']; 0075 arr.endsWith = String.prototype.endsWith; 0076 shouldBeTrue("arr.endsWith('b')"); 0077 var obj = { toString: function() { return "xyz"; } }; 0078 obj.endsWith = String.prototype.endsWith; 0079 shouldBeTrue("obj.endsWith('xyz')"); 0080 0081 // invalid 0082 shouldThrow("'abc'.endsWith(/a/)");