newlines_test.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Unit tests for goog.string.
  16. */
  17. goog.provide('goog.string.newlinesTest');
  18. goog.require('goog.string.newlines');
  19. goog.require('goog.testing.jsunit');
  20. goog.setTestOnly('goog.string.newlinesTest');
  21. // test for goog.string.splitLines
  22. function testSplitLines() {
  23. /**
  24. * @param {!Array<string>} expected
  25. * @param {string} string
  26. * @param {boolean=} opt_keepNewlines
  27. */
  28. function assertSplitLines(expected, string, opt_keepNewlines) {
  29. var keepNewlines = opt_keepNewlines || false;
  30. var lines = goog.string.newlines.splitLines(string, keepNewlines);
  31. assertElementsEquals(expected, lines);
  32. }
  33. // Test values borrowed from Python's splitlines. http://goo.gl/iwawx
  34. assertSplitLines(['abc', 'def', '', 'ghi'], 'abc\ndef\n\rghi');
  35. assertSplitLines(['abc', 'def', '', 'ghi'], 'abc\ndef\n\r\nghi');
  36. assertSplitLines(['abc', 'def', 'ghi'], 'abc\ndef\r\nghi');
  37. assertSplitLines(['abc', 'def', 'ghi'], 'abc\ndef\r\nghi\n');
  38. assertSplitLines(['abc', 'def', 'ghi', ''], 'abc\ndef\r\nghi\n\r');
  39. assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r');
  40. assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r');
  41. assertSplitLines(
  42. ['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], '\nabc\ndef\r\nghi\n\r', true);
  43. assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r');
  44. assertSplitLines(
  45. ['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], '\nabc\ndef\r\nghi\n\r', true);
  46. }
  47. function testGetLines() {
  48. var lines = goog.string.newlines.getLines('abc\ndef\n\rghi');
  49. assertEquals(4, lines.length);
  50. assertEquals(0, lines[0].startLineIndex);
  51. assertEquals(3, lines[0].endContentIndex);
  52. assertEquals(4, lines[0].endLineIndex);
  53. assertEquals('abc', lines[0].getContent());
  54. assertEquals('abc\n', lines[0].getFullLine());
  55. assertEquals('\n', lines[0].getNewline());
  56. assertEquals(4, lines[1].startLineIndex);
  57. assertEquals(7, lines[1].endContentIndex);
  58. assertEquals(8, lines[1].endLineIndex);
  59. assertEquals('def', lines[1].getContent());
  60. assertEquals('def\n', lines[1].getFullLine());
  61. assertEquals('\n', lines[1].getNewline());
  62. assertEquals(8, lines[2].startLineIndex);
  63. assertEquals(8, lines[2].endContentIndex);
  64. assertEquals(9, lines[2].endLineIndex);
  65. assertEquals('', lines[2].getContent());
  66. assertEquals('\r', lines[2].getFullLine());
  67. assertEquals('\r', lines[2].getNewline());
  68. assertEquals(9, lines[3].startLineIndex);
  69. assertEquals(12, lines[3].endContentIndex);
  70. assertEquals(12, lines[3].endLineIndex);
  71. assertEquals('ghi', lines[3].getContent());
  72. assertEquals('ghi', lines[3].getFullLine());
  73. assertEquals('', lines[3].getNewline());
  74. }