| 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 |
/** |
| 16 |
* @fileoverview Unit tests for goog.string. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.string.newlinesTest'); |
| 20 |
|
| 21 |
goog.require('goog.string.newlines'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
goog.setTestOnly('goog.string.newlinesTest'); |
| 25 |
|
| 26 |
// test for goog.string.splitLines |
| 27 |
function testSplitLines() { |
| 28 |
/** |
| 29 |
* @param {!Array<string>} expected |
| 30 |
* @param {string} string |
| 31 |
* @param {boolean=} opt_keepNewlines |
| 32 |
*/ |
| 33 |
function assertSplitLines(expected, string, opt_keepNewlines) { |
| 34 |
var keepNewlines = opt_keepNewlines || false; |
| 35 |
var lines = goog.string.newlines.splitLines(string, keepNewlines); |
| 36 |
assertElementsEquals(expected, lines); |
| 37 |
} |
| 38 |
|
| 39 |
// Test values borrowed from Python's splitlines. http://goo.gl/iwawx |
| 40 |
assertSplitLines(['abc', 'def', '', 'ghi'], 'abc\ndef\n\rghi'); |
| 41 |
assertSplitLines(['abc', 'def', '', 'ghi'], 'abc\ndef\n\r\nghi'); |
| 42 |
assertSplitLines(['abc', 'def', 'ghi'], 'abc\ndef\r\nghi'); |
| 43 |
assertSplitLines(['abc', 'def', 'ghi'], 'abc\ndef\r\nghi\n'); |
| 44 |
assertSplitLines(['abc', 'def', 'ghi', ''], 'abc\ndef\r\nghi\n\r'); |
| 45 |
assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r'); |
| 46 |
assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r'); |
| 47 |
assertSplitLines( |
| 48 |
['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], '\nabc\ndef\r\nghi\n\r', true); |
| 49 |
assertSplitLines(['', 'abc', 'def', 'ghi', ''], '\nabc\ndef\r\nghi\n\r'); |
| 50 |
assertSplitLines( |
| 51 |
['\n', 'abc\n', 'def\r\n', 'ghi\n', '\r'], '\nabc\ndef\r\nghi\n\r', true); |
| 52 |
} |
| 53 |
|
| 54 |
function testGetLines() { |
| 55 |
var lines = goog.string.newlines.getLines('abc\ndef\n\rghi'); |
| 56 |
|
| 57 |
assertEquals(4, lines.length); |
| 58 |
|
| 59 |
assertEquals(0, lines[0].startLineIndex); |
| 60 |
assertEquals(3, lines[0].endContentIndex); |
| 61 |
assertEquals(4, lines[0].endLineIndex); |
| 62 |
assertEquals('abc', lines[0].getContent()); |
| 63 |
assertEquals('abc\n', lines[0].getFullLine()); |
| 64 |
assertEquals('\n', lines[0].getNewline()); |
| 65 |
|
| 66 |
assertEquals(4, lines[1].startLineIndex); |
| 67 |
assertEquals(7, lines[1].endContentIndex); |
| 68 |
assertEquals(8, lines[1].endLineIndex); |
| 69 |
assertEquals('def', lines[1].getContent()); |
| 70 |
assertEquals('def\n', lines[1].getFullLine()); |
| 71 |
assertEquals('\n', lines[1].getNewline()); |
| 72 |
|
| 73 |
assertEquals(8, lines[2].startLineIndex); |
| 74 |
assertEquals(8, lines[2].endContentIndex); |
| 75 |
assertEquals(9, lines[2].endLineIndex); |
| 76 |
assertEquals('', lines[2].getContent()); |
| 77 |
assertEquals('\r', lines[2].getFullLine()); |
| 78 |
assertEquals('\r', lines[2].getNewline()); |
| 79 |
|
| 80 |
assertEquals(9, lines[3].startLineIndex); |
| 81 |
assertEquals(12, lines[3].endContentIndex); |
| 82 |
assertEquals(12, lines[3].endLineIndex); |
| 83 |
assertEquals('ghi', lines[3].getContent()); |
| 84 |
assertEquals('ghi', lines[3].getFullLine()); |
| 85 |
assertEquals('', lines[3].getNewline()); |
| 86 |
} |