csv_test.js 7.7 KB

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