2019-01-25 01:49:59 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-12-24 21:46:26 -05:00
|
|
|
|
2019-01-25 01:49:59 -05:00
|
|
|
require_relative "helper"
|
2007-12-24 21:46:26 -05:00
|
|
|
|
2019-01-25 01:49:59 -05:00
|
|
|
class TestCSVRow < Test::Unit::TestCase
|
2010-12-25 21:15:55 -05:00
|
|
|
extend DifferentOFS
|
2010-12-25 08:49:14 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def setup
|
2010-12-25 08:41:11 -05:00
|
|
|
super
|
2007-12-24 21:46:26 -05:00
|
|
|
@row = CSV::Row.new(%w{A B C A A}, [1, 2, 3, 4])
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_initialize
|
|
|
|
# basic
|
|
|
|
row = CSV::Row.new(%w{A B C}, [1, 2, 3])
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([["A", 1], ["B", 2], ["C", 3]], row.to_a)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# missing headers
|
|
|
|
row = CSV::Row.new(%w{A}, [1, 2, 3])
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([["A", 1], [nil, 2], [nil, 3]], row.to_a)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# missing fields
|
|
|
|
row = CSV::Row.new(%w{A B C}, [1, 2])
|
|
|
|
assert_not_nil(row)
|
|
|
|
assert_instance_of(CSV::Row, row)
|
|
|
|
assert_equal([["A", 1], ["B", 2], ["C", nil]], row.to_a)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_row_type
|
|
|
|
# field rows
|
|
|
|
row = CSV::Row.new(%w{A B C}, [1, 2, 3]) # implicit
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_not_predicate(row, :header_row?)
|
|
|
|
assert_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
row = CSV::Row.new(%w{A B C}, [1, 2, 3], false) # explicit
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_not_predicate(row, :header_row?)
|
|
|
|
assert_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# header row
|
|
|
|
row = CSV::Row.new(%w{A B C}, [1, 2, 3], true)
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_predicate(row, :header_row?)
|
|
|
|
assert_not_predicate(row, :field_row?)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_headers
|
|
|
|
assert_equal(%w{A B C A A}, @row.headers)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_field
|
|
|
|
# by name
|
|
|
|
assert_equal(2, @row.field("B"))
|
|
|
|
assert_equal(2, @row["B"]) # alias
|
|
|
|
|
|
|
|
# by index
|
|
|
|
assert_equal(3, @row.field(2))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2015-06-16 21:58:34 -04:00
|
|
|
# by range
|
|
|
|
assert_equal([2,3], @row.field(1..2))
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# missing
|
|
|
|
assert_nil(@row.field("Missing"))
|
|
|
|
assert_nil(@row.field(10))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# minimum index
|
|
|
|
assert_equal(1, @row.field("A"))
|
|
|
|
assert_equal(1, @row.field("A", 0))
|
|
|
|
assert_equal(4, @row.field("A", 1))
|
|
|
|
assert_equal(4, @row.field("A", 2))
|
|
|
|
assert_equal(4, @row.field("A", 3))
|
|
|
|
assert_equal(nil, @row.field("A", 4))
|
|
|
|
assert_equal(nil, @row.field("A", 5))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-27 11:15:53 -05:00
|
|
|
def test_fetch
|
|
|
|
# only by name
|
|
|
|
assert_equal(2, @row.fetch('B'))
|
|
|
|
|
|
|
|
# missing header raises KeyError
|
|
|
|
assert_raise KeyError do
|
|
|
|
@row.fetch('foo')
|
|
|
|
end
|
|
|
|
|
|
|
|
# missing header yields itself to block
|
|
|
|
assert_equal 'bar', @row.fetch('foo') { |header|
|
|
|
|
header == 'foo' ? 'bar' : false }
|
|
|
|
|
|
|
|
# missing header returns the given default value
|
|
|
|
assert_equal 'bar', @row.fetch('foo', 'bar')
|
|
|
|
|
|
|
|
# more than one vararg raises ArgumentError
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
@row.fetch('foo', 'bar', 'baz')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_key?
|
|
|
|
assert_equal(true, @row.has_key?('B'))
|
|
|
|
assert_equal(false, @row.has_key?('foo'))
|
2019-01-25 01:49:59 -05:00
|
|
|
|
|
|
|
# aliases
|
|
|
|
assert_equal(true, @row.header?('B'))
|
|
|
|
assert_equal(false, @row.header?('foo'))
|
|
|
|
|
|
|
|
assert_equal(true, @row.include?('B'))
|
|
|
|
assert_equal(false, @row.include?('foo'))
|
|
|
|
|
|
|
|
assert_equal(true, @row.member?('B'))
|
|
|
|
assert_equal(false, @row.member?('foo'))
|
|
|
|
|
|
|
|
assert_equal(true, @row.key?('B'))
|
|
|
|
assert_equal(false, @row.key?('foo'))
|
2012-12-27 11:15:53 -05:00
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_set_field
|
|
|
|
# set field by name
|
|
|
|
assert_equal(100, @row["A"] = 100)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# set field by index
|
|
|
|
assert_equal(300, @row[3] = 300)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# set field by name and minimum index
|
|
|
|
assert_equal([:a, :b, :c], @row["A", 4] = [:a, :b, :c])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# verify the changes
|
|
|
|
assert_equal( [ ["A", 100],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 300],
|
|
|
|
["A", [:a, :b, :c]] ], @row.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# assigning an index past the end
|
|
|
|
assert_equal("End", @row[10] = "End")
|
|
|
|
assert_equal( [ ["A", 100],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 300],
|
|
|
|
["A", [:a, :b, :c]],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, "End"] ], @row.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# assigning a new field by header
|
|
|
|
assert_equal("New", @row[:new] = "New")
|
|
|
|
assert_equal( [ ["A", 100],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 300],
|
|
|
|
["A", [:a, :b, :c]],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, nil],
|
|
|
|
[nil, "End"],
|
|
|
|
[:new, "New"] ], @row.to_a )
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_append
|
|
|
|
# add a value
|
|
|
|
assert_equal(@row, @row << "Value")
|
|
|
|
assert_equal( [ ["A", 1],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 4],
|
|
|
|
["A", nil],
|
|
|
|
[nil, "Value"] ], @row.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# add a pair
|
|
|
|
assert_equal(@row, @row << %w{Header Field})
|
|
|
|
assert_equal( [ ["A", 1],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 4],
|
|
|
|
["A", nil],
|
|
|
|
[nil, "Value"],
|
|
|
|
%w{Header Field} ], @row.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# a pair with Hash syntax
|
2008-10-10 11:09:34 -04:00
|
|
|
assert_equal(@row, @row << {key: :value})
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal( [ ["A", 1],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 4],
|
|
|
|
["A", nil],
|
|
|
|
[nil, "Value"],
|
|
|
|
%w{Header Field},
|
|
|
|
[:key, :value] ], @row.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# multiple fields at once
|
|
|
|
assert_equal(@row, @row.push(100, 200, [:last, 300]))
|
|
|
|
assert_equal( [ ["A", 1],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 4],
|
|
|
|
["A", nil],
|
|
|
|
[nil, "Value"],
|
|
|
|
%w{Header Field},
|
|
|
|
[:key, :value],
|
|
|
|
[nil, 100],
|
|
|
|
[nil, 200],
|
|
|
|
[:last, 300] ], @row.to_a )
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_delete
|
|
|
|
# by index
|
|
|
|
assert_equal(["B", 2], @row.delete(1))
|
|
|
|
|
|
|
|
# by header
|
|
|
|
assert_equal(["C", 3], @row.delete("C"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2016-09-07 02:06:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_if
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@row, @row.delete_if { |h, f| h == "A" and not f.nil? })
|
2016-09-07 02:06:09 -04:00
|
|
|
assert_equal([["B", 2], ["C", 3], ["A", nil]], @row.to_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_if_without_block
|
|
|
|
enum = @row.delete_if
|
|
|
|
assert_instance_of(Enumerator, enum)
|
|
|
|
assert_equal(@row.size, enum.size)
|
|
|
|
|
|
|
|
assert_equal(@row, enum.each { |h, f| h == "A" and not f.nil? })
|
|
|
|
assert_equal([["B", 2], ["C", 3], ["A", nil]], @row.to_a)
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_fields
|
|
|
|
# all fields
|
|
|
|
assert_equal([1, 2, 3, 4, nil], @row.fields)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by header
|
|
|
|
assert_equal([1, 3], @row.fields("A", "C"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by index
|
|
|
|
assert_equal([2, 3, nil], @row.fields(1, 2, 10))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by both
|
|
|
|
assert_equal([2, 3, 4], @row.fields("B", "C", 3))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with minimum indices
|
|
|
|
assert_equal([2, 3, 4], @row.fields("B", "C", ["A", 3]))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by header range
|
|
|
|
assert_equal([2, 3], @row.values_at("B".."C"))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_index
|
|
|
|
# basic usage
|
|
|
|
assert_equal(0, @row.index("A"))
|
|
|
|
assert_equal(1, @row.index("B"))
|
|
|
|
assert_equal(2, @row.index("C"))
|
|
|
|
assert_equal(nil, @row.index("Z"))
|
|
|
|
|
|
|
|
# with minimum index
|
|
|
|
assert_equal(0, @row.index("A"))
|
|
|
|
assert_equal(0, @row.index("A", 0))
|
|
|
|
assert_equal(3, @row.index("A", 1))
|
|
|
|
assert_equal(3, @row.index("A", 2))
|
|
|
|
assert_equal(3, @row.index("A", 3))
|
|
|
|
assert_equal(4, @row.index("A", 4))
|
|
|
|
assert_equal(nil, @row.index("A", 5))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_queries
|
|
|
|
# fields
|
|
|
|
assert(@row.field?(4))
|
|
|
|
assert(@row.field?(nil))
|
|
|
|
assert(!@row.field?(10))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_each
|
|
|
|
# array style
|
|
|
|
ary = @row.to_a
|
|
|
|
@row.each do |pair|
|
|
|
|
assert_equal(ary.first.first, pair.first)
|
|
|
|
assert_equal(ary.shift.last, pair.last)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# hash style
|
|
|
|
ary = @row.to_a
|
|
|
|
@row.each do |header, field|
|
|
|
|
assert_equal(ary.first.first, header)
|
|
|
|
assert_equal(ary.shift.last, field)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# verify that we can chain the call
|
|
|
|
assert_equal(@row, @row.each { })
|
2016-09-07 02:06:09 -04:00
|
|
|
|
|
|
|
# without block
|
|
|
|
ary = @row.to_a
|
|
|
|
enum = @row.each
|
|
|
|
assert_instance_of(Enumerator, enum)
|
|
|
|
assert_equal(@row.size, enum.size)
|
|
|
|
enum.each do |pair|
|
|
|
|
assert_equal(ary.first.first, pair.first)
|
|
|
|
assert_equal(ary.shift.last, pair.last)
|
|
|
|
end
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-05-09 00:39:16 -04:00
|
|
|
def test_each_pair
|
|
|
|
assert_equal([
|
|
|
|
["A", 1],
|
|
|
|
["B", 2],
|
|
|
|
["C", 3],
|
|
|
|
["A", 4],
|
|
|
|
["A", nil],
|
|
|
|
],
|
|
|
|
@row.each_pair.to_a)
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_enumerable
|
|
|
|
assert_equal( [["A", 1], ["A", 4], ["A", nil]],
|
|
|
|
@row.select { |pair| pair.first == "A" } )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-12-07 12:42:16 -05:00
|
|
|
assert_equal(10, @row.inject(0) { |sum, (_, n)| sum + (n || 0) })
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_to_a
|
|
|
|
row = CSV::Row.new(%w{A B C}, [1, 2, 3]).to_a
|
|
|
|
assert_instance_of(Array, row)
|
|
|
|
row.each do |pair|
|
|
|
|
assert_instance_of(Array, pair)
|
|
|
|
assert_equal(2, pair.size)
|
|
|
|
end
|
|
|
|
assert_equal([["A", 1], ["B", 2], ["C", 3]], row)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_to_hash
|
2013-11-23 19:44:41 -05:00
|
|
|
hash = @row.to_hash
|
2018-05-09 00:39:16 -04:00
|
|
|
assert_equal({"A" => @row["A"], "B" => @row["B"], "C" => @row["C"]}, hash)
|
2013-11-23 19:44:41 -05:00
|
|
|
hash.keys.each_with_index do |string_key, h|
|
|
|
|
assert_predicate(string_key, :frozen?)
|
|
|
|
assert_same(string_key, @row.headers[h])
|
|
|
|
end
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_to_csv
|
|
|
|
# normal conversion
|
|
|
|
assert_equal("1,2,3,4,\n", @row.to_csv)
|
|
|
|
assert_equal("1,2,3,4,\n", @row.to_s) # alias
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with options
|
|
|
|
assert_equal( "1|2|3|4|\r\n",
|
2008-10-10 11:09:34 -04:00
|
|
|
@row.to_csv(col_sep: "|", row_sep: "\r\n") )
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_array_delegation
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_not_empty(@row, "Row was empty.")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal([@row.headers.size, @row.fields.size].max, @row.size)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
def test_inspect_shows_header_field_pairs
|
|
|
|
str = @row.inspect
|
|
|
|
@row.each do |header, field|
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_include(str, "#{header.inspect}:#{field.inspect}",
|
|
|
|
"Header field pair not found.")
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-24 20:54:38 -04:00
|
|
|
def test_inspect_encoding_is_ascii_compatible
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_send([Encoding, :compatible?,
|
|
|
|
Encoding.find("US-ASCII"),
|
|
|
|
@row.inspect.encoding],
|
|
|
|
"inspect() was not ASCII compatible.")
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
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|
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_include(str, "#{header}:#{field.inspect}",
|
|
|
|
"Header field pair not found.")
|
* lib/csv/csv.rb: Reworked CSV's parser and generator to be m17n. Data
is now parsed in the Encoding it is in without need for translation.
* lib/csv/csv.rb: Improved inspect() messages for better IRb support.
* lib/csv/csv.rb: Fixed header writing bug reported by Dov Murik.
* lib/csv/csv.rb: Use custom separators in parsing header Strings as
suggested by Shmulik Regev.
* lib/csv/csv.rb: Added a :write_headers option for outputting headers.
* lib/csv/csv.rb: Handle open() calls in binary mode whenever we can to
workaround a Windows issue where line-ending translation can cause an
off-by-one error in seeking back to a non-zero starting position after
auto-discovery for :row_sep as suggested by Robert Battle.
* lib/csv/csv.rb: Improved the parser to fail faster when fed some forms
of invalid CSV that can be detected without reading ahead.
* lib/csv/csv.rb: Added a :field_size_limit option to control CSV's
lookahead and prevent the parser from biting off more data than
it can chew.
* lib/csv/csv.rb: Added readers for CSV attributes: col_sep(), row_sep(),
quote_char(), field_size_limit(), converters(), unconverted_fields?(),
headers(), return_headers?(), write_headers?(), header_converters(),
skip_blanks?(), and force_quotes?().
* lib/csv/csv.rb: Cleaned up code syntax to be more inline with
Ruby 1.9 than 1.8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-09-20 20:39:03 -04:00
|
|
|
end
|
|
|
|
end
|
2012-12-07 12:42:16 -05:00
|
|
|
|
|
|
|
def test_can_be_compared_with_other_classes
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_not_nil(CSV::Row.new([ ], [ ]), "The row was nil")
|
2012-12-07 12:42:16 -05:00
|
|
|
end
|
2014-09-05 05:34:38 -04:00
|
|
|
|
|
|
|
def test_can_be_compared_when_not_a_row
|
|
|
|
r = @row == []
|
|
|
|
assert_equal false, r
|
|
|
|
end
|
2018-05-09 00:39:16 -04:00
|
|
|
|
|
|
|
def test_dig_by_index
|
|
|
|
assert_equal(2, @row.dig(1))
|
|
|
|
|
|
|
|
assert_nil(@row.dig(100))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_by_header
|
|
|
|
assert_equal(2, @row.dig("B"))
|
|
|
|
|
|
|
|
assert_nil(@row.dig("Missing"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_cell
|
|
|
|
row = CSV::Row.new(%w{A}, [["foo", ["bar", ["baz"]]]])
|
|
|
|
|
|
|
|
assert_equal("foo", row.dig(0, 0))
|
|
|
|
assert_equal("bar", row.dig(0, 1, 0))
|
|
|
|
|
|
|
|
assert_equal("foo", row.dig("A", 0))
|
|
|
|
assert_equal("bar", row.dig("A", 1, 0))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_cell_no_dig
|
|
|
|
row = CSV::Row.new(%w{A}, ["foo"])
|
|
|
|
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
row.dig(0, 0)
|
|
|
|
end
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
row.dig("A", 0)
|
|
|
|
end
|
|
|
|
end
|
2018-09-05 09:33:21 -04:00
|
|
|
|
|
|
|
def test_dup
|
|
|
|
row = CSV::Row.new(["A"], ["foo"])
|
|
|
|
dupped_row = row.dup
|
2019-10-22 06:01:24 -04:00
|
|
|
dupped_row["A"] = "bar"
|
|
|
|
assert_equal(["foo", "bar"],
|
|
|
|
[row["A"], dupped_row["A"]])
|
2018-09-05 09:33:21 -04:00
|
|
|
dupped_row.delete("A")
|
|
|
|
assert_equal(["foo", nil],
|
|
|
|
[row["A"], dupped_row["A"]])
|
|
|
|
end
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|