зеркало из https://github.com/github/ruby.git
* test/csv/test_data_converters.rb: use descriptive assertions.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45223 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
0b8233ab07
Коммит
2d89896461
|
@ -68,7 +68,7 @@ class TestCSV::DataConverters < TestCSV
|
|||
|
||||
def test_convert_with_builtin_integer
|
||||
# setup parser...
|
||||
assert(@parser.respond_to?(:convert))
|
||||
assert_respond_to(@parser, :convert)
|
||||
assert_nothing_raised(Exception) { @parser.convert(:integer) }
|
||||
|
||||
# and use
|
||||
|
@ -77,7 +77,7 @@ class TestCSV::DataConverters < TestCSV
|
|||
|
||||
def test_convert_with_builtin_float
|
||||
# setup parser...
|
||||
assert(@parser.respond_to?(:convert))
|
||||
assert_respond_to(@parser, :convert)
|
||||
assert_nothing_raised(Exception) { @parser.convert(:float) }
|
||||
|
||||
# and use
|
||||
|
|
|
@ -121,11 +121,9 @@ class TestCSV::Encodings < TestCSV
|
|||
def test_parser_works_with_encoded_headers
|
||||
encode_for_tests([%w[one two three], %w[1 2 3]]) do |data|
|
||||
parsed = CSV.parse(data, headers: true)
|
||||
assert( parsed.headers.all? { |h| h.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(parsed.headers, "Wrong data encoding.") {|h| h.encoding == data.encoding}
|
||||
parsed.each do |row|
|
||||
assert( row.fields.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(row.fields, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -133,8 +131,7 @@ class TestCSV::Encodings < TestCSV
|
|||
def test_built_in_converters_transcode_to_utf_8_then_convert
|
||||
encode_for_tests([%w[one two three], %w[1 2 3]]) do |data|
|
||||
parsed = CSV.parse(data, converters: :integer)
|
||||
assert( parsed[0].all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(parsed[0], "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
assert_equal([1, 2, 3], parsed[1])
|
||||
end
|
||||
end
|
||||
|
@ -143,10 +140,8 @@ class TestCSV::Encodings < TestCSV
|
|||
encode_for_tests([%w[one two three], %w[1 2 3]]) do |data|
|
||||
parsed = CSV.parse( data, headers: true,
|
||||
header_converters: :downcase )
|
||||
assert( parsed.headers.all? { |h| h.encoding.name == "UTF-8" },
|
||||
"Wrong data encoding." )
|
||||
assert( parsed[0].fields.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(parsed.headers, "Wrong data encoding.") {|h| h.encoding.name == "UTF-8"}
|
||||
assert_all?(parsed[0].fields, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -156,8 +151,7 @@ class TestCSV::Encodings < TestCSV
|
|||
File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data }
|
||||
CSV.open(@temp_csv_path, "rb:#{data.encoding.name}") do |csv|
|
||||
csv.each do |row|
|
||||
assert( row.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(row, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -167,8 +161,7 @@ class TestCSV::Encodings < TestCSV
|
|||
end
|
||||
CSV.open(@temp_csv_path, "rb:UTF-32BE:#{data.encoding.name}") do |csv|
|
||||
csv.each do |row|
|
||||
assert( row.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(row, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -188,8 +181,7 @@ class TestCSV::Encodings < TestCSV
|
|||
end
|
||||
CSV.foreach( @temp_csv_path,
|
||||
encoding: "UTF-32BE:#{data.encoding.name}" ) do |row|
|
||||
assert( row.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(row, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -199,8 +191,7 @@ class TestCSV::Encodings < TestCSV
|
|||
# read and write in encoding
|
||||
File.open(@temp_csv_path, "wb:#{data.encoding.name}") { |f| f << data }
|
||||
rows = CSV.read(@temp_csv_path, encoding: data.encoding.name)
|
||||
assert( rows.flatten.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(rows.flatten, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
|
||||
# read and write with transcoding
|
||||
File.open(@temp_csv_path, "wb:UTF-32BE:#{data.encoding.name}") do |f|
|
||||
|
@ -208,8 +199,7 @@ class TestCSV::Encodings < TestCSV
|
|||
end
|
||||
rows = CSV.read( @temp_csv_path,
|
||||
encoding: "UTF-32BE:#{data.encoding.name}" )
|
||||
assert( rows.flatten.all? { |f| f.encoding == data.encoding },
|
||||
"Wrong data encoding." )
|
||||
assert_all?(rows.flatten, "Wrong data encoding.") {|f| f.encoding == data.encoding}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -134,10 +134,9 @@ class TestCSV::Features < TestCSV
|
|||
def test_csv_behavior_readers
|
||||
%w[ unconverted_fields return_headers write_headers
|
||||
skip_blanks force_quotes ].each do |behavior|
|
||||
assert( !CSV.new("abc,def").send("#{behavior}?"),
|
||||
"Behavior defaulted to on." )
|
||||
assert_not_predicate(CSV.new("abc,def"), "#{behavior}?", "Behavior defaulted to on.")
|
||||
csv = CSV.new("abc,def", behavior.to_sym => true)
|
||||
assert(csv.send("#{behavior}?"), "Behavior change now registered.")
|
||||
assert_predicate(csv, "#{behavior}?", "Behavior change now registered.")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -184,9 +183,9 @@ class TestCSV::Features < TestCSV
|
|||
# reported by Chris Roos
|
||||
def test_failing_to_reset_headers_in_rewind_bug_fix
|
||||
csv = CSV.new("forename,surname", headers: true, return_headers: true)
|
||||
csv.each { |row| assert row.header_row? }
|
||||
csv.each {|row| assert_predicate row, :header_row?}
|
||||
csv.rewind
|
||||
csv.each { |row| assert row.header_row? }
|
||||
csv.each {|row| assert_predicate row, :header_row?}
|
||||
end
|
||||
|
||||
# reported by Dave Burt
|
||||
|
@ -223,25 +222,24 @@ class TestCSV::Features < TestCSV
|
|||
zipped << [1, 2, 3]
|
||||
zipped.close
|
||||
|
||||
assert( Zlib::GzipReader.open(file) { |f| f.read }.
|
||||
include?($INPUT_RECORD_SEPARATOR),
|
||||
"@row_sep did not default" )
|
||||
assert_include(Zlib::GzipReader.open(file) {|f| f.read},
|
||||
$INPUT_RECORD_SEPARATOR, "@row_sep did not default")
|
||||
}
|
||||
end if defined?(Zlib::GzipWriter)
|
||||
|
||||
def test_inspect_is_smart_about_io_types
|
||||
str = CSV.new("string,data").inspect
|
||||
assert(str.include?("io_type:StringIO"), "IO type not detected.")
|
||||
assert_include(str, "io_type:StringIO", "IO type not detected.")
|
||||
|
||||
str = CSV.new($stderr).inspect
|
||||
assert(str.include?("io_type:$stderr"), "IO type not detected.")
|
||||
assert_include(str, "io_type:$stderr", "IO type not detected.")
|
||||
|
||||
Tempfile.create(%w"temp .csv") {|tempfile|
|
||||
tempfile.close
|
||||
path = tempfile.path
|
||||
File.open(path, "w") { |csv| csv << "one,two,three\n1,2,3\n" }
|
||||
str = CSV.open(path) { |csv| csv.inspect }
|
||||
assert(str.include?("io_type:File"), "IO type not detected.")
|
||||
assert_include(str, "io_type:File", "IO type not detected.")
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -254,7 +252,7 @@ class TestCSV::Features < TestCSV
|
|||
|
||||
def test_inspect_shows_headers_when_available
|
||||
CSV.new("one,two,three\n1,2,3\n", headers: true) do |csv|
|
||||
assert(csv.inspect.include?("headers:true"), "Header hint not shown.")
|
||||
assert_include(csv.inspect, "headers:true", "Header hint not shown.")
|
||||
csv.shift # load headers
|
||||
assert_match(/headers:\[[^\]]+\]/, csv.inspect)
|
||||
end
|
||||
|
@ -262,16 +260,16 @@ class TestCSV::Features < TestCSV
|
|||
|
||||
def test_inspect_encoding_is_ascii_compatible
|
||||
CSV.new("one,two,three\n1,2,3\n".encode("UTF-16BE")) do |csv|
|
||||
assert( Encoding.compatible?( Encoding.find("US-ASCII"),
|
||||
csv.inspect.encoding ),
|
||||
"inspect() was not ASCII compatible." )
|
||||
assert_send([Encoding, :compatible?,
|
||||
Encoding.find("US-ASCII"), csv.inspect.encoding],
|
||||
"inspect() was not ASCII compatible.")
|
||||
end
|
||||
end
|
||||
|
||||
def test_version
|
||||
assert_not_nil(CSV::VERSION)
|
||||
assert_instance_of(String, CSV::VERSION)
|
||||
assert(CSV::VERSION.frozen?)
|
||||
assert_predicate(CSV::VERSION, :frozen?)
|
||||
assert_match(/\A\d\.\d\.\d\Z/, CSV::VERSION)
|
||||
end
|
||||
|
||||
|
|
|
@ -85,8 +85,8 @@ class TestCSV::Headers < TestCSV
|
|||
assert_not_nil(row)
|
||||
assert_instance_of(CSV::Row, row)
|
||||
assert_equal([["my", :my], ["new", :new], ["headers", :headers]], row.to_a)
|
||||
assert(row.header_row?)
|
||||
assert(!row.field_row?)
|
||||
assert_predicate(row, :header_row?)
|
||||
assert_not_predicate(row, :field_row?)
|
||||
end
|
||||
|
||||
def test_csv_header_string
|
||||
|
@ -127,8 +127,8 @@ class TestCSV::Headers < TestCSV
|
|||
assert_not_nil(row)
|
||||
assert_instance_of(CSV::Row, row)
|
||||
assert_equal([[:my, "my"], [:new, "new"], [:headers, "headers"]], row.to_a)
|
||||
assert(row.header_row?)
|
||||
assert(!row.field_row?)
|
||||
assert_predicate(row, :header_row?)
|
||||
assert_not_predicate(row, :field_row?)
|
||||
end
|
||||
|
||||
def test_csv_header_string_inherits_separators
|
||||
|
@ -159,24 +159,24 @@ class TestCSV::Headers < TestCSV
|
|||
assert_instance_of(CSV::Row, row)
|
||||
assert_equal( [%w{first first}, %w{second second}, %w{third third}],
|
||||
row.to_a )
|
||||
assert(row.header_row?)
|
||||
assert(!row.field_row?)
|
||||
assert_predicate(row, :header_row?)
|
||||
assert_not_predicate(row, :field_row?)
|
||||
|
||||
# first data row - skipping headers
|
||||
row = csv[1]
|
||||
assert_not_nil(row)
|
||||
assert_instance_of(CSV::Row, row)
|
||||
assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
|
||||
assert(!row.header_row?)
|
||||
assert(row.field_row?)
|
||||
assert_not_predicate(row, :header_row?)
|
||||
assert_predicate(row, :field_row?)
|
||||
|
||||
# second data row
|
||||
row = csv[2]
|
||||
assert_not_nil(row)
|
||||
assert_instance_of(CSV::Row, row)
|
||||
assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
|
||||
assert(!row.header_row?)
|
||||
assert(row.field_row?)
|
||||
assert_not_predicate(row, :header_row?)
|
||||
assert_predicate(row, :field_row?)
|
||||
|
||||
# empty
|
||||
assert_nil(csv[3])
|
||||
|
|
|
@ -50,16 +50,16 @@ class TestCSV::Interface < TestCSV
|
|||
csv = CSV.open(@path, "r+", col_sep: "\t", row_sep: "\r\n")
|
||||
assert_not_nil(csv)
|
||||
assert_instance_of(CSV, csv)
|
||||
assert_equal(false, csv.closed?)
|
||||
assert_not_predicate(csv, :closed?)
|
||||
csv.close
|
||||
assert(csv.closed?)
|
||||
assert_predicate(csv, :closed?)
|
||||
|
||||
ret = CSV.open(@path) do |new_csv|
|
||||
csv = new_csv
|
||||
assert_instance_of(CSV, new_csv)
|
||||
"Return value."
|
||||
end
|
||||
assert(csv.closed?)
|
||||
assert_predicate(csv, :closed?)
|
||||
assert_equal("Return value.", ret)
|
||||
end
|
||||
|
||||
|
|
|
@ -40,16 +40,16 @@ class TestCSV::Row < TestCSV
|
|||
def test_row_type
|
||||
# field rows
|
||||
row = CSV::Row.new(%w{A B C}, [1, 2, 3]) # implicit
|
||||
assert(!row.header_row?)
|
||||
assert(row.field_row?)
|
||||
assert_not_predicate(row, :header_row?)
|
||||
assert_predicate(row, :field_row?)
|
||||
row = CSV::Row.new(%w{A B C}, [1, 2, 3], false) # explicit
|
||||
assert(!row.header_row?)
|
||||
assert(row.field_row?)
|
||||
assert_not_predicate(row, :header_row?)
|
||||
assert_predicate(row, :field_row?)
|
||||
|
||||
# header row
|
||||
row = CSV::Row.new(%w{A B C}, [1, 2, 3], true)
|
||||
assert(row.header_row?)
|
||||
assert(!row.field_row?)
|
||||
assert_predicate(row, :header_row?)
|
||||
assert_not_predicate(row, :field_row?)
|
||||
end
|
||||
|
||||
def test_headers
|
||||
|
@ -249,10 +249,10 @@ class TestCSV::Row < TestCSV
|
|||
|
||||
def test_queries
|
||||
# headers
|
||||
assert(@row.header?("A"))
|
||||
assert(@row.header?("C"))
|
||||
assert(!@row.header?("Z"))
|
||||
assert(@row.include?("A")) # alias
|
||||
assert_send([@row, :header?, "A"])
|
||||
assert_send([@row, :header?, "C"])
|
||||
assert_not_send([@row, :header?, "Z"])
|
||||
assert_send([@row, :include?, "A"]) # alias
|
||||
|
||||
# fields
|
||||
assert(@row.field?(4))
|
||||
|
@ -316,7 +316,7 @@ class TestCSV::Row < TestCSV
|
|||
end
|
||||
|
||||
def test_array_delegation
|
||||
assert(!@row.empty?, "Row was empty.")
|
||||
assert_not_empty(@row, "Row was empty.")
|
||||
|
||||
assert_equal([@row.headers.size, @row.fields.size].max, @row.size)
|
||||
end
|
||||
|
@ -324,26 +324,27 @@ class TestCSV::Row < TestCSV
|
|||
def test_inspect_shows_header_field_pairs
|
||||
str = @row.inspect
|
||||
@row.each do |header, field|
|
||||
assert( str.include?("#{header.inspect}:#{field.inspect}"),
|
||||
"Header field pair not found." )
|
||||
assert_include(str, "#{header.inspect}:#{field.inspect}",
|
||||
"Header field pair not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def test_inspect_encoding_is_ascii_compatible
|
||||
assert( Encoding.compatible?( Encoding.find("US-ASCII"),
|
||||
@row.inspect.encoding ),
|
||||
"inspect() was not ASCII compatible." )
|
||||
assert_send([Encoding, :compatible?,
|
||||
Encoding.find("US-ASCII"),
|
||||
@row.inspect.encoding],
|
||||
"inspect() was not ASCII compatible.")
|
||||
end
|
||||
|
||||
def test_inspect_shows_symbol_headers_as_bare_attributes
|
||||
str = CSV::Row.new(@row.headers.map { |h| h.to_sym }, @row.fields).inspect
|
||||
@row.each do |header, field|
|
||||
assert( str.include?("#{header}:#{field.inspect}"),
|
||||
"Header field pair not found." )
|
||||
assert_include(str, "#{header}:#{field.inspect}",
|
||||
"Header field pair not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def test_can_be_compared_with_other_classes
|
||||
assert(CSV::Row.new([ ], [ ]) != nil, "The row was nil")
|
||||
assert_not_nil(CSV::Row.new([ ], [ ]), "The row was nil")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -398,23 +398,24 @@ class TestCSV::Table < TestCSV
|
|||
end
|
||||
|
||||
def test_array_delegation
|
||||
assert(!@table.empty?, "Table was empty.")
|
||||
assert_not_empty(@table, "Table was empty.")
|
||||
|
||||
assert_equal(@rows.size, @table.size)
|
||||
end
|
||||
|
||||
def test_inspect_shows_current_mode
|
||||
str = @table.inspect
|
||||
assert(str.include?("mode:#{@table.mode}"), "Mode not shown.")
|
||||
assert_include(str, "mode:#{@table.mode}", "Mode not shown.")
|
||||
|
||||
@table.by_col!
|
||||
str = @table.inspect
|
||||
assert(str.include?("mode:#{@table.mode}"), "Mode not shown.")
|
||||
assert_include(str, "mode:#{@table.mode}", "Mode not shown.")
|
||||
end
|
||||
|
||||
def test_inspect_encoding_is_ascii_compatible
|
||||
assert( Encoding.compatible?( Encoding.find("US-ASCII"),
|
||||
@table.inspect.encoding ),
|
||||
assert_send([Encoding, :compatible?,
|
||||
Encoding.find("US-ASCII"),
|
||||
@table.inspect.encoding],
|
||||
"inspect() was not ASCII compatible." )
|
||||
end
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче