aboutsummaryrefslogtreecommitdiffstats
path: root/test/csv/test_interface.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/csv/test_interface.rb')
-rwxr-xr-xtest/csv/test_interface.rb56
1 files changed, 53 insertions, 3 deletions
diff --git a/test/csv/test_interface.rb b/test/csv/test_interface.rb
index be27fcd616..912e2ec7f5 100755
--- a/test/csv/test_interface.rb
+++ b/test/csv/test_interface.rb
@@ -4,9 +4,7 @@
# tc_interface.rb
#
-# Created by James Edward Gray II on 2005-10-31.
-# Copyright 2005 James Edward Gray II. You can redistribute or modify this code
-# under the terms of Ruby's license.
+# Created by James Edward Gray II on 2005-10-31.
require_relative "base"
require "tempfile"
@@ -64,6 +62,55 @@ class TestCSV::Interface < TestCSV
assert_equal("Return value.", ret)
end
+ def test_open_encoding_valid
+ # U+1F600 GRINNING FACE
+ # U+1F601 GRINNING FACE WITH SMILING EYES
+ File.open(@path, "w") do |file|
+ file << "\u{1F600},\u{1F601}"
+ end
+ CSV.open(@path, encoding: "utf-8") do |csv|
+ assert_equal([["\u{1F600}", "\u{1F601}"]],
+ csv.to_a)
+ end
+ end
+
+ def test_open_encoding_invalid
+ # U+1F600 GRINNING FACE
+ # U+1F601 GRINNING FACE WITH SMILING EYES
+ File.open(@path, "w") do |file|
+ file << "\u{1F600},\u{1F601}"
+ end
+ CSV.open(@path, encoding: "EUC-JP") do |csv|
+ error = assert_raise(CSV::MalformedCSVError) do
+ csv.shift
+ end
+ assert_equal("Invalid byte sequence in EUC-JP in line 1.",
+ error.message)
+ end
+ end
+
+ def test_open_encoding_nonexistent
+ _output, error = capture_io do
+ CSV.open(@path, encoding: "nonexistent") do
+ end
+ end
+ assert_equal("path:0: warning: Unsupported encoding nonexistent ignored\n",
+ error.gsub(/\A.+:\d+: /, "path:0: "))
+ end
+
+ def test_open_encoding_utf_8_with_bom
+ # U+FEFF ZERO WIDTH NO-BREAK SPACE, BOM
+ # U+1F600 GRINNING FACE
+ # U+1F601 GRINNING FACE WITH SMILING EYES
+ File.open(@path, "w") do |file|
+ file << "\u{FEFF}\u{1F600},\u{1F601}"
+ end
+ CSV.open(@path, encoding: "bom|utf-8") do |csv|
+ assert_equal([["\u{1F600}", "\u{1F601}"]],
+ csv.to_a)
+ end
+ end
+
def test_parse
data = File.binread(@path)
assert_equal( @expected,
@@ -161,6 +208,9 @@ class TestCSV::Interface < TestCSV
assert_equal(csv, csv << ["last", %Q{"row"}])
end
assert_equal(%Q{1,2,3\n4,,5\nlast,"""row"""\n}, str)
+
+ out = CSV.generate("test") { |csv| csv << ["row"] }
+ assert_equal("testrow\n", out)
end
def test_generate_line