From 2c66c299b9cf8eea7078827fff6414237b52d7fd Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Mon, 16 Oct 2017 20:03:39 +0300 Subject: [PATCH 1/4] Add specs for CSV#liberal_parsing? --- library/csv/liberal_parsing_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 library/csv/liberal_parsing_spec.rb diff --git a/library/csv/liberal_parsing_spec.rb b/library/csv/liberal_parsing_spec.rb new file mode 100644 index 0000000000..99cedbcd22 --- /dev/null +++ b/library/csv/liberal_parsing_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'csv' + +ruby_version_is '2.4' do + describe "CSV#liberal_parsing?" do + it "returns true if illegal input is handled" do + csv = CSV.new("", liberal_parsing: true) + csv.liberal_parsing?.should == true + end + + it "returns false if illegal input is not handled" do + csv = CSV.new("", liberal_parsing: false) + csv.liberal_parsing?.should == false + end + + it "returns false by default" do + csv = CSV.new("") + csv.liberal_parsing?.should == false + end + end +end From db7a1b03f3fbd59e859116556d13e3e0a4ab0239 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Mon, 16 Oct 2017 20:04:42 +0300 Subject: [PATCH 2/4] Add spec for CSV.parse to test parsing illegal input --- library/csv/parse_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/csv/parse_spec.rb b/library/csv/parse_spec.rb index 41d37ca9a4..039e139b66 100644 --- a/library/csv/parse_spec.rb +++ b/library/csv/parse_spec.rb @@ -78,4 +78,18 @@ result = CSV.parse "foo;bar\nbaz;quz", col_sep: ?; result.should == [['foo','bar'],['baz','quz']] end + + it "raises CSV::MalformedCSVError exception if input is illegal" do + -> { + CSV.parse('"quoted" field') + }.should raise_error(CSV::MalformedCSVError) + end + + ruby_version_is '2.4' do + it "handles illegal input" do + illegal_input = '"Johnson, Dwayne",Dwayne "The Rock" Johnson' + result = CSV.parse(illegal_input, liberal_parsing: true) + result.should == [["Johnson, Dwayne", 'Dwayne "The Rock" Johnson']] + end + end end From a1bd8abe3b423544af3a362614feaa77ef794909 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Mon, 16 Oct 2017 20:05:12 +0300 Subject: [PATCH 3/4] Add spec for CSV#readlines to test parsing illegal input --- library/csv/readlines_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/csv/readlines_spec.rb b/library/csv/readlines_spec.rb index 387730db16..1a5d450d23 100644 --- a/library/csv/readlines_spec.rb +++ b/library/csv/readlines_spec.rb @@ -20,4 +20,18 @@ file = CSV.new "a,, b, c" file.readlines.should == [["a", nil, " b", " c"]] end + + it "raises CSV::MalformedCSVError exception if input is illegal" do + csv = CSV.new('"quoted" field') + -> { csv.readlines }.should raise_error(CSV::MalformedCSVError) + end + + ruby_version_is '2.4' do + it "handles illegal input" do + illegal_input = '"Johnson, Dwayne",Dwayne "The Rock" Johnson' + csv = CSV.new(illegal_input, liberal_parsing: true) + result = csv.readlines + result.should == [["Johnson, Dwayne", 'Dwayne "The Rock" Johnson']] + end + end end From b38c1791b2c949abbc442ccbbee478bcbb1aba79 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Fri, 20 Oct 2017 10:16:48 +0300 Subject: [PATCH 4/4] Code review. Update case descriptions --- library/csv/parse_spec.rb | 2 +- library/csv/readlines_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/csv/parse_spec.rb b/library/csv/parse_spec.rb index 039e139b66..47d7ebbde1 100644 --- a/library/csv/parse_spec.rb +++ b/library/csv/parse_spec.rb @@ -86,7 +86,7 @@ end ruby_version_is '2.4' do - it "handles illegal input" do + it "handles illegal input with the liberal_parsing option" do illegal_input = '"Johnson, Dwayne",Dwayne "The Rock" Johnson' result = CSV.parse(illegal_input, liberal_parsing: true) result.should == [["Johnson, Dwayne", 'Dwayne "The Rock" Johnson']] diff --git a/library/csv/readlines_spec.rb b/library/csv/readlines_spec.rb index 1a5d450d23..452effd343 100644 --- a/library/csv/readlines_spec.rb +++ b/library/csv/readlines_spec.rb @@ -27,7 +27,7 @@ end ruby_version_is '2.4' do - it "handles illegal input" do + it "handles illegal input with the liberal_parsing option" do illegal_input = '"Johnson, Dwayne",Dwayne "The Rock" Johnson' csv = CSV.new(illegal_input, liberal_parsing: true) result = csv.readlines