1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Tests taken from: 6 // https://github.com/mathiasbynens/String.prototype.codePointAt 7 8 assertEquals(String.prototype.codePointAt.length, 1); 9 assertEquals(String.prototype.propertyIsEnumerable("codePointAt"), false); 10 11 // String that starts with a BMP symbol 12 assertEquals("abc\uD834\uDF06def".codePointAt(""), 0x61); 13 assertEquals("abc\uD834\uDF06def".codePointAt("_"), 0x61); 14 assertEquals("abc\uD834\uDF06def".codePointAt(), 0x61); 15 assertEquals("abc\uD834\uDF06def".codePointAt(-Infinity), undefined); 16 assertEquals("abc\uD834\uDF06def".codePointAt(-1), undefined); 17 assertEquals("abc\uD834\uDF06def".codePointAt(-0), 0x61); 18 assertEquals("abc\uD834\uDF06def".codePointAt(0), 0x61); 19 assertEquals("abc\uD834\uDF06def".codePointAt(3), 0x1D306); 20 assertEquals("abc\uD834\uDF06def".codePointAt(4), 0xDF06); 21 assertEquals("abc\uD834\uDF06def".codePointAt(5), 0x64); 22 assertEquals("abc\uD834\uDF06def".codePointAt(42), undefined); 23 assertEquals("abc\uD834\uDF06def".codePointAt(Infinity), undefined); 24 assertEquals("abc\uD834\uDF06def".codePointAt(Infinity), undefined); 25 assertEquals("abc\uD834\uDF06def".codePointAt(NaN), 0x61); 26 assertEquals("abc\uD834\uDF06def".codePointAt(false), 0x61); 27 assertEquals("abc\uD834\uDF06def".codePointAt(null), 0x61); 28 assertEquals("abc\uD834\uDF06def".codePointAt(undefined), 0x61); 29 30 // String that starts with an astral symbol 31 assertEquals("\uD834\uDF06def".codePointAt(""), 0x1D306); 32 assertEquals("\uD834\uDF06def".codePointAt("1"), 0xDF06); 33 assertEquals("\uD834\uDF06def".codePointAt("_"), 0x1D306); 34 assertEquals("\uD834\uDF06def".codePointAt(), 0x1D306); 35 assertEquals("\uD834\uDF06def".codePointAt(-1), undefined); 36 assertEquals("\uD834\uDF06def".codePointAt(-0), 0x1D306); 37 assertEquals("\uD834\uDF06def".codePointAt(0), 0x1D306); 38 assertEquals("\uD834\uDF06def".codePointAt(1), 0xDF06); 39 assertEquals("\uD834\uDF06def".codePointAt(42), undefined); 40 assertEquals("\uD834\uDF06def".codePointAt(false), 0x1D306); 41 assertEquals("\uD834\uDF06def".codePointAt(null), 0x1D306); 42 assertEquals("\uD834\uDF06def".codePointAt(undefined), 0x1D306); 43 44 // Lone high surrogates 45 assertEquals("\uD834abc".codePointAt(""), 0xD834); 46 assertEquals("\uD834abc".codePointAt("_"), 0xD834); 47 assertEquals("\uD834abc".codePointAt(), 0xD834); 48 assertEquals("\uD834abc".codePointAt(-1), undefined); 49 assertEquals("\uD834abc".codePointAt(-0), 0xD834); 50 assertEquals("\uD834abc".codePointAt(0), 0xD834); 51 assertEquals("\uD834abc".codePointAt(false), 0xD834); 52 assertEquals("\uD834abc".codePointAt(NaN), 0xD834); 53 assertEquals("\uD834abc".codePointAt(null), 0xD834); 54 assertEquals("\uD834abc".codePointAt(undefined), 0xD834); 55 56 // Lone low surrogates 57 assertEquals("\uDF06abc".codePointAt(""), 0xDF06); 58 assertEquals("\uDF06abc".codePointAt("_"), 0xDF06); 59 assertEquals("\uDF06abc".codePointAt(), 0xDF06); 60 assertEquals("\uDF06abc".codePointAt(-1), undefined); 61 assertEquals("\uDF06abc".codePointAt(-0), 0xDF06); 62 assertEquals("\uDF06abc".codePointAt(0), 0xDF06); 63 assertEquals("\uDF06abc".codePointAt(false), 0xDF06); 64 assertEquals("\uDF06abc".codePointAt(NaN), 0xDF06); 65 assertEquals("\uDF06abc".codePointAt(null), 0xDF06); 66 assertEquals("\uDF06abc".codePointAt(undefined), 0xDF06); 67 68 assertThrows(function() { 69 String.prototype.codePointAt.call(undefined); 70 }, TypeError); 71 assertThrows(function() { 72 String.prototype.codePointAt.call(undefined, 4); 73 }, TypeError); 74 assertThrows(function() { 75 String.prototype.codePointAt.call(null); 76 }, TypeError); 77 assertThrows(function() { 78 String.prototype.codePointAt.call(null, 4); 79 }, TypeError); 80 assertEquals(String.prototype.codePointAt.call(42, 0), 0x34); 81 assertEquals(String.prototype.codePointAt.call(42, 1), 0x32); 82 assertEquals(String.prototype.codePointAt.call({ 83 toString: function() { return "abc"; } 84 }, 2), 0x63); 85 var tmp = 0; 86 assertEquals(String.prototype.codePointAt.call({ 87 toString: function() { ++tmp; return String(tmp); } 88 }, 0), 0x31); 89 assertEquals(tmp, 1); 90