dsv 0.12.15 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd41469d831d22e8d0d7f02290d707ebd460568e93a47eb8bcf2dba5873d372b
4
- data.tar.gz: 28fbb75af892221f48234466c98bb7ba08ec0501a2c00f57912398c46844d9aa
3
+ metadata.gz: db9ee29841ef9fa86e047c7c2793bc249fb6a565bc6f76f3a4fb0f24e12ee4b9
4
+ data.tar.gz: 8cd6112162fd005f994a1d2db8699e791e547d0e8ee1f633628ecd246b490cac
5
5
  SHA512:
6
- metadata.gz: 49632e6c244821844316ceffa81df20c9aab05202ce6c69e925eba3d4289d380c2aca4aa893e698db883c78e6cc5fe7e90f1a0465996737210ae7660ffc59a02
7
- data.tar.gz: 0d86e129bd8934f1bd5ab3e13eb7219b06663bb26e040622064e0b021d61433fbf1713f72266a9d9e6326975eadd8ee289eb72e0a7987c0e43966077fde6d778
6
+ metadata.gz: 7645c70c8ac8664ad7febcfe16db321a90ef334946535cf45a63582417cbde9d6353b4cd4fb991daa177b97ac4cd8f74d5e28ed92d422ba283c29676d036ea8b
7
+ data.tar.gz: 2f932b86ad2fa36ed622b13cb88d5e425d51075b75f1c4a4f05e73eb47c03d18da52379b7618b8c5152d227b74ba8294da9b404772d5cf8696abf1bc4e78a86e
data/CHANGELOG CHANGED
@@ -1,5 +1,22 @@
1
1
  # dsv/CHANGELOG
2
2
 
3
+ ## 20261124
4
+
5
+ 0.13.0: Under :double, a row which breaks it raises, where from 0.12.5 to 0.12.15 it was read as unspecified.
6
+
7
+ 1. ~ split_row(): Under :double, a row with a field which is not quoted raises DSV::UnquotedField. 0.12.5 item 2 read such a row as unspecified: a change of design, which cost :double a scan of each unquoted row and a count of each field holding a quote so as to know when to fall back, and so a minor, released as a patch beside a fix.
8
+ 2. ~ split_double_quoted(): A row not starting with a quote raises UnquotedField, an unquoted header included, which every release before split plainly. The row is read in full only where its last field seems open or the split leaves a lone quote in a field.
9
+ 3. + reassemble_wholly_quoted_fields(): A row under :double read in full, a field which is not quoted raising UnquotedField.
10
+ 4. + lib/DSV/Error.rb, + lib/DSV/UnexpectedRow.rb, + lib/DSV/UnquotedField.rb: DSV::Error is the base of what dsv raises about the data; UnexpectedRow, beneath it, a row which contradicts its mode, with its line and mode; UnquotedField, beneath that, a field with no quotes under a wholly quoted mode.
11
+ 5. ~ lib/DSV/UnterminatedQuote.rb: UnterminatedQuote is a DSV::Error.
12
+ 6. ~ row_fields(), parse_row(), columns: An UnexpectedRow is given the line its row began on.
13
+ 7. ~ test/dsv_test.rb: Rows which break :double raise, with their line and mode, an unquoted header included; a field holding quote-separator-quote and an empty last field are read under :double; the errors' classes are tested.
14
+ 8. ~ README.md: A row which breaks :double raises UnquotedField, where it was read as with no mode named. The errors are listed.
15
+ 9. ~ TODO: Only the jobs in hand are kept, in order and without their releases; the rest are in ROADMAP.md. The readers are no longer renamed as nouns, split_double_quoted() alone becoming split_wholly_quoted(). Jobs are added for write keeping a repeated name, and for attributes= setting columns.
16
+ 10. ~ ROADMAP.md: Speed is given as the reason for the modes and for no fallback. 0.13.0 undoes the fallback, naming 0.12.5's errors; 0.14.0 makes the internals private; the declarations are 0.15.0 and streaming 0.16.0. Path or text is no longer a theme, and speed against other languages is one.
17
+ 11. ~ PROPOSED.md: The exception raised is settled. :collect and :infer are asked after, and the version rule of filter alone. The quote character notes what a quote longer than a byte would cost.
18
+ 12. ~ lib/DSV/VERSION.rb: /0.12.15/0.13.0/.
19
+
3
20
  ## 20261120
4
21
 
5
22
  0.12.15: normalised_mode, row_renderer and write_row are laid out as SimpleCSV 0.11.5 has them.
data/README.md CHANGED
@@ -96,10 +96,12 @@ DSV.read('plain.csv', headers: true, quote: :none) # no field is quoted; quot
96
96
  DSV.read('quoted.csv', headers: true, quote: :double) # every field is quoted, as DSV writes them
97
97
  ```
98
98
 
99
- Under `:double` a row that is wholly unquoted, as a header row often is, is split plainly, and a row that is neither wholly quoted nor wholly unquoted is read as it would be with no mode named, so naming the mode never costs a correct read.
99
+ A named mode is held to: under `:double` every field of every row is quoted, the header's included. A row with a field which isn't raises `DSV::UnquotedField`, whose `line` is the line its row began on and `mode` the mode in force. To read a file whose quoting varies, name no mode.
100
100
 
101
101
  Input which isn't RFC 4180 is read to one rule. A quote opens a field only at the start of one, so a quote anywhere else, as in `12" pizza`, is data. A quoted field closes only at a quote followed by the separator or the end of the row, so one which doesn't close by the end of the source, as `"abc` or `"ab"c` doesn't, raises `DSV::UnterminatedQuote`, whose `line` is the line its row began on.
102
102
 
103
+ Every error about the data is a `DSV::Error`: `UnterminatedQuote`, and `UnexpectedRow`, a row which contradicts its mode, of which `UnquotedField` is one kind. A mistake in the options is an `ArgumentError`.
104
+
103
105
  ### Writing
104
106
 
105
107
  Set the columns, give the rows as Hashes, and write:
data/TODO CHANGED
@@ -1,39 +1,19 @@
1
1
  # TODO
2
2
 
3
- 1. filter, a class-level alias of select. (0.12.x)
4
- 2. The internals private. (0.12.x)
5
- 3. split_quoted() renamed quoted_fields(), and split_double_quoted() double_quoted_fields(). (0.12.x)
6
- 4. An unknown quote mode, file mode or option raises ArgumentError. (0.13.0)
7
- 5. :unspecified, the name of the default. (0.13.0)
8
- 6. :unquoted, the name of the mode reading no quotes, :none kept as an alias. (0.13.0)
9
- 7. The reader chosen once, when the quote mode is set, as row_renderer is for writing. (0.13.0)
10
- 8. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists. (0.13.0)
11
- 9. Under :double, a row not starting with a quote raises, the header included. (0.13.0)
12
- 10. Under :double, a bare field in a row read in full raises. (0.13.0)
13
- 11. :single read as :double is, with a single quote, once :double raises. (0.13.0)
14
- 12. each yields each row as it is read. (0.14.0)
15
- 13. each returns its receiver. (0.14.0)
16
- 14. rows no longer filled by each. (0.14.0)
17
- 15. Chunks from each_slice. (0.14.0)
18
- 16. The source read once, start to end, with no rewind; the header from the first row read. (0.14.0)
19
- 17. The line of an error found by seeking only where the source can. (0.14.0)
20
- 18. A rewind left to r+ rewriting and to first_row called alone. (0.14.0)
21
- 19. A corpus in the tests: a day of NEMWEB's dispatch files, trimmed, and the RFC 4180 cases, each checked against stdlib CSV's rows. (1.0)
22
- 20. A benchmark script against stdlib CSV. (1.0)
23
- 21. README: the speed sentence written from the benchmark's output. (1.0)
24
- 22. README: every mode by example. (1.0)
25
- 23. README: a table matching each mode to stdlib CSV's and Python's settings. (1.0)
26
- 24. A per-row fast path for the default: split, the fields bare or wrapped in two quotes unwrapped, any other row read in full. (1.x)
27
- 25. as_array: returning the split fields, rather than building a Hash and then an Array from it. (1.x)
28
- 26. Rows built by zip.to_h. (1.x)
29
- 27. The scanner-based parser, at the tag scanner-parser-20261006, judged against the string operations used now. (1.x)
30
- 28. :rfc_4180, a quote mode reading what RFC 4180 allows as :unspecified does and raising on what it forbids. (1.x)
31
- 29. field_count:, :header or an Integer, off by default: a row of another width raises. (1.x)
32
- 30. Writing with minimal quoting: a field quoted only where it holds a separator, a quote or a row terminator. (1.x)
33
- 31. A line number in each parse error, found only when the error is raised, where the source can seek. (1.x)
34
- 32. line_numbers:, off by default, counting lines as they are read, for any source, a pipe included. (1.x)
35
- 33. headers?, the instance-level header_row renamed, header_row kept as an alias. (1.x)
36
- 34. <<, to append a row. (1.x)
37
- 35. generate, to build a string. (1.x)
38
- 36. Symbols and Strings interchangeable for column names, in a selection, a columns: list and a row's keys. (1.x)
39
- 37. Duplicate headers by option: all kept as an Array, the default; indexed, a, a.1; first; last; error. (1.x)
3
+ 1. write with no columns defined keeping a repeated name, each of its values in a field of its own, where the Array is written whole into one field.
4
+ 2. filter, a class-level alias of select.
5
+ 3. The internals private, and split_double_quoted() renamed split_wholly_quoted().
6
+ 4. An unknown quote mode, file mode or option raises ArgumentError.
7
+ 5. :unspecified, the name of the default.
8
+ 6. :unquoted, the name of the mode reading no quotes, :none kept as an alias.
9
+ 7. The reader chosen once, when the quote mode is set, as row_renderer is for writing.
10
+ 8. .read and .open taking a path, and .parse text; a path which does not exist raising, and nothing inferred from whether a file exists.
11
+ 9. :single read as :double is, with a single quote.
12
+ 10. attributes= setting columns, attributes always worked out from columns; rows read afterwards keyed by those names.
13
+ 11. each yields each row as it is read.
14
+ 12. each returns its receiver.
15
+ 13. rows no longer filled by each.
16
+ 14. Chunks from each_slice.
17
+ 15. The source read once, start to end, with no rewind; the header from the first row read.
18
+ 16. The line of an error found by seeking only where the source can.
19
+ 17. A rewind left to r+ rewriting and to first_row called alone.
data/lib/DSV/Error.rb ADDED
@@ -0,0 +1,8 @@
1
+ # DSV/Error.rb
2
+ # DSV::Error
3
+
4
+ class DSV
5
+ # The base of what dsv raises about the data, so that one rescue catches all of it.
6
+ class Error < ::StandardError
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ # DSV/UnexpectedRow.rb
2
+ # DSV::UnexpectedRow
3
+
4
+ class DSV
5
+ # A row which contradicts its declared quote mode; mode is the mode in force, and line the one on which the row began, given by the reader once it knows it.
6
+ class UnexpectedRow < Error
7
+ attr_reader :mode
8
+ attr_accessor :line
9
+
10
+ def initialize(mode, line = nil)
11
+ @mode = mode
12
+ @line = line
13
+ super()
14
+ end
15
+
16
+ def message
17
+ "The row beginning on line #{@line} does not keep to quote: #{@mode.inspect}."
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # DSV/UnquotedField.rb
2
+ # DSV::UnquotedField
3
+
4
+ class DSV
5
+ # A field with no quotes, under a quote mode which declares every field quoted.
6
+ class UnquotedField < UnexpectedRow
7
+ def message
8
+ "A field in the row beginning on line #{@line} is not quoted, as quote: #{@mode.inspect} declares every field to be."
9
+ end
10
+ end
11
+ end
@@ -3,7 +3,7 @@
3
3
 
4
4
  class DSV
5
5
  # A quoted field which does not close by the end of the source; line is the one on which its row began.
6
- class UnterminatedQuote < ::StandardError
6
+ class UnterminatedQuote < Error
7
7
  attr_reader :line
8
8
 
9
9
  def initialize(line)
data/lib/DSV/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # DSV::VERSION
3
3
 
4
4
  class DSV
5
- VERSION = '0.12.15'
5
+ VERSION = '0.13.0'
6
6
  end
data/lib/dsv.rb CHANGED
@@ -8,7 +8,10 @@ require 'stringio'
8
8
  require 'DSV/VERSION'
9
9
  require 'DSV/File'
10
10
  require 'DSV/String'
11
+ require 'DSV/Error'
11
12
  require 'DSV/UnterminatedQuote'
13
+ require 'DSV/UnexpectedRow'
14
+ require 'DSV/UnquotedField'
12
15
 
13
16
  class DSV
14
17
 
@@ -224,6 +227,9 @@ class DSV
224
227
  nil
225
228
  end
226
229
  )
230
+ rescue UnexpectedRow => error
231
+ error.line = 1
232
+ raise
227
233
  end
228
234
 
229
235
  def columns=(*column_order)
@@ -268,14 +274,14 @@ class DSV
268
274
  value.nil? || value.empty?
269
275
  end
270
276
 
271
- # One row into its fields, by the quote mode: :none splits; :double splits a row as it promises, wholly quoted or wholly unquoted, and one that is neither as unspecified would; unspecified splits and reassembles a quoted field that held the separator, a doubled quote inside it reading as one. nil where a quoted field in the row is still open at its end. Direct string operations throughout; the scanner-based parser is on the branch scanner-parser.
277
+ # One row into its fields, by the quote mode: :none splits; :double splits a row as it promises, every field quoted, and raises UnquotedField on one which is not; unspecified splits and reassembles a quoted field that held the separator, a doubled quote inside it reading as one. nil where a quoted field in the row is still open at its end. Direct string operations throughout; the scanner-based parser is on the branch scanner-parser.
272
278
  def split_row(raw_row)
273
279
  row = raw_row.chomp(@row_separator)
274
280
  case @quote&.to_sym
275
281
  when :none, :unquoted
276
282
  split_on(row, @column_separator)
277
283
  when :double, :double_quoted, :double_quotes
278
- split_double_quoted(row) || split_quoted(row)
284
+ split_double_quoted(row)
279
285
  else
280
286
  split_quoted(row)
281
287
  end
@@ -311,16 +317,44 @@ class DSV
311
317
  end
312
318
  end
313
319
 
314
- # The :double contract: a wholly quoted row split on quote-separator-quote, a wholly unquoted one plainly; nil for a row that is neither, or whose last field does not close.
320
+ # The :double contract, every field quoted: the row split on quote-separator-quote, a closed last field ending in an odd run of quotes, counted from the last piece, whose opening quote the split has already taken. Read in full only where the last field seems open, as it is where it goes on to the next line, or where the split leaves a lone quote in a field, as a field holding quote-separator-quote does. A row not starting with a quote raises UnquotedField, at the cost of one byte compared. nil where the last field does not close.
315
321
  def split_double_quoted(row)
316
- if row.start_with?('"')
317
- unescaped_fields(split_on(row.delete_prefix('"').delete_suffix('"'), quoted_separator)) if trailing_quotes(row, 0).odd?
318
- else
319
- split_on(row, @column_separator) unless row.include?('"')
322
+ raise UnquotedField.new(:double) unless row.start_with?('"')
323
+ fields = split_on(row.delete_prefix('"'), quoted_separator)
324
+ return reassemble_wholly_quoted_fields(row) unless fields.last && trailing_quotes(fields.last, -1).odd?
325
+ fields[-1] = fields.last.delete_suffix('"')
326
+ unescaped_fields(fields) || reassemble_wholly_quoted_fields(row)
327
+ end
328
+
329
+ # A row under :double whose split left a lone quote in a field, read in full as unspecified reads one, except that a field which is not quoted raises UnquotedField. nil where a quoted field is still open after the last piece.
330
+ def reassemble_wholly_quoted_fields(row)
331
+ pieces, separators = (
332
+ if @column_separator.is_a?(::Regexp)
333
+ pieces_and_separators(row, @column_separator)
334
+ else
335
+ [split_on(row, @column_separator), nil]
336
+ end
337
+ )
338
+ fields = []
339
+ buffer = nil
340
+ pieces.each_with_index do |piece, i|
341
+ if buffer && trailing_quotes(piece, -1).even?
342
+ buffer << separator_before(i, separators) << piece
343
+ elsif buffer
344
+ fields << (buffer << separator_before(i, separators) << piece.delete_suffix('"')).gsub('""', '"')
345
+ buffer = nil
346
+ elsif !piece.start_with?('"')
347
+ raise UnquotedField.new(:double)
348
+ elsif trailing_quotes(piece, 0).even?
349
+ buffer = +piece.delete_prefix('"')
350
+ else
351
+ fields << piece.delete_prefix('"').delete_suffix('"').gsub('""', '"')
352
+ end
320
353
  end
354
+ fields unless buffer
321
355
  end
322
356
 
323
- # The fields of a row split as wholly quoted, a doubled quote read as one; nil where a field holds a lone quote, the mark of a row that was not. A field has twice the quotes it has unescaped exactly when every quote in it is one of a doubled pair.
357
+ # The fields of a row split as wholly quoted, a doubled quote read as one; nil where a field holds a lone quote, which the split alone cannot place. A field has twice the quotes it has unescaped exactly when every quote in it is one of a doubled pair.
324
358
  def unescaped_fields(fields)
325
359
  fields.collect do |field|
326
360
  next field unless field.include?('"')
@@ -374,10 +408,13 @@ class DSV
374
408
  separators ? separators[i - 1] : @column_separator
375
409
  end
376
410
 
377
- # A row's fields, the next line taken into the row while a quoted field in it is open, a quoted field being able to hold the row separator; a field still open at the end of the source raises UnterminatedQuote.
411
+ # A row's fields, the next line taken into the row while a quoted field in it is open, a quoted field being able to hold the row separator; a field still open at the end of the source raises UnterminatedQuote. A row which contradicts its mode is given its line here, where the line can be found, and only then.
378
412
  def row_fields(raw_row)
379
413
  raw_row << @source.gets(@row_separator) until (fields = split_row(raw_row)) || @source.eof?
380
414
  fields || raise(UnterminatedQuote.new(line_of(raw_row)))
415
+ rescue UnexpectedRow => error
416
+ error.line = line_of(raw_row)
417
+ raise
381
418
  end
382
419
 
383
420
  # The line on which a row read up to the source's position began, counted only when an error wants it, by reading the source again up to the row, so that no row which closes pays for it.
@@ -389,6 +426,9 @@ class DSV
389
426
 
390
427
  def parse_row(raw_row, *selected_columns)
391
428
  parse_fields(split_row(raw_row) || raise(UnterminatedQuote.new(1)), *selected_columns)
429
+ rescue UnexpectedRow => error
430
+ error.line = 1
431
+ raise
392
432
  end
393
433
 
394
434
  def parse_fields(fields, *selected_columns)
data/test/dsv_test.rb CHANGED
@@ -522,7 +522,7 @@ describe DSV do
522
522
  _(DSV.read("a b\n", column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
523
523
  _(DSV.read("\"x y\" 2\n", column_separator: ' ')).must_equal [{0 => 'x y', 1 => '', 2 => '2'}]
524
524
  _(DSV.read("\"a\" \"\" \"b\"\n", quote: :double, column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
525
- _(DSV.read("a b\n", quote: :double, column_separator: ' ')).must_equal [{0 => 'a', 1 => '', 2 => 'b'}]
525
+ _{DSV.read("a b\n", quote: :double, column_separator: ' ')}.must_raise DSV::UnquotedField
526
526
  end
527
527
 
528
528
  it "does not split on a tab where the separator is a space" do
@@ -587,9 +587,10 @@ describe DSV do
587
587
  _(DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n", headers: true, quote: 'double')).must_equal [{'a' => 'x', 'b' => '2'}]
588
588
  end
589
589
 
590
- it "strips the quotes under quote: :double, a row being wholly quoted or, as a header row often is, wholly unquoted" do
591
- _(DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x', 'b' => '2'}]
590
+ it "strips the quotes under quote: :double, every row wholly quoted, the header included" do
591
+ _(DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x', 'b' => '2'}]
592
592
  _(DSV.read("\"a\",\"b\"\n\"say \"\"hi\"\"\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'say "hi"', 'b' => '2'}]
593
+ _(DSV.read("\"a\",\"\"\n", quote: :double)).must_equal [{0 => 'a', 1 => ''}]
593
594
  end
594
595
 
595
596
  it "keeps every position of a repeated header name in columns, the name at each position in attributes" do
@@ -627,19 +628,41 @@ describe DSV do
627
628
  end
628
629
 
629
630
  it "keeps a separator inside a quoted field under quote: :double" do
630
- _(DSV.read("a,b\n\"x, y\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x, y', 'b' => '2'}]
631
+ _(DSV.read("\"a\",\"b\"\n\"x, y\",\"2\"\n", headers: true, quote: :double)).must_equal [{'a' => 'x, y', 'b' => '2'}]
631
632
  end
632
633
 
633
- it "reads a row that is neither wholly quoted nor wholly unquoted under quote: :double as it would unspecified" do
634
- _(DSV.read("\"a\",b,\"c\"\n", quote: :double)).must_equal [{0 => 'a', 1 => 'b', 2 => 'c'}]
635
- _(DSV.read("1,\"x\"\n", quote: :double)).must_equal [{0 => '1', 1 => 'x'}]
636
- _(DSV.read("\"a\",\"b\",c\n", quote: :double)).must_equal [{0 => 'a', 1 => 'b', 2 => 'c'}]
637
- _(DSV.read("a,b\n1,\"x, y\"\n", headers: true, quote: :double)).must_equal [{'a' => '1', 'b' => 'x, y'}]
634
+ it "reads a field holding quote-separator-quote under quote: :double, the row read in full" do
635
+ _(DSV.read("\"a\"\",\"\"b\",\"c\"\n", quote: :double)).must_equal [{0 => 'a","b', 1 => 'c'}]
636
+ end
637
+
638
+ it "raises UnquotedField on a row under quote: :double with a field not quoted, where 0.12.5 to 0.12.15 read it as unspecified" do
639
+ _{DSV.read("\"a\",b,\"c\"\n", quote: :double)}.must_raise DSV::UnquotedField
640
+ _{DSV.read("1,\"x\"\n", quote: :double)}.must_raise DSV::UnquotedField
641
+ _{DSV.read("\"a\",\"b\",c\n", quote: :double)}.must_raise DSV::UnquotedField
642
+ _{DSV.read("\"a\",,\"c\"\n", quote: :double)}.must_raise DSV::UnquotedField
643
+ _{DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)}.must_raise DSV::UnquotedField
644
+ _{DSV.parse_line('"a",b', quote: :double)}.must_raise DSV::UnquotedField
645
+ end
646
+
647
+ it "names the line and the mode of a row which breaks quote: :double" do
648
+ error = _{DSV.read("\"a\",\"b\"\n\"x\",\"2\"\n\"y\",3\n", quote: :double)}.must_raise DSV::UnquotedField
649
+ _(error.line).must_equal 3
650
+ _(error.mode).must_equal :double
651
+ _(error.message).must_include 'line 3'
652
+ error = _{DSV.read("a,b\n\"x\",\"2\"\n", headers: true, quote: :double)}.must_raise DSV::UnquotedField
653
+ _(error.line).must_equal 1
654
+ end
655
+
656
+ it "raises errors about the data under DSV::Error, UnquotedField under UnexpectedRow" do
657
+ _(DSV::UnquotedField.ancestors).must_include DSV::UnexpectedRow
658
+ _(DSV::UnexpectedRow.ancestors).must_include DSV::Error
659
+ _(DSV::UnterminatedQuote.ancestors).must_include DSV::Error
660
+ _{DSV.read("\"a\",b\n", quote: :double)}.must_raise DSV::Error
638
661
  end
639
662
 
640
663
  it "reads a quoted field holding the row separator as one field across lines under quote: :double, as DSV writes it" do
641
664
  _(DSV.read("\"x\ny\",\"2\"\n\"z\",\"3\"\n", quote: :double)).must_equal [{0 => "x\ny", 1 => '2'}, {0 => 'z', 1 => '3'}]
642
- _(DSV.read("1,\"x\ny\"\n", quote: :double)).must_equal [{0 => '1', 1 => "x\ny"}]
665
+ _(DSV.read("\"a\",\"x\n,y\",\"z\"\n", quote: :double)).must_equal [{0 => 'a', 1 => "x\n,y", 2 => 'z'}]
643
666
  output = written(headers: true, columns: ['a', 'b']){|csv| csv.rows = [{'a' => "x\ny", 'b' => '2'}]; csv.write}
644
667
  _(DSV.read(output, headers: true, quote: :double)).must_equal [{'a' => "x\ny", 'b' => '2'}]
645
668
  end
@@ -654,18 +677,19 @@ describe DSV do
654
677
 
655
678
  it "reads a stray quote in an unquoted field as data, without taking in the next line" do
656
679
  _(DSV.read("12\" pizza,9.99\nnext,row\n", as_array: true)).must_equal [['12" pizza', '9.99'], ['next', 'row']]
657
- _(DSV.read("12\" pizza,9.99\nnext,row\n", as_array: true, quote: :double)).must_equal [['12" pizza', '9.99'], ['next', 'row']]
680
+ _{DSV.read("12\" pizza,9.99\nnext,row\n", as_array: true, quote: :double)}.must_raise DSV::UnquotedField
658
681
  end
659
682
 
660
683
  it "reads a quoted field across lines after a stray quote in the same row" do
661
684
  _(DSV.read("a\"b,\"c\nd\"\n", as_array: true)).must_equal [['a"b', "c\nd"]]
662
- _(DSV.read("a\"b,\"c\nd\"\n", as_array: true, quote: :double)).must_equal [['a"b', "c\nd"]]
685
+ _{DSV.read("a\"b,\"c\nd\"\n", as_array: true, quote: :double)}.must_raise DSV::UnquotedField
663
686
  end
664
687
 
665
688
  it "raises UnterminatedQuote on a quoted field which does not close" do
666
689
  _{DSV.read("\"abc,d\n")}.must_raise DSV::UnterminatedQuote
667
690
  _{DSV.read("\"ab\"c,d\n")}.must_raise DSV::UnterminatedQuote
668
- _{DSV.read("x,\"abc\n", quote: :double)}.must_raise DSV::UnterminatedQuote
691
+ _{DSV.read("\"x\",\"abc\n", quote: :double)}.must_raise DSV::UnterminatedQuote
692
+ _{DSV.read("\"x\",\"y\"\",\"\"\n", quote: :double)}.must_raise DSV::UnterminatedQuote
669
693
  _{DSV.parse_line('x,"abc')}.must_raise DSV::UnterminatedQuote
670
694
  end
671
695
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dsv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.15
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -65,8 +65,11 @@ files:
65
65
  - Rakefile
66
66
  - TODO
67
67
  - dsv.gemspec
68
+ - lib/DSV/Error.rb
68
69
  - lib/DSV/File.rb
69
70
  - lib/DSV/String.rb
71
+ - lib/DSV/UnexpectedRow.rb
72
+ - lib/DSV/UnquotedField.rb
70
73
  - lib/DSV/UnterminatedQuote.rb
71
74
  - lib/DSV/VERSION.rb
72
75
  - lib/dsv.rb