|
| 1 | +/** |
| 2 | + * Portions adapted from Test262 at revision 250f204f23a9249ff204be2baec29600faae7b75: |
| 3 | + * - test/annexB/built-ins/String/prototype/substr/length-falsey.js |
| 4 | + * - test/annexB/built-ins/String/prototype/substr/length-negative.js |
| 5 | + * - test/annexB/built-ins/String/prototype/substr/length-positive.js |
| 6 | + * - test/annexB/built-ins/String/prototype/substr/length-undef.js |
| 7 | + * - test/annexB/built-ins/String/prototype/substr/start-negative.js |
| 8 | + * - test/annexB/built-ins/String/prototype/substr/surrogate-pairs.js |
| 9 | + * - test/built-ins/String/prototype/isWellFormed/returns-boolean.js |
| 10 | + * - test/built-ins/String/prototype/toWellFormed/returns-well-formed-string.js |
| 11 | + * - test/built-ins/Date/prototype/toDateString/format.js |
| 12 | + * - test/built-ins/Date/prototype/toDateString/invalid-date.js |
| 13 | + * - test/built-ins/Date/prototype/toDateString/negative-year.js |
| 14 | + * - test/built-ins/Date/prototype/toTimeString/format.js |
| 15 | + * - test/built-ins/Date/prototype/toTimeString/invalid-date.js |
| 16 | + * |
| 17 | + * Copyright (C) 2016, 2017 the V8 project authors. All rights reserved. |
| 18 | + * Copyright (C) 2018 Richard Gibson. All rights reserved. |
| 19 | + * Copyright (C) 2022 Jordan Harband. All rights reserved. |
| 20 | + * Test262 portions are governed by the BSD license in LICENSE.test262. |
| 21 | + * |
| 22 | + * The `typeof String.prototype.method` checks are replaced with `typeof "".method` because |
| 23 | + * CodeMode has no prototype objects. |
| 24 | + */ |
| 25 | +import { describe, expect, test } from "bun:test" |
| 26 | +import { Effect } from "effect" |
| 27 | +import { CodeMode } from "../src/index.js" |
| 28 | + |
| 29 | +const value = async (code: string) => { |
| 30 | + const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} })) |
| 31 | + if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`) |
| 32 | + return result.value |
| 33 | +} |
| 34 | + |
| 35 | +describe("String.prototype.substr Test262 parity", () => { |
| 36 | + test("test/annexB/built-ins/String/prototype/substr/length-falsey.js", async () => { |
| 37 | + expect( |
| 38 | + await value(` |
| 39 | + return [false, NaN, "", null].flatMap((length) => [0, 1, 2, 3].map((start) => "abc".substr(start, length))) |
| 40 | + `), |
| 41 | + ).toEqual(Array(16).fill("")) |
| 42 | + }) |
| 43 | + |
| 44 | + test("test/annexB/built-ins/String/prototype/substr/length-negative.js", async () => { |
| 45 | + expect( |
| 46 | + await value(` |
| 47 | + return [0, 1, 2, 3].flatMap((start) => [-1, -2, -3, -4].map((length) => "abc".substr(start, length))) |
| 48 | + `), |
| 49 | + ).toEqual(Array(16).fill("")) |
| 50 | + }) |
| 51 | + |
| 52 | + test("test/annexB/built-ins/String/prototype/substr/length-positive.js", async () => { |
| 53 | + expect( |
| 54 | + await value(` |
| 55 | + return [0, 1, 2, 3].map((start) => [1, 2, 3, 4].map((length) => "abc".substr(start, length))) |
| 56 | + `), |
| 57 | + ).toEqual([ |
| 58 | + ["a", "ab", "abc", "abc"], |
| 59 | + ["b", "bc", "bc", "bc"], |
| 60 | + ["c", "c", "c", "c"], |
| 61 | + ["", "", "", ""], |
| 62 | + ]) |
| 63 | + }) |
| 64 | + |
| 65 | + test("test/annexB/built-ins/String/prototype/substr/length-undef.js", async () => { |
| 66 | + expect( |
| 67 | + await value(` |
| 68 | + return [ |
| 69 | + "abc".substr(0), "abc".substr(1), "abc".substr(2), "abc".substr(3), |
| 70 | + "abc".substr(0, undefined), "abc".substr(1, undefined), "abc".substr(2, undefined), "abc".substr(3, undefined), |
| 71 | + ] |
| 72 | + `), |
| 73 | + ).toEqual(["abc", "bc", "c", "", "abc", "bc", "c", ""]) |
| 74 | + }) |
| 75 | + |
| 76 | + test("test/annexB/built-ins/String/prototype/substr/start-negative.js", async () => { |
| 77 | + expect( |
| 78 | + await value(` |
| 79 | + return ["abc".substr(-1), "abc".substr(-2), "abc".substr(-3), "abc".substr(-4), "abc".substr(-1.1)] |
| 80 | + `), |
| 81 | + ).toEqual(["c", "bc", "abc", "abc", "c"]) |
| 82 | + }) |
| 83 | + |
| 84 | + test("test/annexB/built-ins/String/prototype/substr/surrogate-pairs.js", async () => { |
| 85 | + expect( |
| 86 | + await value(` |
| 87 | + const pair = "\\ud834\\udf06" |
| 88 | + return [pair.substr(0), pair.substr(1), pair.substr(2), pair.substr(0, 0), pair.substr(0, 1), pair.substr(0, 2)] |
| 89 | + `), |
| 90 | + ).toEqual(["\ud834\udf06", "\udf06", "", "", "\ud834", "\ud834\udf06"]) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe("String well-formedness Test262 parity", () => { |
| 95 | + test("test/built-ins/String/prototype/isWellFormed/returns-boolean.js", async () => { |
| 96 | + expect( |
| 97 | + await value(` |
| 98 | + const leadingPoo = "\\uD83D" |
| 99 | + const trailingPoo = "\\uDCA9" |
| 100 | + const wholePoo = leadingPoo + trailingPoo |
| 101 | + return [ |
| 102 | + typeof "".isWellFormed, |
| 103 | + ("a" + leadingPoo + "c" + leadingPoo + "e").isWellFormed(), |
| 104 | + ("a" + trailingPoo + "c" + trailingPoo + "e").isWellFormed(), |
| 105 | + ("a" + trailingPoo + leadingPoo + "d").isWellFormed(), |
| 106 | + "a💩c".isWellFormed(), |
| 107 | + "a\\uD83D\\uDCA9c".isWellFormed(), |
| 108 | + ("a" + leadingPoo + trailingPoo + "d").isWellFormed(), |
| 109 | + wholePoo.slice(0, 1).isWellFormed(), |
| 110 | + wholePoo.slice(1).isWellFormed(), |
| 111 | + "abc".isWellFormed(), |
| 112 | + "a\\u25A8c".isWellFormed(), |
| 113 | + ] |
| 114 | + `), |
| 115 | + ).toEqual(["function", false, false, false, true, true, true, false, false, true, true]) |
| 116 | + }) |
| 117 | + |
| 118 | + test("test/built-ins/String/prototype/toWellFormed/returns-well-formed-string.js", async () => { |
| 119 | + expect( |
| 120 | + await value(` |
| 121 | + const replacementChar = "\\uFFFD" |
| 122 | + const leadingPoo = "\\uD83D" |
| 123 | + const trailingPoo = "\\uDCA9" |
| 124 | + const wholePoo = leadingPoo + trailingPoo |
| 125 | + return [ |
| 126 | + typeof "".toWellFormed, |
| 127 | + ("a" + leadingPoo + "c" + leadingPoo + "e").toWellFormed() === "a" + replacementChar + "c" + replacementChar + "e", |
| 128 | + ("a" + trailingPoo + "c" + trailingPoo + "e").toWellFormed() === "a" + replacementChar + "c" + replacementChar + "e", |
| 129 | + ("a" + trailingPoo + leadingPoo + "d").toWellFormed() === "a" + replacementChar + replacementChar + "d", |
| 130 | + "a💩c".toWellFormed() === "a💩c", |
| 131 | + "a\\uD83D\\uDCA9c".toWellFormed() === "a\\uD83D\\uDCA9c", |
| 132 | + ("a" + leadingPoo + trailingPoo + "d").toWellFormed() === "a" + wholePoo + "d", |
| 133 | + wholePoo.slice(0, 1).toWellFormed() === replacementChar, |
| 134 | + wholePoo.slice(1).toWellFormed() === replacementChar, |
| 135 | + "abc".toWellFormed() === "abc", |
| 136 | + "a\\u25A8c".toWellFormed() === "a\\u25A8c", |
| 137 | + ] |
| 138 | + `), |
| 139 | + ).toEqual(["function", true, true, true, true, true, true, true, true, true, true]) |
| 140 | + }) |
| 141 | +}) |
| 142 | + |
| 143 | +describe("Date string formatting Test262 parity", () => { |
| 144 | + test("test/built-ins/Date/prototype/toDateString/format.js", async () => { |
| 145 | + expect( |
| 146 | + await value(` |
| 147 | + const dateRegExp = /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) [0-9]{2} [0-9]{4}$/ |
| 148 | + return [dateRegExp.test(new Date(0).toDateString()), dateRegExp.test(new Date("0020-01-01T00:00:00Z").toDateString())] |
| 149 | + `), |
| 150 | + ).toEqual([true, true]) |
| 151 | + }) |
| 152 | + |
| 153 | + test("test/built-ins/Date/prototype/toDateString/invalid-date.js", async () => { |
| 154 | + expect(await value(`return new Date(NaN).toDateString()`)).toBe("Invalid Date") |
| 155 | + }) |
| 156 | + |
| 157 | + test("test/built-ins/Date/prototype/toDateString/negative-year.js", async () => { |
| 158 | + expect( |
| 159 | + await value(` |
| 160 | + return ["-000001", "-000012", "-000123", "-001234", "-012345", "-123456"].map( |
| 161 | + (year) => new Date(year + "-07-01T00:00Z").toDateString().split(" ")[3], |
| 162 | + ) |
| 163 | + `), |
| 164 | + ).toEqual(["-0001", "-0012", "-0123", "-1234", "-12345", "-123456"]) |
| 165 | + }) |
| 166 | + |
| 167 | + test("test/built-ins/Date/prototype/toTimeString/format.js", async () => { |
| 168 | + expect( |
| 169 | + await value(` |
| 170 | + const timeRegExp = /^[0-9]{2}:[0-9]{2}:[0-9]{2} GMT[+-][0-9]{4}( \\(.+\\))?$/ |
| 171 | + return timeRegExp.test(new Date(0).toTimeString()) |
| 172 | + `), |
| 173 | + ).toBe(true) |
| 174 | + }) |
| 175 | + |
| 176 | + test("test/built-ins/Date/prototype/toTimeString/invalid-date.js", async () => { |
| 177 | + expect(await value(`return new Date(NaN).toTimeString()`)).toBe("Invalid Date") |
| 178 | + }) |
| 179 | +}) |
0 commit comments