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 TestCSVTable < 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
|
|
|
@rows = [ CSV::Row.new(%w{A B C}, [1, 2, 3]),
|
|
|
|
CSV::Row.new(%w{A B C}, [4, 5, 6]),
|
|
|
|
CSV::Row.new(%w{A B C}, [7, 8, 9]) ]
|
|
|
|
@table = CSV::Table.new(@rows)
|
|
|
|
|
|
|
|
@header_table = CSV::Table.new(
|
|
|
|
[CSV::Row.new(%w{A B C}, %w{A B C}, true)] + @rows
|
|
|
|
)
|
2020-05-14 06:57:39 -04:00
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
@header_only_table = CSV::Table.new([], headers: %w{A B C})
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
|
|
|
|
2021-11-03 20:02:47 -04:00
|
|
|
def test_initialize
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_not_nil(@table)
|
|
|
|
assert_instance_of(CSV::Table, @table)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_modes
|
|
|
|
assert_equal(:col_or_row, @table.mode)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# non-destructive changes, intended for one shot calls
|
|
|
|
cols = @table.by_col
|
|
|
|
assert_equal(:col_or_row, @table.mode)
|
|
|
|
assert_equal(:col, cols.mode)
|
|
|
|
assert_equal(@table, cols)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
rows = @table.by_row
|
|
|
|
assert_equal(:col_or_row, @table.mode)
|
|
|
|
assert_equal(:row, rows.mode)
|
|
|
|
assert_equal(@table, rows)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-09-05 09:33:21 -04:00
|
|
|
col_or_row = rows.by_col_or_row
|
|
|
|
assert_equal(:row, rows.mode)
|
|
|
|
assert_equal(:col_or_row, col_or_row.mode)
|
|
|
|
assert_equal(@table, col_or_row)
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# destructive mode changing calls
|
|
|
|
assert_equal(@table, @table.by_row!)
|
|
|
|
assert_equal(:row, @table.mode)
|
|
|
|
assert_equal(@table, @table.by_col_or_row!)
|
|
|
|
assert_equal(:col_or_row, @table.mode)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_headers
|
|
|
|
assert_equal(@rows.first.headers, @table.headers)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-09-05 05:34:38 -04:00
|
|
|
def test_headers_empty
|
|
|
|
t = CSV::Table.new([])
|
|
|
|
assert_equal Array.new, t.headers
|
|
|
|
end
|
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_headers_only
|
|
|
|
assert_equal(%w[A B C], @header_only_table.headers)
|
|
|
|
end
|
|
|
|
|
2019-01-25 01:49:59 -05:00
|
|
|
def test_headers_modified_by_row
|
|
|
|
table = CSV::Table.new([], headers: ["A", "B"])
|
|
|
|
table << ["a", "b"]
|
|
|
|
table.first << {"C" => "c"}
|
|
|
|
assert_equal(["A", "B", "C"], table.headers)
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_index
|
|
|
|
##################
|
|
|
|
### Mixed Mode ###
|
|
|
|
##################
|
|
|
|
# by row
|
|
|
|
@rows.each_index { |i| assert_equal(@rows[i], @table[i]) }
|
|
|
|
assert_equal(nil, @table[100]) # empty row
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2015-06-16 21:58:34 -04:00
|
|
|
# by row with Range
|
|
|
|
assert_equal([@table[1], @table[2]], @table[1..2])
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by col
|
|
|
|
@rows.first.headers.each do |header|
|
|
|
|
assert_equal(@rows.map { |row| row[header] }, @table[header])
|
|
|
|
end
|
|
|
|
assert_equal([nil] * @rows.size, @table["Z"]) # empty col
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by cell, row then col
|
|
|
|
assert_equal(2, @table[0][1])
|
|
|
|
assert_equal(6, @table[1]["C"])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# by cell, col then row
|
|
|
|
assert_equal(5, @table["B"][1])
|
|
|
|
assert_equal(9, @table["C"][2])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with headers (by col)
|
|
|
|
assert_equal(["B", 2, 5, 8], @header_table["B"])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal([2, 5, 8], @table[1])
|
|
|
|
assert_equal([2, 5, 8], @table["B"])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
################
|
|
|
|
### Row Mode ###
|
|
|
|
################
|
|
|
|
@table.by_row!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@rows[1], @table[1])
|
|
|
|
assert_raise(TypeError) { @table["B"] }
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
############################
|
|
|
|
### One Shot Mode Change ###
|
|
|
|
############################
|
|
|
|
assert_equal(@rows[1], @table[1])
|
|
|
|
assert_equal([2, 5, 8], @table.by_col[1])
|
|
|
|
assert_equal(@rows[1], @table[1])
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_set_row_or_column
|
|
|
|
##################
|
|
|
|
### Mixed Mode ###
|
|
|
|
##################
|
|
|
|
# set row
|
|
|
|
@table[2] = [10, 11, 12]
|
|
|
|
assert_equal([%w[A B C], [1, 2, 3], [4, 5, 6], [10, 11, 12]], @table.to_a)
|
|
|
|
|
|
|
|
@table[3] = CSV::Row.new(%w[A B C], [13, 14, 15])
|
|
|
|
assert_equal( [%w[A B C], [1, 2, 3], [4, 5, 6], [10, 11, 12], [13, 14, 15]],
|
|
|
|
@table.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# set col
|
|
|
|
@table["Type"] = "data"
|
|
|
|
assert_equal( [ %w[A B C Type],
|
|
|
|
[1, 2, 3, "data"],
|
|
|
|
[4, 5, 6, "data"],
|
|
|
|
[10, 11, 12, "data"],
|
|
|
|
[13, 14, 15, "data"] ],
|
|
|
|
@table.to_a )
|
|
|
|
|
|
|
|
@table["Index"] = [1, 2, 3]
|
|
|
|
assert_equal( [ %w[A B C Type Index],
|
|
|
|
[1, 2, 3, "data", 1],
|
|
|
|
[4, 5, 6, "data", 2],
|
|
|
|
[10, 11, 12, "data", 3],
|
|
|
|
[13, 14, 15, "data", nil] ],
|
|
|
|
@table.to_a )
|
|
|
|
|
|
|
|
@table["B"] = [100, 200]
|
|
|
|
assert_equal( [ %w[A B C Type Index],
|
|
|
|
[1, 100, 3, "data", 1],
|
|
|
|
[4, 200, 6, "data", 2],
|
|
|
|
[10, nil, 12, "data", 3],
|
|
|
|
[13, nil, 15, "data", nil] ],
|
|
|
|
@table.to_a )
|
|
|
|
|
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
A,B,C,Type,Index
|
|
|
|
1,100,3,data,1
|
|
|
|
4,200,6,data,2
|
|
|
|
10,,12,data,3
|
|
|
|
13,,15,data,
|
|
|
|
CSV
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with headers
|
|
|
|
@header_table["Type"] = "data"
|
|
|
|
assert_equal(%w[Type data data data], @header_table["Type"])
|
|
|
|
|
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
@table[1] = [2, 5, 11, 14]
|
|
|
|
assert_equal( [ %w[A B C Type Index],
|
|
|
|
[1, 2, 3, "data", 1],
|
|
|
|
[4, 5, 6, "data", 2],
|
|
|
|
[10, 11, 12, "data", 3],
|
|
|
|
[13, 14, 15, "data", nil] ],
|
|
|
|
@table.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
@table["Extra"] = "new stuff"
|
|
|
|
assert_equal( [ %w[A B C Type Index Extra],
|
|
|
|
[1, 2, 3, "data", 1, "new stuff"],
|
|
|
|
[4, 5, 6, "data", 2, "new stuff"],
|
|
|
|
[10, 11, 12, "data", 3, "new stuff"],
|
|
|
|
[13, 14, 15, "data", nil, "new stuff"] ],
|
|
|
|
@table.to_a )
|
|
|
|
|
|
|
|
################
|
|
|
|
### Row Mode ###
|
|
|
|
################
|
|
|
|
@table.by_row!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
@table[1] = (1..6).to_a
|
|
|
|
assert_equal( [ %w[A B C Type Index Extra],
|
|
|
|
[1, 2, 3, "data", 1, "new stuff"],
|
|
|
|
[1, 2, 3, 4, 5, 6],
|
|
|
|
[10, 11, 12, "data", 3, "new stuff"],
|
|
|
|
[13, 14, 15, "data", nil, "new stuff"] ],
|
|
|
|
@table.to_a )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_raise(TypeError) { @table["Extra"] = nil }
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-09-05 05:34:38 -04:00
|
|
|
def test_set_by_col_with_header_row
|
|
|
|
r = [ CSV::Row.new(%w{X Y Z}, [97, 98, 99], true) ]
|
|
|
|
t = CSV::Table.new(r)
|
|
|
|
t.by_col!
|
|
|
|
t['A'] = [42]
|
|
|
|
assert_equal(['A'], t['A'])
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_each
|
|
|
|
######################
|
|
|
|
### Mixed/Row Mode ###
|
|
|
|
######################
|
|
|
|
i = 0
|
|
|
|
@table.each do |row|
|
|
|
|
assert_equal(@rows[i], row)
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# verify that we can chain the call
|
|
|
|
assert_equal(@table, @table.each { })
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2016-09-07 02:06:09 -04:00
|
|
|
# without block
|
|
|
|
enum = @table.each
|
|
|
|
assert_instance_of(Enumerator, enum)
|
|
|
|
assert_equal(@table.size, enum.size)
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
enum.each do |row|
|
|
|
|
assert_equal(@rows[i], row)
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
headers = @table.headers
|
|
|
|
@table.each do |header, column|
|
|
|
|
assert_equal(headers.shift, header)
|
|
|
|
assert_equal(@table[header], column)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2016-09-07 02:06:09 -04:00
|
|
|
# without block
|
|
|
|
enum = @table.each
|
|
|
|
assert_instance_of(Enumerator, enum)
|
|
|
|
assert_equal(@table.headers.size, enum.size)
|
|
|
|
|
|
|
|
headers = @table.headers
|
|
|
|
enum.each do |header, column|
|
|
|
|
assert_equal(headers.shift, header)
|
|
|
|
assert_equal(@table[header], column)
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
############################
|
|
|
|
### One Shot Mode Change ###
|
|
|
|
############################
|
|
|
|
@table.by_col_or_row!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
@table.each { |row| assert_instance_of(CSV::Row, row) }
|
|
|
|
@table.by_col.each { |tuple| assert_instance_of(Array, tuple) }
|
|
|
|
@table.each { |row| assert_instance_of(CSV::Row, row) }
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-05-09 00:39:16 -04:00
|
|
|
def test_each_split
|
|
|
|
yielded_values = []
|
|
|
|
@table.each do |column1, column2, column3|
|
|
|
|
yielded_values << [column1, column2, column3]
|
|
|
|
end
|
|
|
|
assert_equal(@rows.collect(&:to_a),
|
|
|
|
yielded_values)
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_enumerable
|
|
|
|
assert_equal( @rows.values_at(0, 2),
|
|
|
|
@table.select { |row| (row["B"] % 2).zero? } )
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@rows[1], @table.find { |row| row["C"] > 5 })
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_to_a
|
|
|
|
assert_equal([%w[A B C], [1, 2, 3], [4, 5, 6], [7, 8, 9]], @table.to_a)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with headers
|
|
|
|
assert_equal( [%w[A B C], [1, 2, 3], [4, 5, 6], [7, 8, 9]],
|
|
|
|
@header_table.to_a )
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_to_csv
|
2018-09-05 09:33:21 -04:00
|
|
|
csv = <<-CSV
|
|
|
|
A,B,C
|
|
|
|
1,2,3
|
|
|
|
4,5,6
|
|
|
|
7,8,9
|
|
|
|
CSV
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# normal conversion
|
|
|
|
assert_equal(csv, @table.to_csv)
|
|
|
|
assert_equal(csv, @table.to_s) # alias
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# with options
|
|
|
|
assert_equal( csv.gsub(",", "|").gsub("\n", "\r\n"),
|
2008-10-10 11:09:34 -04:00
|
|
|
@table.to_csv(col_sep: "|", row_sep: "\r\n") )
|
2010-12-25 01:58:58 -05:00
|
|
|
assert_equal( csv.lines.to_a[1..-1].join(''),
|
2010-03-23 10:59:25 -04:00
|
|
|
@table.to_csv(:write_headers => false) )
|
2007-12-24 21:46:26 -05:00
|
|
|
|
|
|
|
# with headers
|
|
|
|
assert_equal(csv, @header_table.to_csv)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_append
|
|
|
|
# verify that we can chain the call
|
|
|
|
assert_equal(@table, @table << [10, 11, 12])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# Array append
|
|
|
|
assert_equal(CSV::Row.new(%w[A B C], [10, 11, 12]), @table[-1])
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# Row append
|
|
|
|
assert_equal(@table, @table << CSV::Row.new(%w[A B C], [13, 14, 15]))
|
|
|
|
assert_equal(CSV::Row.new(%w[A B C], [13, 14, 15]), @table[-1])
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-05-09 00:39:16 -04:00
|
|
|
def test_delete_mixed_one
|
2007-12-24 21:46:26 -05:00
|
|
|
##################
|
|
|
|
### Mixed Mode ###
|
|
|
|
##################
|
|
|
|
# delete a row
|
|
|
|
assert_equal(@rows[1], @table.delete(1))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# delete a col
|
|
|
|
assert_equal(@rows.map { |row| row["A"] }, @table.delete("A"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
B,C
|
|
|
|
2,3
|
|
|
|
8,9
|
|
|
|
CSV
|
2010-12-25 08:04:43 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2018-05-09 00:39:16 -04:00
|
|
|
def test_delete_mixed_multiple
|
|
|
|
##################
|
|
|
|
### Mixed Mode ###
|
|
|
|
##################
|
|
|
|
# delete row and col
|
|
|
|
second_row = @rows[1]
|
|
|
|
a_col = @rows.map { |row| row["A"] }
|
|
|
|
a_col_without_second_row = a_col[0..0] + a_col[2..-1]
|
|
|
|
assert_equal([
|
|
|
|
second_row,
|
|
|
|
a_col_without_second_row,
|
|
|
|
],
|
|
|
|
@table.delete(1, "A"))
|
|
|
|
|
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
B,C
|
|
|
|
2,3
|
|
|
|
8,9
|
|
|
|
CSV
|
2018-05-09 00:39:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-25 08:04:43 -05:00
|
|
|
def test_delete_column
|
2007-12-24 21:46:26 -05:00
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
|
|
|
|
|
|
|
assert_equal(@rows.map { |row| row[0] }, @table.delete(0))
|
|
|
|
assert_equal(@rows.map { |row| row["C"] }, @table.delete("C"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
B
|
|
|
|
2
|
|
|
|
5
|
|
|
|
8
|
|
|
|
CSV
|
2010-12-25 08:04:43 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2010-12-25 08:04:43 -05:00
|
|
|
def test_delete_row
|
2007-12-24 21:46:26 -05:00
|
|
|
################
|
|
|
|
### Row Mode ###
|
|
|
|
################
|
|
|
|
@table.by_row!
|
|
|
|
|
|
|
|
assert_equal(@rows[1], @table.delete(1))
|
|
|
|
assert_raise(TypeError) { @table.delete("C") }
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
A,B,C
|
|
|
|
1,2,3
|
|
|
|
7,8,9
|
|
|
|
CSV
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2010-01-31 00:34:17 -05:00
|
|
|
def test_delete_with_blank_rows
|
|
|
|
data = "col1,col2\nra1,ra2\n\nrb1,rb2"
|
|
|
|
table = CSV.parse(data, :headers => true)
|
|
|
|
assert_equal(["ra2", nil, "rb2"], table.delete("col2"))
|
|
|
|
end
|
2011-04-26 11:57:04 -04:00
|
|
|
|
2010-12-25 08:04:43 -05:00
|
|
|
def test_delete_if_row
|
2007-12-24 21:46:26 -05:00
|
|
|
######################
|
|
|
|
### Mixed/Row Mode ###
|
|
|
|
######################
|
|
|
|
# verify that we can chain the call
|
|
|
|
assert_equal(@table, @table.delete_if { |row| (row["B"] % 2).zero? })
|
|
|
|
|
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
A,B,C
|
|
|
|
4,5,6
|
|
|
|
CSV
|
2010-12-25 08:04:43 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2016-09-07 02:06:09 -04:00
|
|
|
def test_delete_if_row_without_block
|
|
|
|
######################
|
|
|
|
### Mixed/Row Mode ###
|
|
|
|
######################
|
|
|
|
enum = @table.delete_if
|
|
|
|
assert_instance_of(Enumerator, enum)
|
|
|
|
assert_equal(@table.size, enum.size)
|
|
|
|
|
|
|
|
# verify that we can chain the call
|
|
|
|
assert_equal(@table, enum.each { |row| (row["B"] % 2).zero? })
|
|
|
|
|
|
|
|
# verify resulting table
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
A,B,C
|
|
|
|
4,5,6
|
|
|
|
CSV
|
2016-09-07 02:06:09 -04:00
|
|
|
end
|
|
|
|
|
2010-12-25 08:04:43 -05:00
|
|
|
def test_delete_if_column
|
2007-12-24 21:46:26 -05:00
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@table, @table.delete_if { |h, v| h > "A" })
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
A
|
|
|
|
1
|
|
|
|
4
|
|
|
|
7
|
|
|
|
CSV
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2016-09-07 02:06:09 -04:00
|
|
|
def test_delete_if_column_without_block
|
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
|
|
|
|
|
|
|
enum = @table.delete_if
|
|
|
|
assert_instance_of(Enumerator, enum)
|
|
|
|
assert_equal(@table.headers.size, enum.size)
|
|
|
|
|
|
|
|
assert_equal(@table, enum.each { |h, v| h > "A" })
|
2018-09-05 09:33:21 -04:00
|
|
|
assert_equal(<<-CSV, @table.to_csv)
|
|
|
|
A
|
|
|
|
1
|
|
|
|
4
|
|
|
|
7
|
|
|
|
CSV
|
2016-09-07 02:06:09 -04:00
|
|
|
end
|
|
|
|
|
2018-12-23 02:00:35 -05:00
|
|
|
def test_delete_headers_only
|
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@header_only_table.by_col!
|
|
|
|
|
|
|
|
# delete by index
|
|
|
|
assert_equal([], @header_only_table.delete(0))
|
|
|
|
assert_equal(%w[B C], @header_only_table.headers)
|
|
|
|
|
|
|
|
# delete by header
|
|
|
|
assert_equal([], @header_only_table.delete("C"))
|
|
|
|
assert_equal(%w[B], @header_only_table.headers)
|
|
|
|
end
|
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
def test_values_at
|
|
|
|
##################
|
|
|
|
### Mixed Mode ###
|
|
|
|
##################
|
|
|
|
# rows
|
|
|
|
assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
|
|
|
|
assert_equal(@rows.values_at(1..2), @table.values_at(1..2))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
# cols
|
|
|
|
assert_equal([[1, 3], [4, 6], [7, 9]], @table.values_at("A", "C"))
|
|
|
|
assert_equal([[2, 3], [5, 6], [8, 9]], @table.values_at("B".."C"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
###################
|
|
|
|
### Column Mode ###
|
|
|
|
###################
|
|
|
|
@table.by_col!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal([[1, 3], [4, 6], [7, 9]], @table.values_at(0, 2))
|
|
|
|
assert_equal([[1, 3], [4, 6], [7, 9]], @table.values_at("A", "C"))
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
################
|
|
|
|
### Row Mode ###
|
|
|
|
################
|
|
|
|
@table.by_row!
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
|
|
|
|
assert_raise(TypeError) { @table.values_at("A", "C") }
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
############################
|
|
|
|
### One Shot Mode Change ###
|
|
|
|
############################
|
|
|
|
assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
|
|
|
|
assert_equal([[1, 3], [4, 6], [7, 9]], @table.by_col.values_at(0, 2))
|
|
|
|
assert_equal(@rows.values_at(0, 2), @table.values_at(0, 2))
|
|
|
|
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(@table, "Table was empty.")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-24 21:46:26 -05:00
|
|
|
assert_equal(@rows.size, @table.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_current_mode
|
|
|
|
str = @table.inspect
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_include(str, "mode:#{@table.mode}", "Mode not shown.")
|
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
|
|
|
@table.by_col!
|
|
|
|
str = @table.inspect
|
2014-03-01 02:08:19 -05:00
|
|
|
assert_include(str, "mode:#{@table.mode}", "Mode not shown.")
|
* 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
|
|
|
|
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"),
|
|
|
|
@table.inspect.encoding],
|
2008-10-24 20:54:38 -04:00
|
|
|
"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
|
2018-05-09 00:39:16 -04:00
|
|
|
|
|
|
|
def test_dig_mixed
|
|
|
|
# by row
|
|
|
|
assert_equal(@rows[0], @table.dig(0))
|
|
|
|
assert_nil(@table.dig(100)) # empty row
|
|
|
|
|
|
|
|
# by col
|
|
|
|
assert_equal([2, 5, 8], @table.dig("B"))
|
|
|
|
assert_equal([nil] * @rows.size, @table.dig("Z")) # empty col
|
|
|
|
|
|
|
|
# by row then col
|
|
|
|
assert_equal(2, @table.dig(0, 1))
|
|
|
|
assert_equal(6, @table.dig(1, "C"))
|
|
|
|
|
|
|
|
# by col then row
|
|
|
|
assert_equal(5, @table.dig("B", 1))
|
|
|
|
assert_equal(9, @table.dig("C", 2))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_by_column
|
|
|
|
@table.by_col!
|
|
|
|
|
|
|
|
assert_equal([2, 5, 8], @table.dig(1))
|
|
|
|
assert_equal([2, 5, 8], @table.dig("B"))
|
|
|
|
|
|
|
|
# by col then row
|
|
|
|
assert_equal(5, @table.dig("B", 1))
|
|
|
|
assert_equal(9, @table.dig("C", 2))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_by_row
|
|
|
|
@table.by_row!
|
|
|
|
|
|
|
|
assert_equal(@rows[1], @table.dig(1))
|
|
|
|
assert_raise(TypeError) { @table.dig("B") }
|
|
|
|
|
|
|
|
# by row then col
|
|
|
|
assert_equal(2, @table.dig(0, 1))
|
|
|
|
assert_equal(6, @table.dig(1, "C"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_cell
|
|
|
|
table = CSV::Table.new([CSV::Row.new(["A"], [["foo", ["bar", ["baz"]]]])])
|
|
|
|
|
|
|
|
# by row, col then cell
|
|
|
|
assert_equal("foo", table.dig(0, "A", 0))
|
|
|
|
assert_equal(["baz"], table.dig(0, "A", 1, 1))
|
|
|
|
|
|
|
|
# by col, row then cell
|
|
|
|
assert_equal("foo", table.dig("A", 0, 0))
|
|
|
|
assert_equal(["baz"], table.dig("A", 0, 1, 1))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dig_cell_no_dig
|
|
|
|
table = CSV::Table.new([CSV::Row.new(["A"], ["foo"])])
|
|
|
|
|
|
|
|
# by row, col then cell
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
table.dig(0, "A", 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
# by col, row then cell
|
|
|
|
assert_raise(TypeError) do
|
|
|
|
table.dig("A", 0, 0)
|
|
|
|
end
|
|
|
|
end
|
2007-12-24 21:46:26 -05:00
|
|
|
end
|