1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
# frozen_string_literal: false
require_relative "../helper"
class TestCSVInterfaceRead < Test::Unit::TestCase
extend DifferentOFS
def setup
super
@data = ""
@data << "1\t2\t3\r\n"
@data << "4\t5\r\n"
@input = Tempfile.new(["interface-read", ".csv"], binmode: true)
@input << @data
@input.rewind
@rows = [
["1", "2", "3"],
["4", "5"],
]
end
def teardown
@input.close(true)
super
end
def test_foreach
rows = []
CSV.foreach(@input.path, col_sep: "\t", row_sep: "\r\n").each do |row|
rows << row
end
assert_equal(@rows, rows)
end
def test_foreach_mode
rows = []
CSV.foreach(@input.path, "r", col_sep: "\t", row_sep: "\r\n").each do |row|
rows << row
end
assert_equal(@rows, rows)
end
def test_foreach_enumurator
rows = CSV.foreach(@input.path, col_sep: "\t", row_sep: "\r\n").to_a
assert_equal(@rows, rows)
end
def test_closed?
csv = CSV.open(@input.path, "r+", col_sep: "\t", row_sep: "\r\n")
assert_not_predicate(csv, :closed?)
csv.close
assert_predicate(csv, :closed?)
end
def test_open_auto_close
csv = nil
CSV.open(@input.path) do |_csv|
csv = _csv
end
assert_predicate(csv, :closed?)
end
def test_open_closed
csv = nil
CSV.open(@input.path) do |_csv|
csv = _csv
csv.close
end
assert_predicate(csv, :closed?)
end
def test_open_block_return_value
return_value = CSV.open(@input.path) do
"Return value."
end
assert_equal("Return value.", return_value)
end
def test_open_encoding_valid
# U+1F600 GRINNING FACE
# U+1F601 GRINNING FACE WITH SMILING EYES
File.open(@input.path, "w") do |file|
file << "\u{1F600},\u{1F601}"
end
CSV.open(@input.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(@input.path, "w") do |file|
file << "\u{1F600},\u{1F601}"
end
CSV.open(@input.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_output do
CSV.open(@input.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(@input.path, "w") do |file|
file << "\u{FEFF}\u{1F600},\u{1F601}"
end
CSV.open(@input.path, encoding: "bom|utf-8") do |csv|
assert_equal([["\u{1F600}", "\u{1F601}"]],
csv.to_a)
end
end
def test_parse
assert_equal(@rows,
CSV.parse(@data, col_sep: "\t", row_sep: "\r\n"))
end
def test_parse_block
rows = []
CSV.parse(@data, col_sep: "\t", row_sep: "\r\n") do |row|
rows << row
end
assert_equal(@rows, rows)
end
def test_parse_enumerator
rows = CSV.parse(@data, col_sep: "\t", row_sep: "\r\n").to_a
assert_equal(@rows, rows)
end
def test_parse_headers_only
table = CSV.parse("a,b,c", headers: true)
assert_equal([
["a", "b", "c"],
[],
],
[
table.headers,
table.each.to_a,
])
end
def test_parse_line
assert_equal(["1", "2", "3"],
CSV.parse_line("1;2;3", col_sep: ";"))
end
def test_parse_line_shortcut
assert_equal(["1", "2", "3"],
"1;2;3".parse_csv(col_sep: ";"))
end
def test_parse_line_empty
assert_equal(nil, CSV.parse_line("")) # to signal eof
end
def test_parse_line_empty_line
assert_equal([], CSV.parse_line("\n1,2,3"))
end
def test_read
assert_equal(@rows,
CSV.read(@input.path, col_sep: "\t", row_sep: "\r\n"))
end
def test_readlines
assert_equal(@rows,
CSV.readlines(@input.path, col_sep: "\t", row_sep: "\r\n"))
end
def test_open_read
rows = CSV.open(@input.path, col_sep: "\t", row_sep: "\r\n") do |csv|
csv.read
end
assert_equal(@rows, rows)
end
def test_open_readlines
rows = CSV.open(@input.path, col_sep: "\t", row_sep: "\r\n") do |csv|
csv.readlines
end
assert_equal(@rows, rows)
end
def test_table
table = CSV.table(@input.path, col_sep: "\t", row_sep: "\r\n")
assert_equal(CSV::Table.new([
CSV::Row.new([:"1", :"2", :"3"], [4, 5, nil]),
]),
table)
end
def test_shift # aliased as gets() and readline()
CSV.open(@input.path, "rb+", col_sep: "\t", row_sep: "\r\n") do |csv|
rows = [
csv.shift,
csv.shift,
csv.shift,
]
assert_equal(@rows + [nil],
rows)
end
end
def test_enumerator
CSV.open(@input.path, col_sep: "\t", row_sep: "\r\n") do |csv|
assert_equal(@rows, csv.each.to_a)
end
end
def test_shift_and_each
CSV.open(@input.path, col_sep: "\t", row_sep: "\r\n") do |csv|
rows = []
rows << csv.shift
rows.concat(csv.each.to_a)
assert_equal(@rows, rows)
end
end
def test_each_twice
CSV.open(@input.path, col_sep: "\t", row_sep: "\r\n") do |csv|
assert_equal([
@rows,
[],
],
[
csv.each.to_a,
csv.each.to_a,
])
end
end
def test_eof?
eofs = []
CSV.open(@input.path, col_sep: "\t", row_sep: "\r\n") do |csv|
eofs << csv.eof?
csv.shift
eofs << csv.eof?
csv.shift
eofs << csv.eof?
end
assert_equal([false, false, true],
eofs)
end
def test_new_nil
assert_raise_with_message ArgumentError, "Cannot parse nil as CSV" do
CSV.new(nil)
end
end
def test_options_not_modified
options = {}.freeze
CSV.foreach(@input.path, **options)
CSV.open(@input.path, **options) {}
CSV.parse("", **options)
CSV.parse_line("", **options)
CSV.read(@input.path, **options)
CSV.readlines(@input.path, **options)
CSV.table(@input.path, **options)
end
end
|