PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mjsunit/third_party/string-trim.js

http://v8.googlecode.com/
JavaScript | 107 lines | 61 code | 13 blank | 33 comment | 2 complexity | 804b8449352585e7ab219df336d5378c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
  2. //
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions
  5. // are met:
  6. //
  7. // 1. Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. //
  10. // 2. Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following
  12. // disclaimer in the documentation and/or other materials provided
  13. // with the distribution.
  14. //
  15. // 3. Neither the name of the copyright holder(s) nor the names of any
  16. // contributors may be used to endorse or promote products derived
  17. // from this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. // OF THE POSSIBILITY OF SUCH DAMAGE.
  31. // Based on LayoutTests/fast/js/script-tests/string-trim.js
  32. // References to trim(), trimLeft() and trimRight() functions for
  33. // testing Function's *.call() and *.apply() methods.
  34. var trim = String.prototype.trim;
  35. var trimLeft = String.prototype.trimLeft;
  36. var trimRight = String.prototype.trimRight;
  37. var testString = 'foo bar';
  38. var trimString = '';
  39. var leftTrimString = '';
  40. var rightTrimString = '';
  41. var wsString = '';
  42. var whitespace = [
  43. {s : '\u0009', t : 'HORIZONTAL TAB'},
  44. {s : '\u000A', t : 'LINE FEED OR NEW LINE'},
  45. {s : '\u000B', t : 'VERTICAL TAB'},
  46. {s : '\u000C', t : 'FORMFEED'},
  47. {s : '\u000D', t : 'CARRIAGE RETURN'},
  48. {s : '\u0020', t : 'SPACE'},
  49. {s : '\u00A0', t : 'NO-BREAK SPACE'},
  50. {s : '\u2000', t : 'EN QUAD'},
  51. {s : '\u2001', t : 'EM QUAD'},
  52. {s : '\u2002', t : 'EN SPACE'},
  53. {s : '\u2003', t : 'EM SPACE'},
  54. {s : '\u2004', t : 'THREE-PER-EM SPACE'},
  55. {s : '\u2005', t : 'FOUR-PER-EM SPACE'},
  56. {s : '\u2006', t : 'SIX-PER-EM SPACE'},
  57. {s : '\u2007', t : 'FIGURE SPACE'},
  58. {s : '\u2008', t : 'PUNCTUATION SPACE'},
  59. {s : '\u2009', t : 'THIN SPACE'},
  60. {s : '\u200A', t : 'HAIR SPACE'},
  61. {s : '\u3000', t : 'IDEOGRAPHIC SPACE'},
  62. {s : '\u2028', t : 'LINE SEPARATOR'},
  63. {s : '\u2029', t : 'PARAGRAPH SEPARATOR'},
  64. {s : '\u200B', t : 'ZERO WIDTH SPACE (category Cf)'}
  65. ];
  66. for (var i = 0; i < whitespace.length; i++) {
  67. assertEquals(whitespace[i].s.trim(), '');
  68. assertEquals(whitespace[i].s.trimLeft(), '');
  69. assertEquals(whitespace[i].s.trimRight(), '');
  70. wsString += whitespace[i].s;
  71. }
  72. trimString = wsString + testString + wsString;
  73. leftTrimString = testString + wsString; // Trimmed from the left.
  74. rightTrimString = wsString + testString; // Trimmed from the right.
  75. assertEquals(wsString.trim(), '');
  76. assertEquals(wsString.trimLeft(), '');
  77. assertEquals(wsString.trimRight(), '');
  78. assertEquals(trimString.trim(), testString);
  79. assertEquals(trimString.trimLeft(), leftTrimString);
  80. assertEquals(trimString.trimRight(), rightTrimString);
  81. assertEquals(leftTrimString.trim(), testString);
  82. assertEquals(leftTrimString.trimLeft(), leftTrimString);
  83. assertEquals(leftTrimString.trimRight(), testString);
  84. assertEquals(rightTrimString.trim(), testString);
  85. assertEquals(rightTrimString.trimLeft(), testString);
  86. assertEquals(rightTrimString.trimRight(), rightTrimString);
  87. var testValues = [0, Infinity, NaN, true, false, ({}), ['an','array'],
  88. ({toString:function(){return 'wibble'}})
  89. ];
  90. for (var i = 0; i < testValues.length; i++) {
  91. assertEquals(trim.call(testValues[i]), String(testValues[i]));
  92. assertEquals(trimLeft.call(testValues[i]), String(testValues[i]));
  93. assertEquals(trimRight.call(testValues[i]), String(testValues[i]));
  94. }