From 5c1941a9be56a979c27d740370b781882d344f79 Mon Sep 17 00:00:00 2001 From: hsbt Date: Wed, 9 May 2018 04:39:16 +0000 Subject: Merge csv-1.0.2 from upstream. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63364 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/csv/test_row.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'test/csv/test_row.rb') diff --git a/test/csv/test_row.rb b/test/csv/test_row.rb index 1cb851b027..23df4d4fe6 100755 --- a/test/csv/test_row.rb +++ b/test/csv/test_row.rb @@ -4,9 +4,7 @@ # tc_row.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" @@ -304,6 +302,17 @@ class TestCSV::Row < TestCSV end end + def test_each_pair + assert_equal([ + ["A", 1], + ["B", 2], + ["C", 3], + ["A", 4], + ["A", nil], + ], + @row.each_pair.to_a) + end + def test_enumerable assert_equal( [["A", 1], ["A", 4], ["A", nil]], @row.select { |pair| pair.first == "A" } ) @@ -323,7 +332,7 @@ class TestCSV::Row < TestCSV def test_to_hash hash = @row.to_hash - assert_equal({"A" => nil, "B" => 2, "C" => 3}, hash) + assert_equal({"A" => @row["A"], "B" => @row["B"], "C" => @row["C"]}, hash) hash.keys.each_with_index do |string_key, h| assert_predicate(string_key, :frozen?) assert_same(string_key, @row.headers[h]) @@ -377,4 +386,37 @@ class TestCSV::Row < TestCSV r = @row == [] assert_equal false, r end + + 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 end -- cgit v1.2.3