UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

843 lines (695 loc) 31 kB
<!DOCTYPE html> <html> <!-- Copyright 2006 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by an Apache 2.0 License. See the COPYING file for details. --> <head> <title>Closure Unit Tests - goog.string</title> <script src="../base.js"></script> <script> goog.require('goog.object'); goog.require('goog.string'); goog.require('goog.testing.jsunit'); </script> </head> <body> <script> //=== tests for goog.string.collapseWhitespace === function testCollapseWhiteSpace() { var f = goog.string.collapseWhitespace; assertEquals('Leading spaces not stripped', f(' abc'), 'abc'); assertEquals('Trailing spaces not stripped', f('abc '), 'abc'); assertEquals('Wrapping spaces not stripped', f(' abc '), 'abc'); assertEquals('All white space chars not stripped', f('\xa0\n\t abc\xa0\n\t '), 'abc'); assertEquals('Spaces not collapsed', f('a b c'), 'a b c'); assertEquals('Tabs not collapsed', f('a\t\t\tb\tc'), 'a b c'); assertEquals('All check failed', f(' \ta \t \t\tb\t\n\xa0 c \t\n'), 'a b c'); } //=== tests for goog.string.isEmpty === function testIsEmpty() { assert('1. Should be empty', goog.string.isEmpty('')); assert('2. Should be empty', goog.string.isEmpty(' ')); assert('3. Should be empty', goog.string.isEmpty(' ')); assert('4. Should be empty', goog.string.isEmpty(' \t\t\n\xa0 ')); assert('1. Should not be empty', !goog.string.isEmpty(' abc \t\xa0')); assert('2. Should not be empty', !goog.string.isEmpty(' a b c \t')); assert('3. Should not be empty', !goog.string.isEmpty(';')); var a; assert('Undefined is not empty', !goog.string.isEmpty(a)); assert('Null is not empty', !goog.string.isEmpty(null)); assert('A random object is not empty', !goog.string.isEmpty({a:1,b:2})); } //=== tests for goog.string.isAlpha === function testIsAlpha() { assertTrue('"a" should be alpha', goog.string.isAlpha('a')); assertTrue('"n" should be alpha', goog.string.isAlpha('n')); assertTrue('"z" should be alpha', goog.string.isAlpha('z')); assertTrue('"A" should be alpha', goog.string.isAlpha('A')); assertTrue('"N" should be alpha', goog.string.isAlpha('N')); assertTrue('"Z" should be alpha', goog.string.isAlpha('Z')); assertTrue('"aa" should be alpha', goog.string.isAlpha('aa')); assertTrue('null is alpha', goog.string.isAlpha(null)); assertTrue('undefined is alpha', goog.string.isAlpha(undefined)); assertFalse('"aa!" is not alpha', goog.string.isAlpha('aa!s')); assertFalse('"!" is not alpha', goog.string.isAlpha('!')); assertFalse('"0" is not alpha', goog.string.isAlpha('0')); assertFalse('"5" is not alpha', goog.string.isAlpha('5')); } //=== tests for goog.string.isNumeric === function testIsNumeric() { assertTrue('"8" is a numeric string', goog.string.isNumeric('8')); assertTrue('"5" is a numeric string', goog.string.isNumeric('5')); assertTrue('"34" is a numeric string', goog.string.isNumeric('34')); assertTrue('34 is a number', goog.string.isNumeric(34)); assertFalse('"3.14" has a period', goog.string.isNumeric('3.14')); assertFalse('"A" is a letter', goog.string.isNumeric('A')); assertFalse('"!" is punctuation', goog.string.isNumeric('!')); assertFalse('null is not numeric', goog.string.isNumeric(null)); assertFalse('undefined is not numeric', goog.string.isNumeric(undefined)); } //=== tests for tests for goog.string.isAlphaNumeric === function testIsAlphaNumeric() { assertTrue('"ABCabc" should be alphanumeric', goog.string.isAlphaNumeric('ABCabc')); assertTrue('"123" should be alphanumeric', goog.string.isAlphaNumeric('123')); assertTrue('"ABCabc123" should be alphanumeric', goog.string.isAlphaNumeric('ABCabc123')); assertTrue('null is alphanumeric', goog.string.isAlphaNumeric(null)); assertTrue('undefined is alphanumeric', goog.string.isAlphaNumeric(undefined)); assertFalse('"123!" should not be alphanumeric', goog.string.isAlphaNumeric('123!')); assertFalse('" " should not be alphanumeric', goog.string.isAlphaNumeric(' ')); } //== tests for goog.string.isBreakingWhitespace === function testIsBreakingWhitespace() { assertTrue('" " is breaking', goog.string.isBreakingWhitespace(' ')); assertTrue('"\\n" is breaking', goog.string.isBreakingWhitespace('\n')); assertTrue('"\\t" is breaking', goog.string.isBreakingWhitespace('\t')); assertTrue('"\\r" is breaking', goog.string.isBreakingWhitespace('\r')); assertTrue('"\\r\\n\\t " is breaking', goog.string.isBreakingWhitespace('\r\n\t ')); assertFalse('nbsp is non-breaking', goog.string.isBreakingWhitespace('\xa0')); assertFalse('"a" is non-breaking', goog.string.isBreakingWhitespace('a')); assertFalse('"a\\r" is non-breaking', goog.string.isBreakingWhitespace('a\r')); } //=== tests for goog.string.isSpace === function testIsSpace() { assertTrue('" " is a space', goog.string.isSpace(' ')); assertFalse('"\\n" is not a space', goog.string.isSpace('\n')); assertFalse('"\\t" is not a space', goog.string.isSpace('\t')); assertFalse('" " is not a space, it\'s two spaces', goog.string.isSpace(' ')); assertFalse('"a" is not a space', goog.string.isSpace('a')); assertFalse('"3" is not a space', goog.string.isSpace('3')); assertFalse('"#" is not a space', goog.string.isSpace('#')); assertFalse('null is not a space', goog.string.isSpace(null)); assertFalse('nbsp is not a space', goog.string.isSpace('\xa0')); } // === tests for goog.string.stripNewlines === function testStripNewLines() { assertEquals('Should replace new lines with spaces', goog.string.stripNewlines('some\nlines\rthat\r\nare\n\nsplit'), 'some lines that are split'); } // === tests for goog.string.canonicalizeNewlines === function testCanonicalizeNewlines() { assertEquals('Should replace all types of new line with \\n', goog.string.canonicalizeNewlines( 'some\nlines\rthat\r\nare\n\nsplit'), 'some\nlines\nthat\nare\n\nsplit'); } // === tests for goog.string.normalizeWhitespace === function testWhitespace() { assertEquals('All whitespace chars should be replaced with a normal space', goog.string.normalizeWhitespace('\xa0 \n\t \xa0 \n\t'), ' '); } // === tests for goog.string.normalizeSpaces === function testNormalizeSpaces() { assertEquals('All whitespace chars should be replaced with a normal space', goog.string.normalizeSpaces('\xa0 \t \xa0 \t'), ' '); } /// === tests for goog.string.trim === function testTrim() { assertEquals('Should be the same', goog.string.trim('nothing 2 trim'), 'nothing 2 trim'); assertEquals('Remove spaces', goog.string.trim(' hello goodbye '), 'hello goodbye'); assertEquals('Trim other stuff', goog.string.trim('\n\r\xa0 hi \r\n\xa0'), 'hi'); } /// === tests for goog.string.trimLeft === function testTrimLeft() { var f = goog.string.trimLeft; assertEquals('Should be the same', f('nothing to trim'), 'nothing to trim'); assertEquals('Remove spaces', f(' hello goodbye '), 'hello goodbye '); assertEquals('Trim other stuff', f('\xa0\n\r hi \r\n\xa0'), 'hi \r\n\xa0'); } /// === tests for goog.string.trimRight === function testTrimRight() { var f = goog.string.trimRight; assertEquals('Should be the same', f('nothing to trim'), 'nothing to trim'); assertEquals('Remove spaces', f(' hello goodbye '), ' hello goodbye'); assertEquals('Trim other stuff', f('\n\r\xa0 hi \r\n\xa0'), '\n\r\xa0 hi'); } // === tests for goog.string.startsWith === function testStartsWith() { assertTrue('Should start with \'\'', goog.string.startsWith('abcd', '')); assertTrue('Should start with \'ab\'', goog.string.startsWith('abcd', 'ab')); assertTrue('Should start with \'abcd\'', goog.string.startsWith('abcd', 'abcd')); assertFalse('Should not start with \'bcd\'', goog.string.startsWith('abcd', 'bcd')); } function testEndsWith() { assertTrue('Should end with \'\'', goog.string.endsWith('abcd', '')); assertTrue('Should end with \'ab\'', goog.string.endsWith('abcd', 'cd')); assertTrue('Should end with \'abcd\'', goog.string.endsWith('abcd', 'abcd')); assertFalse('Should not end \'abc\'', goog.string.endsWith('abcd', 'abc')); assertFalse('Should not end \'abcde\'', goog.string.endsWith('abcd', 'abcde')); } // === tests for goog.string.caseInsensitiveStartsWith === function testCaseInsensitiveStartsWith() { assertTrue('Should start with \'\'', goog.string.caseInsensitiveStartsWith('abcd', '')); assertTrue('Should start with \'ab\'', goog.string.caseInsensitiveStartsWith('abcd', 'Ab')); assertTrue('Should start with \'abcd\'', goog.string.caseInsensitiveStartsWith('AbCd', 'abCd')); assertFalse('Should not start with \'bcd\'', goog.string.caseInsensitiveStartsWith('ABCD', 'bcd')); } // === tests for goog.string.caseInsensitiveEndsWith === function testCaseInsensitiveEndsWith() { assertTrue('Should end with \'\'', goog.string.caseInsensitiveEndsWith('abcd', '')); assertTrue('Should end with \'cd\'', goog.string.caseInsensitiveEndsWith('abCD', 'cd')); assertTrue('Should end with \'abcd\'', goog.string.caseInsensitiveEndsWith('abcd', 'abCd')); assertFalse('Should not end \'abc\'', goog.string.caseInsensitiveEndsWith('aBCd', 'ABc')); assertFalse('Should not end \'abcde\'', goog.string.caseInsensitiveEndsWith('ABCD', 'abcde')); } // === tests for goog.string.subs === function testSubs() { assertEquals('Should be the same', goog.string.subs('nothing to subs'), 'nothing to subs'); assertEquals('Should be the same', goog.string.subs('%s', '1'), '1'); assertEquals('Should be the same', goog.string.subs('%s%s%s', '1', 2, true), '12true'); function f() { assertTrue('This should not be called', false); } f.toString = function () { return 'f'; }; assertEquals('Should not call function', goog.string.subs('%s', f), 'f'); // If the string that is to be substituted in contains $& then it will be // usually be replaced with %s, we need to check goog.string.subs, handles // this case. assertEquals('$& should not be substituted with %s', 'Foo Bar $&', goog.string.subs('Foo %s', 'Bar $&')); assertEquals('$$ should not be substituted', '_$$_', goog.string.subs('%s', '_$$_')); assertEquals('$` should not be substituted', '_$`_', goog.string.subs('%s', '_$`_')); assertEquals('$\' should not be substituted', '_$\'_', goog.string.subs('%s', '_$\'_')); for (var i = 0; i < 99; i+=9) { assertEquals('$' + i + ' should not be substituted', '_$' + i + '_', goog.string.subs('%s', '_$' + i + '_')); } } // === tests for goog.string.caseInsensitiveCompare === function testCaseInsensitiveCompare() { var f = goog.string.caseInsensitiveCompare; assert('"ABC" should be less than "def"', f('ABC', 'def') == -1); assert('"abc" should be less than "DEF"', f('abc', 'DEF') == -1); assert('"XYZ" should equal "xyz"', f('XYZ', 'xyz') == 0); assert('"XYZ" should be greater than "UVW"', f('xyz', 'UVW') == 1); assert('"XYZ" should be greater than "uvw"', f('XYZ', 'uvw') == 1); } // === tests for goog.string.numerateCompare === function testNumerateCompare() { var f = goog.string.numerateCompare; // Each comparison in this list is tested to assure that t[0] < t[1], // t[1] > t[0], and identity tests t[0] == t[0] and t[1] == t[1]. var comparisons = [ ['', '0'], ['2', '10'], ['05', '9'], ['3.14', '3.2'], ['sub', 'substring'], ['Photo 7', 'photo 8'], // Case insensitive for most sorts. ['Mango', 'mango'], // Case sensitive if strings are otherwise identical. ['album 2 photo 20', 'album 10 photo 20'], ['album 7 photo 20', 'album 7 photo 100']]; for (var i = 0; i < comparisons.length; i++) { var t = comparisons[i]; assert(t[0] + ' should be less than ' + t[1], f(t[0], t[1]) < 0); assert(t[1] + ' should be greater than ' + t[0], f(t[1], t[0]) > 0); assert(t[0] + ' should be equal to ' + t[0], f(t[0], t[0]) == 0); assert(t[1] + ' should be equal to ' + t[1], f(t[1], t[1]) == 0); } } // === tests for goog.string.urlEncode && .urlDecode === // NOTE: When test was written it was simply an alias for the built in // 'encodeURICompoent', therefore this test is simply used to make sure that in // the future it doesn't get broken. function testUrlEncodeAndDecode() { var input = '<p>"hello there," she said, "what is going on here?</p>'; var output = '%3Cp%3E%22hello%20there%2C%22%20she%20said%2C%20%22what%20is' + '%20going%20on%20here%3F%3C%2Fp%3E'; assertEquals('urlEncode vs encodeURIComponent', encodeURIComponent(input), goog.string.urlEncode(input)); assertEquals('urlEncode vs model', goog.string.urlEncode(input), output); assertEquals('urlDecode vs model', goog.string.urlDecode(output), input); assertEquals('urlDecode vs urlEncode', goog.string.urlDecode(goog.string.urlEncode(input)), input); assertEquals('urlDecode with +s instead of %20s', goog.string.urlDecode(output.replace(/%20/g, '+')), input); } function testEncodeOptimizationRegex() { assertEquals(0, "foo".search(goog.string.encodeUriRegExp_)); assertEquals(0, "baby1224".search(goog.string.encodeUriRegExp_)); assertEquals(0, "_-*!~".search(goog.string.encodeUriRegExp_)); assertEquals(-1, "%".search(goog.string.encodeUriRegExp_)); assertEquals(-1, "@".search(goog.string.encodeUriRegExp_)); assertEquals(-1, "/".search(goog.string.encodeUriRegExp_)); } // === tests for goog.string.newLineToBr === function testNewLineToBr() { var str = 'some\nlines\rthat\r\nare\n\nsplit'; var html = 'some<br>lines<br>that<br>are<br><br>split'; var xhtml = 'some<br />lines<br />that<br />are<br /><br />split'; assertEquals('Should be html', goog.string.newLineToBr(str), html); assertEquals('Should be html', goog.string.newLineToBr(str, false), html); assertEquals('Should be xhtml', goog.string.newLineToBr(str, true), xhtml); } // === tests for goog.string.htmlEscape and .unescapeEntities === function testHtmlEscapeAndUnescapeEntities() { var text = '"x1 < x2 && y2 > y1"'; var html = '&quot;x1 &lt; x2 &amp;&amp; y2 &gt; y1&quot;'; assertEquals('Testing htmlEscape', goog.string.htmlEscape(text), html); assertEquals('Testing htmlEscape', goog.string.htmlEscape(text, false), html); assertEquals('Testing htmlEscape', goog.string.htmlEscape(text, true), html); assertEquals('Testing unescapeEntities', goog.string.unescapeEntities(html), text); assertEquals('escape -> unescape', goog.string.unescapeEntities(goog.string.htmlEscape(text)), text); assertEquals('unescape -> escape', goog.string.htmlEscape(goog.string.unescapeEntities(html)), html); } function testHtmlEscapeAndUnescapeEntitiesUsingDom() { var text = '"x1 < x2 && y2 > y1"'; var html = '&quot;x1 &lt; x2 &amp;&amp; y2 &gt; y1&quot;'; assertEquals('Testing unescapeEntities', goog.string.unescapeEntitiesUsingDom_(html), text); assertEquals('escape -> unescape', goog.string.unescapeEntitiesUsingDom_( goog.string.htmlEscape(text)), text); assertEquals('unescape -> escape', goog.string.htmlEscape( goog.string.unescapeEntitiesUsingDom_(html)), html); } function testHtmlEscapeAndUnescapePureXmlEntities_() { var text = '"x1 < x2 && y2 > y1"'; var html = '&quot;x1 &lt; x2 &amp;&amp; y2 &gt; y1&quot;'; assertEquals('Testing unescapePureXmlEntities_', goog.string.unescapePureXmlEntities_(html), text); assertEquals('escape -> unescape', goog.string.unescapePureXmlEntities_( goog.string.htmlEscape(text)), text); assertEquals('unescape -> escape', goog.string.htmlEscape( goog.string.unescapePureXmlEntities_(html)), html); } var globalXssVar = 0; function testXssUnescapeEntities() { // This tests that we don't have any XSS exploits in unescapeEntities var test = '&amp;<script defer>globalXssVar=1;</' + 'script>'; var expected = '&<script defer>globalXssVar=1;</' + 'script>'; assertEquals('Testing unescapeEntities', expected, goog.string.unescapeEntities(test)); assertEquals('unescapeEntities is vulnarable to XSS', 0, globalXssVar); test = '&amp;<script>globalXssVar=1;</' + 'script>'; expected = '&<script>globalXssVar=1;</' + 'script>'; assertEquals('Testing unescapeEntities', expected, goog.string.unescapeEntities(test)); assertEquals('unescapeEntities is vulnarable to XSS', 0, globalXssVar); } function testXssUnescapeEntitiesUsingDom() { // unescapeEntitiesUsingDom_ does not handle XSS issues and } function testXssUnescapePureXmlEntities() { // This tests that we don't have any XSS exploits in unescapeEntities var test = '&amp;<script defer>globalXssVar=1;</' + 'script>'; var expected = '&<script defer>globalXssVar=1;</' + 'script>'; assertEquals('Testing unescapePureXmlEntities_', expected, goog.string.unescapePureXmlEntities_(test)); assertEquals('unescapePureXmlEntities_ is vulnarable to XSS', 0, globalXssVar); test = '&amp;<script>globalXssVar=1;</' + 'script>'; expected = '&<script>globalXssVar=1;</' + 'script>'; assertEquals('Testing unescapePureXmlEntities_', expected, goog.string.unescapePureXmlEntities_(test)); assertEquals('unescapePureXmlEntities_ is vulnarable to XSS', 0, globalXssVar); } // === tests for goog.string.whitespaceEscape === function testWhiteSpaceEscape() { assertEquals('Should be the same', goog.string.whitespaceEscape('one two three four five '), 'one two &#160;three &#160; four &#160; &#160;five &#160; &#160; '); } // === tests for goog.string.stripQuotes === function testStripQuotes() { assertEquals('Quotes should be stripped', goog.string.stripQuotes('"hello"', '"'), 'hello'); assertEquals('Quotes should be stripped', goog.string.stripQuotes('\'hello\'', '\''), 'hello'); assertEquals('Quotes should not be stripped', goog.string.stripQuotes('-"hello"', '"'), '-"hello"'); } function testStripQuotesMultiple() { assertEquals('Quotes should be stripped', goog.string.stripQuotes('"hello"', '"\''), 'hello'); assertEquals('Quotes should be stripped', goog.string.stripQuotes('\'hello\'', '"\''), 'hello'); assertEquals('Quotes should be stripped', goog.string.stripQuotes('\'hello\'', ''), '\'hello\''); } function testStripQuotesMultiple2() { // Makes sure we do not strip twice assertEquals('Quotes should be stripped', goog.string.stripQuotes('"\'hello\'"', '"\''), '\'hello\''); assertEquals('Quotes should be stripped', goog.string.stripQuotes('"\'hello\'"', '\'"'), '\'hello\''); } // === tests for goog.string.truncate === function testTruncate() { var str = 'abcdefghijklmnopqrstuvwxyz'; assertEquals('Should be equal', goog.string.truncate(str, 8), 'abcde...'); assertEquals('Should be equal', goog.string.truncate(str, 11), 'abcdefgh...'); var html = 'true &amp;&amp; false == false'; assertEquals('Should clip html char', goog.string.truncate(html, 11), 'true &am...'); assertEquals('Should not clip html char', goog.string.truncate(html, 12, true), 'true &amp;&amp; f...'); } // === tests for goog.string.truncateMiddle === function testTruncateMiddle() { var str = 'abcdefghijklmnopqrstuvwxyz'; assertEquals('abc...xyz', goog.string.truncateMiddle(str, 6)); assertEquals( 'abc...yz', goog.string.truncateMiddle(str, 5)); assertEquals(str, goog.string.truncateMiddle(str, str.length)); var html = 'true &amp;&amp; false == false'; assertEquals('Should clip html char', 'true &a...= false', goog.string.truncateMiddle(html, 14)); assertEquals('Should not clip html char', 'true &amp;&amp;...= false', goog.string.truncateMiddle(html, 14, true)); } // === goog.string.quote === function testQuote() { var str = allChars(); var a; try { a = eval(goog.string.quote(str)); } catch (e) { fail('Quote failed: err ' + e.message); } assertEquals(str, a); // empty string str = ''; try { a = eval(goog.string.quote(str)); } catch (e) { fail('Quote failed: err ' + e.message); } assertEquals(str, a); // unicode str = allChars(0, 10000); try { a = eval(goog.string.quote(str)); } catch (e) { fail('Quote failed: err ' + e.message); } assertEquals(str, a); } function testQuoteSpecialChars() { assertEquals('"\\""', goog.string.quote('"')); assertEquals("\"'\"", goog.string.quote("'")); assertEquals("\"\\\\\"", goog.string.quote("\\")); } function testCrossBrowserQuote() { var oldQuoteFn = String.prototype.quote; try { // The vertical space char has weird semantics on jscript, so we don't test // that one. var vertChar = '\x0B'.charCodeAt(0); var str = allChars(0, vertChar) + allChars(vertChar + 1, 10000); var nativeQuote = goog.string.quote(str); String.prototype.quote = null; assertNull("".quote); assertEquals(nativeQuote, goog.string.quote(str)); } finally { String.prototype.quote = oldQuoteFn; } } function allChars(opt_start, opt_end) { opt_start = opt_start || 0; opt_end = opt_end || 256; var rv = ''; for (var i = opt_start; i < opt_end; i++) { rv += String.fromCharCode(i); } return rv; } function testEscapeString() { var expected = allChars(0, 10000); try { var actual = eval('"' + goog.string.escapeString(expected) + '"'); } catch (e) { fail('Quote failed: err ' + e.message); } assertEquals(expected, actual); } function testRemoveAt() { var str = 'barfoobarbazbar'; str = goog.string.removeAt(str, 0, 3); assertEquals('Remove first bar', 'foobarbazbar', str); str = goog.string.removeAt(str, 3, 3); assertEquals('Remove middle bar', 'foobazbar', str); str = goog.string.removeAt(str, 6, 3); assertEquals('Remove last bar', 'foobaz', str); assertEquals('Invalid negative index', 'foobaz', goog.string.removeAt(str, -1, 0)); assertEquals('Invalid overflow index', 'foobaz', goog.string.removeAt(str, 9, 0)); assertEquals('Invalid negative stringLength', 'foobaz', goog.string.removeAt(str, 0, -1)); assertEquals('Invalid overflow stringLength', '', goog.string.removeAt(str, 0, 9)); assertEquals('Invalid overflow index and stringLength', 'foobaz', goog.string.removeAt(str, 9, 9)); assertEquals('Invalid zero stringLength', 'foobaz', goog.string.removeAt(str, 0, 0)); } function testRemove() { var str = 'barfoobarbazbar'; str = goog.string.remove(str, 'bar'); assertEquals('Remove first bar', 'foobarbazbar', str); str = goog.string.remove(str, 'bar'); assertEquals('Remove middle bar', 'foobazbar', str); str = goog.string.remove(str, 'bar'); assertEquals('Remove last bar', 'foobaz', str); str = goog.string.remove(str, 'bar'); assertEquals('Original string', 'foobaz', str); } function testRemoveAll() { var str = 'foobarbazbarfoobazfoo'; str = goog.string.removeAll(str, 'foo'); assertEquals('Remove all occurrences of foo', 'barbazbarbaz', str); str = goog.string.removeAll(str, 'foo'); assertEquals('Original string', 'barbazbarbaz', str); } function testRegExpEscape() { var spec = '()[]{}+-?*.$^|,:#<!\\'; var escapedSpec = '\\' + spec.split('').join('\\'); assertEquals('special chars', escapedSpec, goog.string.regExpEscape(spec)); assertEquals('backslash b', '\\x08', goog.string.regExpEscape('\b')); var s = allChars(); var re = new RegExp('^' + goog.string.regExpEscape(s) + '$'); assertTrue('All ASCII', re.test(s)); s = ''; var re = new RegExp('^' + goog.string.regExpEscape(s) + '$'); assertTrue('empty string', re.test(s)); s = allChars(0, 10000); var re = new RegExp('^' + goog.string.regExpEscape(s) + '$'); assertTrue('Unicode', re.test(s)); } function testPadNumber() { assertEquals('01.250', goog.string.padNumber(1.25, 2, 3)); assertEquals('01.25', goog.string.padNumber(1.25, 2)); assertEquals('01.3', goog.string.padNumber(1.25, 2, 1)); assertEquals('1.25', goog.string.padNumber(1.25, 0)); assertEquals('10', goog.string.padNumber(9.9, 2, 0)); assertEquals('7', goog.string.padNumber(7, 0)); assertEquals('7', goog.string.padNumber(7, 1)); assertEquals('07', goog.string.padNumber(7, 2)); } function testAsString() { assertEquals('', goog.string.makeSafe(null)); assertEquals('', goog.string.makeSafe(undefined)); assertEquals('', goog.string.makeSafe('')); assertEquals('abc', goog.string.makeSafe('abc')); assertEquals('123', goog.string.makeSafe(123)); assertEquals('0', goog.string.makeSafe(0)); assertEquals('true', goog.string.makeSafe(true)); assertEquals('false', goog.string.makeSafe(false)); var funky = function() {}; funky.toString = function() { return 'funky-thing' }; assertEquals('funky-thing', goog.string.makeSafe(funky)); } function testStringRepeat() { assertEquals('', goog.string.repeat('*', 0)); assertEquals('*', goog.string.repeat('*', 1)); assertEquals(' ', goog.string.repeat(' ', 5)); assertEquals('__________', goog.string.repeat('_', 10)); assertEquals('aaa', goog.string.repeat('a', 3)); assertEquals('foofoofoofoofoofoo', goog.string.repeat('foo', 6)); } function testBuildString() { assertEquals('', goog.string.buildString()); assertEquals('a', goog.string.buildString('a')); assertEquals('ab', goog.string.buildString('ab')); assertEquals('ab', goog.string.buildString('a', 'b')); assertEquals('abcd', goog.string.buildString('a', 'b', 'c', 'd')); assertEquals('0', goog.string.buildString(0)); assertEquals('0123', goog.string.buildString(0, 1, 2, 3)); assertEquals('ab01', goog.string.buildString('a', 'b', 0, 1)); assertEquals('', goog.string.buildString(null, undefined)); } function testCompareVersions() { var f = goog.string.compareVersions; assertTrue('numeric equality broken', f(1, 1) == 0) assertTrue('numeric less than broken', f(1.0, 1.1) < 0) assertTrue('numeric greater than broken', f(2.0, 1.1) > 0) assertTrue('exact equality broken', f('1.0', '1.0') == 0) assertTrue('mutlidot equality broken', f('1.0.0.0', '1.0') == 0); assertTrue('mutlidigit equality broken', f('1.000', '1.0') == 0); assertTrue('less than broken', f('1.0.2.1', '1.1') < 0); assertTrue('greater than broken', f('1.1', '1.0.2.1') > 0); assertTrue('substring less than broken', f('1', '1.1') < 0) assertTrue('substring greater than broken', f('2.2', '2') > 0) assertTrue('b greater than broken', f('1.1', '1.1b') > 0); assertTrue('b less than broken', f('1.1b', '1.1') < 0); assertTrue('b equality broken', f('1.1b', '1.1b') == 0); assertTrue('b>a broken', f('1.1b', '1.1a') > 0); assertTrue('a<b broken', f('1.1a', '1.1b') < 0); assertTrue('9.5<9.10 broken', f('9.5', '9.10') < 0); assertTrue('9.5<9.11 broken', f('9.5', '9.11') < 0); assertTrue('9.11>9.10 broken', f('9.11', '9.10') > 0); assertTrue('9.1<9.10 broken', f('9.1', '9.10') < 0); assertTrue('9.1.1<9.10 broken', f('9.1.1', '9.10') < 0); assertTrue('9.1.1<9.11 broken', f('9.1.1', '9.11') < 0); assertTrue('10a>9b broken', f('1.10a', '1.9b') > 0); assertTrue('b<b2 broken', f('1.1b', '1.1b2') < 0); assertTrue('b10>b9 broken', f('1.1b10', '1.1b9') > 0); assertTrue('7>6 broken with leading whitespace', f(' 7', '6') > 0); assertTrue('7>6 broken with trailing whitespace', f('7 ', '6') > 0); } function testIsUnicodeChar() { assertFalse('empty string broken', goog.string.isUnicodeChar('')); assertFalse('non-single char string broken', goog.string.isUnicodeChar('abc')); assertTrue('space broken', goog.string.isUnicodeChar(' ')); assertTrue('single char broken', goog.string.isUnicodeChar('a')); assertTrue('upper case broken', goog.string.isUnicodeChar('A')); assertTrue('unicode char broken', goog.string.isUnicodeChar('\u0C07')); } function assertHashcodeEquals(expectedHashCode, str) { assertEquals('wrong hashCode for ' + str.substring(0, 32), expectedHashCode, goog.string.hashCode(str)); } /** * Verify we get random-ish looking values for hash of Strings. */ function testHashCode() { try { goog.string.hashCode(null); fail('should throw exception for null'); } catch (ex) { // success } assertHashcodeEquals(0, ''); assertHashcodeEquals(101574, 'foo'); assertHashcodeEquals(1301670364, '\uAAAAfoo'); assertHashcodeEquals(92567585, goog.string.repeat('a', 5)); assertHashcodeEquals(2869595232, goog.string.repeat('a', 6)); assertHashcodeEquals(3058106369, goog.string.repeat('a', 7)); assertHashcodeEquals(312017024, goog.string.repeat('a', 8)); assertHashcodeEquals(2929737728, goog.string.repeat('a', 1024)); } function testUniqueString() { var TEST_COUNT = 20; var obj = {}; for (var i = 0; i < TEST_COUNT; i++) { obj[goog.string.createUniqueString()] = true; } assertEquals('All strings should be unique.', TEST_COUNT, goog.object.getCount(obj)); } function testToNumber() { // First, test the cases goog.string.toNumber() was primarily written for, // because JS built-ins are dumb. assertNaN(goog.string.toNumber('123a')); assertNaN(goog.string.toNumber('123.456.78')); assertNaN(goog.string.toNumber('')); assertNaN(goog.string.toNumber(' ')); // Now, sanity-check. assertEquals(123, goog.string.toNumber(' 123 ')); assertEquals(321.123, goog.string.toNumber('321.123')); assertEquals(1.00001, goog.string.toNumber('1.00001')); assertEquals(1, goog.string.toNumber('1.00000')); assertEquals(0.2, goog.string.toNumber('0.20')); assertEquals(0, goog.string.toNumber('0')); assertEquals(0, goog.string.toNumber('0.0')); assertEquals(-1, goog.string.toNumber('-1')); assertEquals(-0.3, goog.string.toNumber('-.3')); assertEquals(-12.345, goog.string.toNumber('-12.345')); assertEquals(100, goog.string.toNumber('1e2')); assertEquals(0.123, goog.string.toNumber('12.3e-2')); assertNaN(goog.string.toNumber('abc')); } </script> </body> </html>