diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /js/src/tests/non262/template-strings/noSubst.js | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | js/src/tests/non262/template-strings/noSubst.js | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/js/src/tests/non262/template-strings/noSubst.js b/js/src/tests/non262/template-strings/noSubst.js new file mode 100644 index 0000000000..730275fd99 --- /dev/null +++ b/js/src/tests/non262/template-strings/noSubst.js @@ -0,0 +1,148 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// This test case is weird in the sense the actual work happens at the eval +// at the end. If template strings are not enabled, the test cases would throw +// a syntax error and we'd have failure reported. To avoid that, the entire +// test case is commented out and is part of a function. We use toString to +// get the string version, obtain the actual lines to run, and then use eval to +// do the actual evaluation. + +function syntaxError (script) { + try { + Function(script); + } catch (e) { + if (e.name === "SyntaxError") { + return; + } + } + throw "Expected syntax error: " + script; +} + +// TEST BEGIN + +// unterminated quasi literal +syntaxError("`"); +syntaxError("`$"); +syntaxError("`${"); +syntaxError("`${}"); +syntaxError("`${1}"); +syntaxError("`${1 + 2}"); + +// almost template substitutions +assertEq("$", `$`); +assertEq("$}", `$}`); +assertEq("}", `}`); +assertEq("{", `{`); + + +// character escape sequence (single escape character) +assertEq("\'", `\'`); +assertEq("\"", `\"`); +assertEq("\\", `\\`); +assertEq("\b", `\b`); +assertEq("\f", `\f`); +assertEq("\n", `\n`); +assertEq("\r", `\r`); +assertEq("\t", `\t`); +assertEq("\v", `\v`); +assertEq("\r\n", `\r\n`); + + +assertEq("\0", eval("`\\" + String.fromCharCode(0) + "`")); +assertEq("$", `\$`); +assertEq(".", `\.`); +assertEq("A", `\A`); +assertEq("a", `\a`); + + +// digit escape sequence +assertEq("\0", `\0`); +syntaxError("`\\1`"); +syntaxError("`\\2`"); +syntaxError("`\\3`"); +syntaxError("`\\4`"); +syntaxError("`\\5`"); +syntaxError("`\\6`"); +syntaxError("`\\7`"); +syntaxError("`\\01`"); +syntaxError("`\\001`"); +syntaxError("`\\00`"); + +// hex escape sequence +syntaxError("`\\x`"); +syntaxError("`\\x0`"); +syntaxError("`\\x0Z`"); +syntaxError("`\\xZ`"); + +assertEq("\0", `\x00`); +assertEq("$", `\x24`); +assertEq(".", `\x2E`); +assertEq("A", `\x41`); +assertEq("a", `\x61`); +assertEq("AB", `\x41B`); +assertEq(String.fromCharCode(0xFF), `\xFF`); + + +// unicode escape sequence + +assertEq("\0", `\u0000`); +assertEq("$", `\u0024`); +assertEq(".", `\u002E`); +assertEq("A", `\u0041`); +assertEq("a", `\u0061`); +assertEq("AB", `\u0041B`); +assertEq(String.fromCharCode(0xFFFF), `\uFFFF`); + + +// line continuation +assertEq("", eval("`\\\n`")); +assertEq("", eval("`\\\r`")); +assertEq("", eval("`\\\u2028`")); +assertEq("", eval("`\\\u2029`")); +assertEq("\u2028", eval("`\u2028`")); +assertEq("\u2029", eval("`\u2029`")); + +assertEq("a\nb", eval("`a\rb`")) +assertEq("a\nb", eval("`a\r\nb`")) +assertEq("a\n\nb", eval("`a\r\rb`")) + + +// source character +for (var i = 0; i < 0xFF; ++i) { + var c = String.fromCharCode(i); + if (c == "`" || c == "\\" || c == "\r") continue; + assertEq(c, eval("`" + c + "`")); +} + +assertEq("", ``); +assertEq("`", `\``); +assertEq("$", `$`); +assertEq("$$", `$$`); +assertEq("$$}", `$$}`); + +// multi-line +assertEq(`hey +there`, "hey\nthere"); + +// differences between strings and template strings +syntaxError("var obj = { `illegal`: 1}"); + +// test for JSON.parse +assertThrowsInstanceOf(() => JSON.parse('[1, `false`]'), SyntaxError); + +syntaxError('({get `name`() { return 10; }});'); + +// test for "use strict" directive +assertEq(5, Function("`use strict`; return 05;")()); +var func = function f() { + `ignored string`; + "use strict"; + return 06; +} +assertEq(6, func()); +syntaxError("\"use strict\"; return 06;"); + + +reportCompare(0, 0, "ok"); |