test/test_row.rb in workbook-0.8.1 vs test/test_row.rb in workbook-0.8.2

- old
+ new

@@ -1,35 +1,29 @@ # frozen_string_literal: true -# -*- encoding : utf-8 -*- -require File.join(File.dirname(__FILE__), 'helper') +require File.join(File.dirname(__FILE__), "helper") class TestRow < Minitest::Test - - def test_init t = Workbook::Table.new - r = Workbook::Row.new([1,2,3],t) - c1 = Workbook::Cell.new(1) - c2 = Workbook::Cell.new(2) - c3 = Workbook::Cell.new(3) - assert_equal([c1,c2,c3].collect{|c| c.value},r.collect{|c| c.value}) + r = Workbook::Row.new([1, 2, 3], t) + assert_equal([1, 2, 3], r.collect { |c| c.value }) - #t = Workbook::Table.new + # t = Workbook::Table.new c1 = Workbook::Cell.new(1) c2 = Workbook::Cell.new(2) c3 = Workbook::Cell.new(3) - r = Workbook::Row.new([c1,c2,c3]) + r = Workbook::Row.new([c1, c2, c3]) - assert_equal([c1,c2,c3],r) - + assert_equal([c1, c2, c3], r.cells) + assert_equal(Workbook::Row, r.class) end def test_table= r = Workbook::Row.new - assert_raises(ArgumentError, 'table should be a Workbook::Table (you passed a String)') { r.table = "asdf" } + assert_raises(ArgumentError, "table should be a Workbook::Table (you passed a String)") { r.table = "asdf" } r.table = nil assert_nil(r.table) r = Workbook::Row.new t = Workbook::Table.new @@ -72,55 +66,57 @@ def test_no_values? t = Workbook::Table.new r1 = Workbook::Row.new r1.table = t assert_equal(true, r1.no_values?) - r1 << Workbook::Cell.new('abcd') + r1 << Workbook::Cell.new("abcd") assert_equal(false, r1.no_values?) - r2 = Workbook::Row.new [nil, '', nil, '', ''] + r2 = Workbook::Row.new [nil, "", nil, "", ""] r2.table = t assert_equal(true, r2.no_values?) end def test_to_symbols r1 = Workbook::Row.new ["test", "asdf-asd", "asdf - asdf", "asdf2"] assert_equal([:test, :asdfasd, :asdf_asdf, :asdf2], r1.to_symbols) r1 = Workbook::Row.new ["inït", "è-éë"] assert_equal([:init, :eee], r1.to_symbols) - end def test_to_hash r1 = Workbook::Row.new ["test", "asdf-asd", "asdf - asdf", "asdf2"] - assert_raises(NoMethodError, 'undefined method `header\' for nil:NilClass') { r1.to_hash } + assert_raises(NoMethodError, "undefined method `header' for nil:NilClass") { r1.to_hash } t = Workbook::Table.new - r1 = Workbook::Row.new ["test", "asdf-asd"] + r1 = Workbook::Row.new ["test", "asdf-asd"] r1.table = t - expected = {:test=>Workbook::Cell.new("test"), :asdfasd=>Workbook::Cell.new("asdf-asd")} + expected = {test: Workbook::Cell.new("test"), asdfasd: Workbook::Cell.new("asdf-asd")} assert_equal(expected, r1.to_hash) date = DateTime.now - r2 = Workbook::Row.new [2, date] + r2 = Workbook::Row.new [2, date] r2.table = t - expected = {:test=>Workbook::Cell.new(2), :asdfasd=>Workbook::Cell.new(date)} + expected = {test: Workbook::Cell.new(2), asdfasd: Workbook::Cell.new(date)} assert_equal(expected, r2.to_hash) assert_equal(date, r2[:asdfasd].value) assert_equal(date, r2[1].value) end - def test_to_hash_with_values t = Workbook::Table.new - r1 = Workbook::Row.new ["test", "asdf-asd"] + r1 = Workbook::Row.new ["test", "asdf-asd"] r1.table = t - expected = {:test=>"test", :asdfasd=>"asdf-asd"} + expected = {test: "test", asdfasd: "asdf-asd"} assert_equal(expected, r1.to_hash_with_values) date = DateTime.now - r2 = Workbook::Row.new [2, date] + r2 = Workbook::Row.new [2, date] r2.table = t - expected = {:test=>2, :asdfasd=>date} + expected = {test: 2, asdfasd: date} assert_equal(expected, r2.to_hash_with_values) + r3 = Workbook::Row.new [4] + r3.table = t + expected = {test: 4, asdfasd: nil} + assert_equal(expected, r3.to_hash_with_values) end def test_to_hash_cache t = Workbook::Table.new t << ["test", "asdf-asd"] @@ -135,51 +131,50 @@ r[:test] = 5 assert_equal(5, r[:test].value) end def test_compare - r1 = Workbook::Row.new ["test", "asdf-asd"] - r2 = Workbook::Row.new [nil, "asdf-asd"] - assert_equal(-1,r1<=>r2) - r1 = Workbook::Row.new [1, "asdf-asd"] - r2 = Workbook::Row.new ["test", "asdf-asd"] - assert_equal(-1,r1<=>r2) - r1 = Workbook::Row.new [nil, "asdf-asd"] - r2 = Workbook::Row.new [Time.now, "asdf-asd"] - assert_equal(1,r1<=>r2) - r1 = Workbook::Row.new [2, 3] - r2 = Workbook::Row.new [2, nil] - assert_equal(-1,r1<=>r2) - r1 = Workbook::Row.new [3, 0] - r2 = Workbook::Row.new [2, 100000] - assert_equal(1,r1<=>r2) - r1 = Workbook::Row.new [-10, 3] - r2 = Workbook::Row.new [nil, 5] - assert_equal(-1,r1<=>r2) - + r1 = Workbook::Row.new ["test", "asdf-asd"] + r2 = Workbook::Row.new [nil, "asdf-asd"] + assert_equal(-1, r1 <=> r2) + r1 = Workbook::Row.new [1, "asdf-asd"] + r2 = Workbook::Row.new ["test", "asdf-asd"] + assert_equal(-1, r1 <=> r2) + r1 = Workbook::Row.new [nil, "asdf-asd"] + r2 = Workbook::Row.new [Time.now, "asdf-asd"] + assert_equal(1, r1 <=> r2) + r1 = Workbook::Row.new [2, 3] + r2 = Workbook::Row.new [2, nil] + assert_equal(-1, r1 <=> r2) + r1 = Workbook::Row.new [3, 0] + r2 = Workbook::Row.new [2, 100000] + assert_equal(1, r1 <=> r2) + r1 = Workbook::Row.new [-10, 3] + r2 = Workbook::Row.new [nil, 5] + assert_equal(-1, r1 <=> r2) end def test_find_cells_by_background_color - r = Workbook::Row.new ["test", "asdf-asd"] - assert_equal([],r.find_cells_by_background_color) + r = Workbook::Row.new ["test", "asdf-asd"] + assert_equal([], r.find_cells_by_background_color) f = Workbook::Format.new - f[:background_color]='#ff00ff' + f[:background_color] = "#ff00ff" r.first.format = f - assert_equal([:test],r.find_cells_by_background_color) - assert_equal([],r.find_cells_by_background_color('#ff0000')) + assert_equal([:test], r.find_cells_by_background_color) + assert_equal([], r.find_cells_by_background_color("#ff0000")) end def test_to_s r1 = Workbook::Row.new ["test", "asdf-asd"] - assert_equal("test,asdf-asd\n",r1.to_csv) + assert_equal("test,asdf-asd\n", r1.to_csv) end def test_clone b = Workbook::Book.new table = b.sheet.table - table << Workbook::Row.new(["a","b"]) - row = Workbook::Row.new(["1","2"]) + table << Workbook::Row.new(["a", "b"]) + row = Workbook::Row.new(["1", "2"]) table << row table << row row[1] = Workbook::Cell.new(3) table << table[1].clone table.last[1].value = 5 @@ -187,91 +182,91 @@ end def test_clone_has_no_table b = Workbook::Book.new table = b.sheet.table - table << Workbook::Row.new(["a","b"]) - table << Workbook::Row.new([1,2]) + table << Workbook::Row.new(["a", "b"]) + table << Workbook::Row.new([1, 2]) row = table[1].clone assert_nil(row[:a]) assert_nil(row[:b]) - assert_equal(1,row[0].value) - assert_equal(2,row[1].value) + assert_equal(1, row[0].value) + assert_equal(2, row[1].value) end def test_push b = Workbook::Book.new table = b.sheet.table - table << Workbook::Row.new(["a","b"]) - table << Workbook::Row.new([1,2]) - assert_equal(1,table[1][:a].value) - assert_equal(2,table[1][:b].value) + table << Workbook::Row.new(["a", "b"]) + table << Workbook::Row.new([1, 2]) + assert_equal(1, table[1][:a].value) + assert_equal(2, table[1][:b].value) b = Workbook::Book.new table = b.sheet.table - table.push Workbook::Row.new(["a","b"]) - table.push Workbook::Row.new([1,2]) - assert_equal(1,table[1][:a].value) - assert_equal(2,table[1][:b].value) + table.push Workbook::Row.new(["a", "b"]) + table.push Workbook::Row.new([1, 2]) + assert_equal(1, table[1][:a].value) + assert_equal(2, table[1][:b].value) end def test_assign b = Workbook::Book.new table = b.sheet.table - table.push Workbook::Row.new(["a","b"]) - table[1] = Workbook::Row.new([1,2]) - assert_equal(1,table[1][:a].value) - assert_equal(2,table[1][:b].value) + table.push Workbook::Row.new(["a", "b"]) + table[1] = Workbook::Row.new([1, 2]) + assert_equal(1, table[1][:a].value) + assert_equal(2, table[1][:b].value) b = Workbook::Book.new table = b.sheet.table - table.push Workbook::Row.new(["a","b"]) - table[1] = [1,2] - assert_equal(1,table[1][:a].value) - assert_equal(2,table[1][:b].value) + table.push Workbook::Row.new(["a", "b"]) + table[1] = [1, 2] + assert_equal(1, table[1][:a].value) + assert_equal(2, table[1][:b].value) end def test_preservation_of_format_on_assign - row = Workbook::Row.new([1,2]) + row = Workbook::Row.new([1, 2]) cellformat = row.first.format - cellformat["background"]="#f00" + cellformat["background"] = "#f00" row[0] = 3 - assert_equal(3,row[0].value) - assert_equal("#f00",row[0].format["background"]) + assert_equal(3, row[0].value) + assert_equal("#f00", row[0].format["background"]) end def test_find_by_string b = Workbook::Book.new table = b.sheet.table - table << Workbook::Row.new(["a","b"]) - row = Workbook::Row.new([],table) - row[1]= 12 + table << Workbook::Row.new(["a", "b"]) + row = Workbook::Row.new([], table) + row[1] = 12 assert_equal(12, table.last["b"]) assert_nil(table.last["a"]) end def test_find_by_column_string b = Workbook::Book.new table = b.sheet.table - table << Workbook::Row.new(["b","a"]) - row = Workbook::Row.new([],table) - row[1]= 12 + table << Workbook::Row.new(["b", "a"]) + row = Workbook::Row.new([], table) + row[1] = 12 assert_equal(12, table.last["B"]) assert_nil(table.last["A"]) end def test_row_hash_index_string_assignment b = Workbook::Book.new table = b.sheet.table - table << Workbook::Row.new(["a","b","d"]) - row = Workbook::Row.new([],table) - row[1]= 12 + table << Workbook::Row.new(["a", "b", "d"]) + row = Workbook::Row.new([], table) + row[1] = 12 assert_equal(12, table.last.last.value) - row[:b]= 15 + row[:b] = 15 assert_equal(15, table.last.last.value) - row["b"]= 18 + row["b"] = 18 assert_equal(18, table.last.last.value) - row["C"]= 2 + row["C"] = 2 assert_equal(2, table.last[2].value) end def test_trim! a = Workbook::Row.new @@ -338,52 +333,53 @@ c[0] = nil c[1] = 2 d = a.trim assert_equal(b, a) assert_equal(c, d) - end def test_add a = Workbook::Row.new a << 1 a << 2 a << "asdf" a << 2.2 a.push(5) - assert_equal(1,a[0].value) - assert_equal(2,a[1].value) - assert_equal("asdf",a[2].value) - assert_equal(2.2,a[3].value) - assert_equal(5,a[4].value) + assert_equal(1, a[0].value) + assert_equal(2, a[1].value) + assert_equal("asdf", a[2].value) + assert_equal(2.2, a[3].value) + assert_equal(5, a[4].value) end def test_plus - header = Workbook::Row.new([:a,:b]) + header = Workbook::Row.new([:a, :b]) a = Workbook::Row.new table = Workbook::Table.new table << header table << a assert_equal(table, a.table) - assert_equal(Workbook::Row, (a + [1,1]).class ) - assert_equal([1,1],(a + [1,1]).to_a ) - assert_equal(Workbook::Cell,(a + [1,1])[0].class ) - a += [1,1] - assert_equal([1,1],a.to_a ) - assert_equal(Workbook::Row, a.class ) + assert_equal(Workbook::Row, (a + [1, 1]).class) + assert_equal([1, 1], (a + [1, 1]).to_a) + assert_equal(Workbook::Cell, (a + [1, 1])[0].class) + a += [1, 1] + assert_equal([1, 1], a.to_a) + assert_equal(Workbook::Row, a.class) assert_nil(a.table) - assert_equal(Workbook::Cell,a[0].class) + assert_equal(Workbook::Cell, a[0].class) end def test_concat - header = Workbook::Row.new([:a,:b]) + header = Workbook::Row.new([:a, :b]) a = Workbook::Row.new table = Workbook::Table.new table << header table << a - a.concat [1,1] - assert_equal([1,1],a.to_a ) - assert_equal(Workbook::Row, a.class ) + + a.concat [1, 1] + + assert_equal([1, 1], a.to_a) + assert_equal(Workbook::Row, a.class) assert_equal(table, a.table) - assert_equal(Workbook::Cell,a[0].class) + assert_equal(Workbook::Cell, a[0].class) end end