| <!doctype html> |
| <html> |
| <head> |
| <meta charset="utf-8"> |
| <script src="../../http/tests/inspector/resources/inspector-test.js"></script> |
| <script> |
| function test() |
| { |
| let suite = InspectorTest.createSyncSuite("StringUtilities"); |
| |
| suite.addTestCase({ |
| name: "String.format", |
| test() { |
| InspectorTest.expectThat("%f".format(1.23456789) === "1.234568", "float format specifier with no sub-specifier should show 6 decimal digits"); |
| InspectorTest.expectThat("%.0f".format(1.23456789) === "1", "float format specifier with precision 0 should show 0 decimal digits"); |
| InspectorTest.expectThat("%.1f".format(1.23456789) === "1.2", "float format specifier with precision 1 should show 1 decimal digit"); |
| InspectorTest.expectThat("%.2f".format(1.23456789) === "1.23", "float format specifier with precision 2 should show 2 decimal digits"); |
| InspectorTest.expectThat("%.3f".format(1.23456789) === "1.235", "float format specifier with precision 3 should show 3 decimal digits"); |
| InspectorTest.expectThat("%.4f".format(1.23456789) === "1.2346", "float format specifier with precision 4 should show 4 decimal digits"); |
| InspectorTest.expectThat("%.5f".format(1.23456789) === "1.23457", "float format specifier with precision 5 should show 5 decimal digits"); |
| InspectorTest.expectThat("%.6f".format(1.23456789) === "1.234568", "float format specifier with precision 6 should show 6 decimal digits"); |
| InspectorTest.expectThat("%.7f".format(1.23456789) === "1.2345679", "float format specifier with precision 7 should show 7 decimal digits"); |
| InspectorTest.expectThat("%.8f".format(1.23456789) === "1.23456789", "float format specifier with precision 8 should show 8 decimal digits"); |
| InspectorTest.expectThat("%.9f".format(1.23456789) === "1.234567890", "float format specifier with precision 9 should show 9 decimal digits"); |
| InspectorTest.expectThat("%f".format("1.23456789") === "1.234568", "float format specifier with string argument should attempt conversion to float"); |
| InspectorTest.expectThat("%f".format(Infinity) === "\u221E", "float format specifier with Infinity argument should show \"\u221E\""); |
| InspectorTest.expectThat("%f".format(NaN) === "NaN", "float format specifier with NaN argument should show \"NaN\""); |
| |
| InspectorTest.expectThat("%d".format(137.1) === "137", "integer format specifier with float argument should convert to integer"); |
| InspectorTest.expectThat("%d".format("137") === "137", "integer format specifier with string argument should attempt conversion to integer"); |
| InspectorTest.expectThat("%d".format(Infinity) === "NaN", "integer format specifier with Infinity argument should show \"NaN\""); |
| InspectorTest.expectThat("%d".format(NaN) === "NaN", "integer format specifier with NaN argument should show \"NaN\""); |
| |
| InspectorTest.log("Testing string with unknown specifier."); |
| let formatWithUnknownSpecifiers = String.format("%_ %s", ["first", "second"], String.standardFormatters, "", function(a, b) { return a + b; }); |
| InspectorTest.expectThat(formatWithUnknownSpecifiers.formattedResult === "%_ first", "Formatter string with unknown specifier should not replace unknown specifier."); |
| InspectorTest.expectThat(formatWithUnknownSpecifiers.unusedSubstitutions[0] === "second", "Formatter string with unknown specifier should have an unused substitution value."); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.extendedLocaleCompare", |
| test() { |
| InspectorTest.expectEqual("1".extendedLocaleCompare("2"), -1, `"1" < "2"`); |
| InspectorTest.expectEqual("2".extendedLocaleCompare("1"), 1, `"2" > "1"`); |
| InspectorTest.expectEqual("2".extendedLocaleCompare("10"), -1, `"2" < "10"`); |
| InspectorTest.expectEqual("10".extendedLocaleCompare("2"), 1, `"10" > "2"`); |
| InspectorTest.expectEqual("1".extendedLocaleCompare("10"), -1, `"1" < "10"`); |
| InspectorTest.expectEqual("10".extendedLocaleCompare("1"), 1, `"10" > "1"`); |
| |
| InspectorTest.expectEqual("a1".extendedLocaleCompare("a2"), -1, `"a1" < "a2"`); |
| InspectorTest.expectEqual("a2".extendedLocaleCompare("a1"), 1, `"a2" > "a1"`); |
| InspectorTest.expectEqual("a2".extendedLocaleCompare("a10"), -1, `"a2" < "a10"`); |
| InspectorTest.expectEqual("a10".extendedLocaleCompare("a2"), 1, `"a10" > "a2"`); |
| InspectorTest.expectEqual("a1".extendedLocaleCompare("a10"), -1, `"a1" < "a10"`); |
| InspectorTest.expectEqual("a10".extendedLocaleCompare("a1"), 1, `"a10" > "a1"`); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.get lineCount", |
| test() { |
| InspectorTest.expectEqual("1\n2\n3".lineCount, 3, "A string with two line breaks should have three lines."); |
| InspectorTest.expectEqual("1\n\n3".lineCount, 3, "A string with two consecutive line breaks should have three lines."); |
| InspectorTest.expectEqual("1\n".lineCount, 2, "A string with a traling line breaks should have two lines."); |
| InspectorTest.expectEqual("".lineCount, 1, "An empty string is one line."); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.get lastLine", |
| test() { |
| InspectorTest.expectEqual("single line".lastLine, "single line", "Last line of one line string is the same string."); |
| InspectorTest.expectEqual("1\n2\n3".lastLine, "3", "Last line of a three line string should be the third line."); |
| InspectorTest.expectEqual("1\n".lastLine, "", "Last line of a string with a traling line break should be empty."); |
| InspectorTest.expectEqual("".lastLine, "", "Last line of an empty string is the same empty string."); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.truncateStart", |
| test() { |
| const ellipsis = "\u2026"; |
| InspectorTest.expectEqual("abcdef".truncateStart(6), "abcdef", "String stays the same."); |
| InspectorTest.expectEqual("abcdef".truncateStart(5), ellipsis + "cdef", "Ellipsis is inserted before the third character."); |
| InspectorTest.expectEqual("abcdef".truncateStart(4), ellipsis + "def", "Ellipsis is inserted before the fourth character."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.truncateMiddle", |
| test() { |
| const ellipsis = "\u2026"; |
| InspectorTest.expectEqual("abcdef".truncateMiddle(6), "abcdef", "String stays the same."); |
| InspectorTest.expectEqual("abcdef".truncateMiddle(5), `ab${ellipsis}ef`, "Ellipsis is inserted in the middle."); |
| InspectorTest.expectEqual("abcdef".truncateMiddle(4), `ab${ellipsis}f`, "Ellipsis is inserted after the second character."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.truncateEnd", |
| test() { |
| const ellipsis = "\u2026"; |
| InspectorTest.expectEqual("abcdef".truncateEnd(6), "abcdef", "String stays the same."); |
| InspectorTest.expectEqual("abcdef".truncateEnd(5), "abcd" + ellipsis, "Ellipsis is inserted after the fourth character."); |
| InspectorTest.expectEqual("abcdef".truncateEnd(4), "abc" + ellipsis, "Ellipsis is inserted after the third character."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.truncate", |
| test() { |
| const ellipsis = "\u2026"; |
| InspectorTest.expectEqual("abcdef".truncate(6), "abcdef", "String stays the same."); |
| InspectorTest.expectEqual("abcdef".truncate(5), "abcde" + ellipsis, "String without whitespace is truncated at maxLength."); |
| InspectorTest.expectEqual("foo bar baz".truncate(8), "foo ba" + ellipsis, "String is truncated at maxLength when no suitable word boundary."); |
| InspectorTest.expectEqual("foo bar baz".truncate(10), "foo ba" + ellipsis, "String is truncated before last whitespace when past halfway point."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.collapseWhitespace", |
| test() { |
| InspectorTest.expectEqual("foo bar".collapseWhitespace(), "foo bar", "Multiple spaces are collapsed to one."); |
| InspectorTest.expectEqual("foo\t\tbar".collapseWhitespace(), "foo bar", "Multiple tabs are collapsed to one space."); |
| InspectorTest.expectEqual("foo\n\nbar".collapseWhitespace(), "foo bar", "Multiple newlines are collapsed to one space."); |
| InspectorTest.expectEqual("foo bar\t\nbaz".collapseWhitespace(), "foo bar baz", "Mixed whitespace is collapsed to single spaces."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.removeWhitespace", |
| test() { |
| InspectorTest.expectEqual("foo bar".removeWhitespace(), "foobar", "Spaces are removed."); |
| InspectorTest.expectEqual("foo\tbar".removeWhitespace(), "foobar", "Tabs are removed."); |
| InspectorTest.expectEqual("foo\nbar".removeWhitespace(), "foobar", "Newlines are removed."); |
| InspectorTest.expectEqual("foo bar\t\nbaz".removeWhitespace(), "foobarbaz", "All whitespace is removed."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.capitalize", |
| test() { |
| InspectorTest.expectEqual("foo".capitalize(), "Foo", "First character is capitalized."); |
| InspectorTest.expectEqual("foo bar".capitalize(), "Foo bar", "Only first character is capitalized."); |
| InspectorTest.expectEqual("FOO".capitalize(), "FOO", "Already capitalized string stays the same."); |
| InspectorTest.expectEqual("".capitalize(), "", "Empty string stays empty."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.escapeCharacters", |
| test() { |
| InspectorTest.expectEqual("abcdef".escapeCharacters(), "abcdef", "String stays the same with no escape characters."); |
| InspectorTest.expectEqual("abcdef".escapeCharacters(""), "abcdef", "String stays the same with empty escape characters."); |
| InspectorTest.expectEqual("abcdef".escapeCharacters("g"), "abcdef", "String stays the same with no matching escape characters."); |
| InspectorTest.expectEqual("abcdef".escapeCharacters("c"), "ab\\cdef", "The letter 'c' is escaped."); |
| InspectorTest.expectEqual("abcdef".escapeCharacters("ce"), "ab\\cd\\ef", "The letter 'c' and 'e' are escaped."); |
| InspectorTest.expectEqual("abcdef".escapeCharacters("cee"), "ab\\cd\\ef", "The letter 'c' and 'e' are escaped."); |
| InspectorTest.expectEqual("abcdef".escapeCharacters("ced"), "ab\\c\\d\\ef", "The letter 'c', 'd', and 'e' are escaped."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.escapeForRegExp", |
| test() { |
| InspectorTest.expectEqual("abc".escapeForRegExp(), "abc", "String with no special characters stays the same."); |
| InspectorTest.expectEqual("a.b".escapeForRegExp(), "a\\.b", "Dot is escaped."); |
| InspectorTest.expectEqual("a*b".escapeForRegExp(), "a\\*b", "Asterisk is escaped."); |
| InspectorTest.expectEqual("a+b".escapeForRegExp(), "a\\+b", "Plus is escaped."); |
| InspectorTest.expectEqual("a?b".escapeForRegExp(), "a\\?b", "Question mark is escaped."); |
| InspectorTest.expectEqual("[a]".escapeForRegExp(), "\\[a\\]", "Square brackets are escaped."); |
| InspectorTest.expectEqual("(a)".escapeForRegExp(), "\\(a\\)", "Parentheses are escaped."); |
| InspectorTest.expectEqual("a|b".escapeForRegExp(), "a\\|b", "Pipe is escaped."); |
| InspectorTest.expectEqual("^a$".escapeForRegExp(), "\\^a\\$", "Caret and dollar are escaped."); |
| InspectorTest.expectEqual("a\\b".escapeForRegExp(), "a\\\\b", "Backslash is escaped."); |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.isLowerCase", |
| test() { |
| InspectorTest.expectTrue("a".isLowerCase(), "String with single lowercase character should be lowercase."); |
| InspectorTest.expectTrue("abc".isLowerCase(), "String with multiple lowercase characters should be lowercase."); |
| InspectorTest.expectFalse("A".isLowerCase(), "String with single uppercase character should not be lowercase."); |
| InspectorTest.expectFalse("aBc".isLowerCase(), "String with mixed case characters should not be lowercase."); |
| InspectorTest.expectFalse("".isLowerCase(), "Empty string should not be lowercase."); |
| InspectorTest.expectFalse(".".isLowerCase(), "String with non-alpha character should not be lowercase."); |
| InspectorTest.expectFalse("1".isLowerCase(), "String with numeric character should not be lowercase."); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.isUpperCase", |
| test() { |
| InspectorTest.expectTrue("A".isUpperCase(), "String with single uppercase character should be uppercase."); |
| InspectorTest.expectTrue("ABC".isUpperCase(), "String with multiple uppercase characters should be uppercase."); |
| InspectorTest.expectFalse("a".isUpperCase(), "String with single lowercase character should not be uppercase."); |
| InspectorTest.expectFalse("AbC".isUpperCase(), "String with mixed case characters should not be uppercase."); |
| InspectorTest.expectFalse("".isUpperCase(), "Empty string should not be uppercase."); |
| InspectorTest.expectFalse(".".isUpperCase(), "String with non-alpha character should not be uppercase."); |
| InspectorTest.expectFalse("1".isUpperCase(), "String with numeric character should not be uppercase."); |
| |
| return true; |
| } |
| }); |
| |
| suite.addTestCase({ |
| name: "String.prototype.StrictModeReturnTypes", |
| description: "Test that String.prototype methods using strict mode return primitive strings, not String objects.", |
| test() { |
| // Without strict mode, `this` inside the method would be boxed into a String object, |
| // causing the return value to be a String object instead of a primitive string. |
| // This test verifies that strict mode is properly used, ensuring primitive string returns. |
| |
| InspectorTest.expectEqual(typeof "abc".collapseWhitespace(), "string", "collapseWhitespace should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".removeWhitespace(), "string", "removeWhitespace should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".escapeCharacters(""), "string", "escapeCharacters should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".escapeForRegExp(), "string", "escapeForRegExp should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".capitalize(), "string", "capitalize should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".insertWordBreakCharacters(), "string", "insertWordBreakCharacters should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".removeWordBreakCharacters(), "string", "removeWordBreakCharacters should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".truncateStart(10), "string", "truncateStart should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".truncateMiddle(10), "string", "truncateMiddle should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".truncateEnd(10), "string", "truncateEnd should return a primitive string."); |
| InspectorTest.expectEqual(typeof "abc".truncate(10), "string", "truncate should return a primitive string."); |
| |
| // Verify equality comparisons work correctly (would fail if String objects were returned). |
| InspectorTest.expectEqual("abc".collapseWhitespace(), "abc", "collapseWhitespace result should be strictly equal to primitive string."); |
| InspectorTest.expectEqual("abc".removeWhitespace(), "abc", "removeWhitespace result should be strictly equal to primitive string."); |
| InspectorTest.expectEqual("abc".capitalize(), "Abc", "capitalize result should be strictly equal to primitive string."); |
| InspectorTest.expectEqual("abc".truncateStart(10), "abc", "truncateStart result should be strictly equal to primitive string."); |
| InspectorTest.expectEqual("abc".truncateMiddle(10), "abc", "truncateMiddle result should be strictly equal to primitive string."); |
| InspectorTest.expectEqual("abc".truncateEnd(10), "abc", "truncateEnd result should be strictly equal to primitive string."); |
| InspectorTest.expectEqual("abc".truncate(10), "abc", "truncate result should be strictly equal to primitive string."); |
| |
| return true; |
| } |
| }); |
| |
| suite.runTestCasesAndFinish(); |
| } |
| </script> |
| </head> |
| <body onLoad="runTest()"> |
| </body> |
| </html> |