summaryrefslogtreecommitdiff
path: root/test/csv/interface/test_read.rb
diff options
context:
space:
mode:
authorKoichi ITO <koic.ito@gmail.com>2020-06-04 12:33:47 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-07-20 02:32:52 +0900
commit08e70126aeca4edcdeab7dc292acda2f50cc41a1 (patch)
treead573ea15c27d271f9de0902bad6edf5b377c8c6 /test/csv/interface/test_read.rb
parentcee10c1b7033f4682b3f4c76220c0c190e70aa1a (diff)
[ruby/csv] Add `invalid: :replace` for `CSV.open` (#130)
This PR adds `invalid: :replace` for `CSV.open`. It is a PR similar to #129. https://github.com/ruby/csv/commit/5bf687341c
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3332
Diffstat (limited to 'test/csv/interface/test_read.rb')
-rw-r--r--test/csv/interface/test_read.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/csv/interface/test_read.rb b/test/csv/interface/test_read.rb
index a2bcceda88c..b86c54fc9f6 100644
--- a/test/csv/interface/test_read.rb
+++ b/test/csv/interface/test_read.rb
@@ -135,6 +135,36 @@ class TestCSVInterfaceRead < Test::Unit::TestCase
end
end
+ def test_open_with_invalid_nil
+ CSV.open(@input.path, "w", encoding: Encoding::CP932, invalid: nil) do |rows|
+ error = assert_raise(Encoding::InvalidByteSequenceError) do
+ rows << ["\x82\xa0"]
+ end
+ assert_equal('"\x82" on UTF-8',
+ error.message)
+ end
+ end
+
+ def test_open_with_invalid_replace
+ CSV.open(@input.path, "w", encoding: Encoding::CP932, invalid: :replace) do |rows|
+ rows << ["\x82\xa0".force_encoding(Encoding::UTF_8)]
+ end
+ CSV.open(@input.path, encoding: Encoding::CP932) do |csv|
+ assert_equal([["??"]],
+ csv.to_a)
+ end
+ end
+
+ def test_open_with_invalid_replace_and_replace_string
+ CSV.open(@input.path, "w", encoding: Encoding::CP932, invalid: :replace, replace: "X") do |rows|
+ rows << ["\x82\xa0".force_encoding(Encoding::UTF_8)]
+ end
+ CSV.open(@input.path, encoding: Encoding::CP932) do |csv|
+ assert_equal([["XX"]],
+ csv.to_a)
+ end
+ end
+
def test_open_with_undef_replace
# U+00B7 Middle Dot
CSV.open(@input.path, "w", encoding: Encoding::CP932, undef: :replace) do |rows|