| 1 |
// Copyright 2012 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 |
goog.provide('goog.labs.format.csvTest'); |
| 16 |
|
| 17 |
goog.require('goog.labs.format.csv'); |
| 18 |
goog.require('goog.labs.format.csv.ParseError'); |
| 19 |
goog.require('goog.object'); |
| 20 |
goog.require('goog.testing.asserts'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
goog.setTestOnly('goog.labs.format.csvTest'); |
| 24 |
|
| 25 |
|
| 26 |
function testGoldenPath() { |
| 27 |
assertObjectEquals( |
| 28 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 29 |
goog.labs.format.csv.parse('a,b,c\nd,e,f\ng,h,i\n')); |
| 30 |
assertObjectEquals( |
| 31 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 32 |
goog.labs.format.csv.parse('a,b,c\r\nd,e,f\r\ng,h,i\r\n')); |
| 33 |
} |
| 34 |
|
| 35 |
function testGoldenPathWithSpaceAsCustomDelimiter() { |
| 36 |
assertObjectEquals( |
| 37 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 38 |
goog.labs.format.csv.parse('a b c\nd e f\ng h i\n', undefined, ' ')); |
| 39 |
assertObjectEquals( |
| 40 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 41 |
goog.labs.format.csv.parse( |
| 42 |
'a b c\r\nd e f\r\ng h i\r\n', undefined, ' ')); |
| 43 |
} |
| 44 |
|
| 45 |
function testGoldenPathWithEmptyStringAsCustomDelimiterUsesComma() { |
| 46 |
assertObjectEquals( |
| 47 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 48 |
goog.labs.format.csv.parse('a,b,c\nd,e,f\ng,h,i\n', undefined, '')); |
| 49 |
assertObjectEquals( |
| 50 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 51 |
goog.labs.format.csv.parse('a,b,c\r\nd,e,f\r\ng,h,i\r\n', undefined, '')); |
| 52 |
} |
| 53 |
|
| 54 |
function testBadDelimitersNewLine() { |
| 55 |
var e = assertThrows(function() { |
| 56 |
goog.labs.format.csv.parse('a,b,c\r\nd,e,f\r\ng,h,i\r\n', undefined, '\n'); |
| 57 |
}); |
| 58 |
assertEquals( |
| 59 |
'Assertion failed: Cannot use newline or carriage return has delimiter.', |
| 60 |
e.message); |
| 61 |
} |
| 62 |
|
| 63 |
function testBadDelimitersCarriageReturn() { |
| 64 |
var e = assertThrows(function() { |
| 65 |
goog.labs.format.csv.parse('a,b,c\r\nd,e,f\r\ng,h,i\r\n', undefined, '\r'); |
| 66 |
}); |
| 67 |
assertEquals( |
| 68 |
'Assertion failed: Cannot use newline or carriage return has delimiter.', |
| 69 |
e.message); |
| 70 |
} |
| 71 |
|
| 72 |
function testBadDelimitersTwoCharacter() { |
| 73 |
var e = assertThrows(function() { |
| 74 |
goog.labs.format.csv.parse('a,b,c\r\nd,e,f\r\ng,h,i\r\n', undefined, 'aa'); |
| 75 |
}); |
| 76 |
assertEquals( |
| 77 |
'Assertion failed: Delimiter must be a single character.', e.message); |
| 78 |
} |
| 79 |
|
| 80 |
function testNoCrlfAtEnd() { |
| 81 |
assertObjectEquals( |
| 82 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 83 |
goog.labs.format.csv.parse('a,b,c\nd,e,f\ng,h,i')); |
| 84 |
} |
| 85 |
|
| 86 |
function testQuotes() { |
| 87 |
assertObjectEquals( |
| 88 |
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], |
| 89 |
goog.labs.format.csv.parse('a,"b",c\n"d","e","f"\ng,h,"i"')); |
| 90 |
assertObjectEquals( |
| 91 |
[['a', 'b, as in boy', 'c'], ['d', 'e', 'f']], |
| 92 |
goog.labs.format.csv.parse('a,"b, as in boy",c\n"d","e","f"\n')); |
| 93 |
} |
| 94 |
|
| 95 |
function testEmbeddedCrlfs() { |
| 96 |
assertObjectEquals( |
| 97 |
[['a', 'b\nball', 'c'], ['d\nd', 'e', 'f'], ['g', 'h', 'i']], |
| 98 |
goog.labs.format.csv.parse('a,"b\nball",c\n"d\nd","e","f"\ng,h,"i"\n')); |
| 99 |
} |
| 100 |
|
| 101 |
function testEmbeddedQuotes() { |
| 102 |
assertObjectEquals( |
| 103 |
[['a', '"b"', 'Jonathan "Smokey" Feinberg'], ['d', 'e', 'f']], |
| 104 |
goog.labs.format.csv.parse( |
| 105 |
'a,"""b""","Jonathan ""Smokey"" Feinberg"\nd,e,f\r\n')); |
| 106 |
} |
| 107 |
|
| 108 |
function testUnclosedQuote() { |
| 109 |
var e = |
| 110 |
assertThrows(function() { goog.labs.format.csv.parse('a,"b,c\nd,e,f'); }); |
| 111 |
|
| 112 |
assertTrue(e instanceof goog.labs.format.csv.ParseError); |
| 113 |
assertEquals(2, e.position.line); |
| 114 |
assertEquals(5, e.position.column); |
| 115 |
assertEquals( |
| 116 |
'Unexpected end of text after open quote at line 2 column 5\n' + |
| 117 |
'd,e,f\n' + |
| 118 |
' ^', |
| 119 |
e.message); |
| 120 |
} |
| 121 |
|
| 122 |
function testQuotesInUnquotedField() { |
| 123 |
var e = assertThrows(function() { |
| 124 |
goog.labs.format.csv.parse('a,b "and" b,c\nd,e,f'); |
| 125 |
}); |
| 126 |
|
| 127 |
assertTrue(e instanceof goog.labs.format.csv.ParseError); |
| 128 |
|
| 129 |
assertEquals(1, e.position.line); |
| 130 |
assertEquals(5, e.position.column); |
| 131 |
|
| 132 |
assertEquals( |
| 133 |
'Unexpected quote mark at line 1 column 5\n' + |
| 134 |
'a,b "and" b,c\n' + |
| 135 |
' ^', |
| 136 |
e.message); |
| 137 |
} |
| 138 |
|
| 139 |
function testGarbageOutsideQuotes() { |
| 140 |
var e = assertThrows(function() { |
| 141 |
goog.labs.format.csv.parse('a,"b",c\nd,"e"oops,f'); |
| 142 |
}); |
| 143 |
|
| 144 |
assertTrue(e instanceof goog.labs.format.csv.ParseError); |
| 145 |
assertEquals(2, e.position.line); |
| 146 |
assertEquals(6, e.position.column); |
| 147 |
assertEquals( |
| 148 |
'Unexpected character "o" after quote mark at line 2 column 6\n' + |
| 149 |
'd,"e"oops,f\n' + |
| 150 |
' ^', |
| 151 |
e.message); |
| 152 |
} |
| 153 |
|
| 154 |
function testEmptyRecords() { |
| 155 |
assertObjectEquals( |
| 156 |
[['a', '', 'c'], ['d', 'e', ''], ['', '', '']], |
| 157 |
goog.labs.format.csv.parse('a,,c\r\nd,e,\n,,')); |
| 158 |
} |
| 159 |
|
| 160 |
function testEmptyRecordsWithColonCustomDelimiter() { |
| 161 |
assertObjectEquals( |
| 162 |
[['a', '', 'c'], ['d', 'e', ''], ['', '', '']], |
| 163 |
goog.labs.format.csv.parse('a::c\r\nd:e:\n::', undefined, ':')); |
| 164 |
} |
| 165 |
|
| 166 |
function testIgnoringErrors() { |
| 167 |
// The results of these tests are not defined by the RFC. They |
| 168 |
// generally strive to be "reasonable" while keeping the code simple. |
| 169 |
|
| 170 |
// Quotes inside field |
| 171 |
assertObjectEquals( |
| 172 |
[['Hello "World"!', 'b'], ['c', 'd']], |
| 173 |
goog.labs.format.csv.parse('Hello "World"!,b\nc,d', true)); |
| 174 |
|
| 175 |
// Missing closing quote |
| 176 |
assertObjectEquals( |
| 177 |
[['Hello', 'World!']], goog.labs.format.csv.parse('Hello,"World!', true)); |
| 178 |
|
| 179 |
// Broken use of quotes in quoted field |
| 180 |
assertObjectEquals( |
| 181 |
[['a', '"Hello"World!"']], |
| 182 |
goog.labs.format.csv.parse('a,"Hello"World!"', true)); |
| 183 |
|
| 184 |
// All of the above. A real mess. |
| 185 |
assertObjectEquals( |
| 186 |
[['This" is', '"very\n\tvery"broken"', ' indeed!']], |
| 187 |
goog.labs.format.csv.parse( |
| 188 |
'This" is,"very\n\tvery"broken"," indeed!', true)); |
| 189 |
} |
| 190 |
|
| 191 |
function testIgnoringErrorsTrailingTabs() { |
| 192 |
assertObjectEquals( |
| 193 |
[['"a\tb"\t'], ['c,d']], |
| 194 |
goog.labs.format.csv.parse('"a\tb"\t\n"c,d"', true)); |
| 195 |
} |
| 196 |
|
| 197 |
function testFindLineInfo() { |
| 198 |
var testString = 'abc\ndef\rghi'; |
| 199 |
var info = goog.labs.format.csv.ParseError.findLineInfo_(testString, 4); |
| 200 |
|
| 201 |
assertEquals(4, info.line.startLineIndex); |
| 202 |
assertEquals(7, info.line.endContentIndex); |
| 203 |
assertEquals(8, info.line.endLineIndex); |
| 204 |
|
| 205 |
assertEquals(1, info.lineIndex); |
| 206 |
} |
| 207 |
|
| 208 |
function testGetLineDebugString() { |
| 209 |
var str = 'abcdefghijklmnop'; |
| 210 |
var index = str.indexOf('j'); |
| 211 |
var column = index + 1; |
| 212 |
assertEquals( |
| 213 |
goog.labs.format.csv.ParseError.getLineDebugString_(str, column), |
| 214 |
'abcdefghijklmnop\n' + |
| 215 |
' ^'); |
| 216 |
} |
| 217 |
|
| 218 |
function testIsCharacterString() { |
| 219 |
assertTrue(goog.labs.format.csv.isCharacterString_('a')); |
| 220 |
assertTrue(goog.labs.format.csv.isCharacterString_('\n')); |
| 221 |
assertTrue(goog.labs.format.csv.isCharacterString_(' ')); |
| 222 |
|
| 223 |
assertFalse(goog.labs.format.csv.isCharacterString_(null)); |
| 224 |
assertFalse(goog.labs.format.csv.isCharacterString_(' ')); |
| 225 |
assertFalse(goog.labs.format.csv.isCharacterString_('')); |
| 226 |
assertFalse(goog.labs.format.csv.isCharacterString_('aa')); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
function testAssertToken() { |
| 231 |
goog.labs.format.csv.assertToken_('a'); |
| 232 |
|
| 233 |
goog.object.forEach(goog.labs.format.csv.SENTINELS_, function(value) { |
| 234 |
goog.labs.format.csv.assertToken_(value); |
| 235 |
}); |
| 236 |
|
| 237 |
assertThrows(function() { goog.labs.format.csv.assertToken_('aa'); }); |
| 238 |
|
| 239 |
assertThrows(function() { goog.labs.format.csv.assertToken_(''); }); |
| 240 |
|
| 241 |
assertThrows(function() { goog.labs.format.csv.assertToken_({}); }); |
| 242 |
} |