Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bin/csv-filter
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ parser.on('-c', '--col-sep=SEPARATOR',
options[:col_sep] = value
end

parser.on('-q', '--quote-char=CHAR',
'Quote character.') do |value|
options[:quote_char] = value
end

begin
parser.parse!
rescue OptionParser::InvalidOption
Expand Down
15 changes: 15 additions & 0 deletions test/csv/test_csv_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,19 @@ def test_option_col_sep
assert_equal(["aaa.bbb.ccc\nddd.eee.fff\n", ""],
run_csv_filter(csv, "--col-sep=:", "--output-col-sep=."))
end

def test_option_quote_char
quote_char = "'"
csv = CSV.generate(quote_char: quote_char) do |csv|
csv << ['foo', 0]
csv << ["'bar'", 1]
csv << ['"baz"', 2]
end
Comment on lines +142 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
quote_char = "'"
csv = CSV.generate(quote_char: quote_char) do |csv|
csv << ['foo', 0]
csv << ["'bar'", 1]
csv << ['"baz"', 2]
end
csv = "foo,0\n'''bar''',1\n\"baz\",2\n"

assert_equal(["foo,0\n'''bar''',1\n\"baz\",2\n", ""],
run_csv_filter(csv, "--quote-char=:"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is --quote-char=: a good test?
It does nothing, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have the same situation for --col-sep=: and --row-sep=:, don't we? They all at least test that the option is accepted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really?

They use : in the input CSV:

csv = "aaa:bbb:ccc\nddd:eee:fff\n"
assert_equal(["aaa:bbb:ccc\nddd:eee:fff\n", ""],
run_csv_filter(csv, "--col-sep=:"))

csv = "aaa,bbb,ccc:ddd,eee,fff:"
assert_equal(["aaa,bbb,ccc:ddd,eee,fff:", ""],
run_csv_filter(csv, "--row-sep=:"))

assert_equal(["foo,0\n'''bar''',1\n\"\"\"baz\"\"\",2\n", ""],
run_csv_filter(csv, "--input-quote-char=:"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we specify --quote-char=... too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. Doesn't the default value serve us well enough?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--row-sep tests use --row-sep and --{input,output}-row-sep.
I think that we want to test that --quote-char configuration is overridden by --{input,output}-quote-char here. Is it incorrect?

If we want to test only --{input,output}-quote-char, we should do it in the test for --{input,output}-quote-char not the test for --quote-char.

assert_equal(["foo,0\n'''bar''',1\nbaz,2\n", ""],
run_csv_filter(csv, "--output-quote-char=:"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

end
end
Loading