diff options
| author | Randy Morgan <[email protected]> | 2012-04-01 00:35:26 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-04-01 00:35:26 +0900 |
| commit | 22a341841f191a5aa00e87b1f166b4f25cc67f0a (patch) | |
| tree | 505f46708d5cac7d33d0dd6679c125e2eb819075 /test | |
| parent | bb2117ba17297e02a0fc6d5ad5a22462e72a9a79 (diff) | |
| download | caxlsx-22a341841f191a5aa00e87b1f166b4f25cc67f0a.tar.gz caxlsx-22a341841f191a5aa00e87b1f166b4f25cc67f0a.zip | |
part way through changing all serialization to use string concatenation prior to dropping Nokogiri dep in production.
Diffstat (limited to 'test')
| -rw-r--r-- | test/content_type/tc_content_type.rb | 10 | ||||
| -rw-r--r-- | test/content_type/tc_default.rb | 23 | ||||
| -rw-r--r-- | test/content_type/tc_override.rb | 21 | ||||
| -rw-r--r-- | test/doc_props/tc_app.rb | 7 | ||||
| -rw-r--r-- | test/doc_props/tc_core.rb | 4 | ||||
| -rw-r--r-- | test/rels/tc_relationships.rb | 4 | ||||
| -rw-r--r-- | test/workbook/tc_workbook.rb | 14 | ||||
| -rw-r--r-- | test/workbook/worksheet/table/tc_table.rb | 4 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_cell.rb | 7 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_page_margins.rb | 4 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_row.rb | 12 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 49 |
12 files changed, 41 insertions, 118 deletions
diff --git a/test/content_type/tc_content_type.rb b/test/content_type/tc_content_type.rb index 79141748..e353b1bb 100644 --- a/test/content_type/tc_content_type.rb +++ b/test/content_type/tc_content_type.rb @@ -4,7 +4,7 @@ require 'tc_helper.rb' class TestContentType < Test::Unit::TestCase def setup @package = Axlsx::Package.new - @doc = Nokogiri::XML(@package.send(:content_types).to_xml) + @doc = Nokogiri::XML(@package.send(:content_types).to_xml_string) end def test_valid_document @@ -51,12 +51,12 @@ class TestContentType < Test::Unit::TestCase o_path = "//xmlns:Override[@ContentType='%s']" ws = @package.workbook.add_worksheet - doc = Nokogiri::XML(@package.send(:content_types).to_xml) + doc = Nokogiri::XML(@package.send(:content_types).to_xml_string) assert_equal(doc.xpath("//xmlns:Override").size, 5, "adding a worksheet should add another type") assert_equal(doc.xpath(o_path % Axlsx::WORKSHEET_CT).last["PartName"], "/xl/#{ws.pn}", "Worksheet part invalid") ws = @package.workbook.add_worksheet - doc = Nokogiri::XML(@package.send(:content_types).to_xml) + doc = Nokogiri::XML(@package.send(:content_types).to_xml_string) assert_equal(doc.xpath("//xmlns:Override").size, 6, "adding workship should add another type") assert_equal(doc.xpath(o_path % Axlsx::WORKSHEET_CT).last["PartName"], "/xl/#{ws.pn}", "Worksheet part invalid") @@ -67,13 +67,13 @@ class TestContentType < Test::Unit::TestCase ws = @package.workbook.add_worksheet c = ws.add_chart Axlsx::Pie3DChart - doc = Nokogiri::XML(@package.send(:content_types).to_xml) + doc = Nokogiri::XML(@package.send(:content_types).to_xml_string) assert_equal(doc.xpath("//xmlns:Override").size, 7, "expected 7 types got #{doc.css("Types Override").size}") assert_equal(doc.xpath(o_path % Axlsx::DRAWING_CT).first["PartName"], "/xl/#{ws.drawing.pn}", "Drawing part name invlid") assert_equal(doc.xpath(o_path % Axlsx::CHART_CT).last["PartName"], "/xl/#{c.pn}", "Chart part name invlid") c = ws.add_chart Axlsx::Pie3DChart - doc = Nokogiri::XML(@package.send(:content_types).to_xml) + doc = Nokogiri::XML(@package.send(:content_types).to_xml_string) assert_equal(doc.xpath("//xmlns:Override").size, 8, "expected 7 types got #{doc.css("Types Override").size}") assert_equal(doc.xpath(o_path % Axlsx::CHART_CT).last["PartName"], "/xl/#{c.pn}", "Chart part name invlid") end diff --git a/test/content_type/tc_default.rb b/test/content_type/tc_default.rb index e5245b38..2fe0d965 100644 --- a/test/content_type/tc_default.rb +++ b/test/content_type/tc_default.rb @@ -2,10 +2,7 @@ require 'tc_helper.rb' class TestDefault < Test::Unit::TestCase - def setup - end - def teardown - end + def test_initialization_requires_Extension_and_ContentType assert_raise(ArgumentError, "raises argument error if Extension and/or ContentType are not specified") { Axlsx::Default.new } assert_raise(ArgumentError, "raises argument error if Extension and/or ContentType are not specified") { Axlsx::Default.new :Extension=>"xml" } @@ -18,21 +15,11 @@ class TestDefault < Test::Unit::TestCase assert_raise(ArgumentError, "raises argument error if invlalid ContentType is") { Axlsx::Default.new :ContentType=>"asdf" } end - def test_to_xml - schema = Nokogiri::XML::Schema(File.open(Axlsx::CONTENT_TYPES_XSD)) + def test_to_xml_string type = Axlsx::Default.new :Extension=>"xml", :ContentType=>Axlsx::XML_CT - builder = Nokogiri::XML::Builder.new(:encoding => Axlsx::ENCODING) do |xml| - xml.Types(:xmlns => Axlsx::XML_NS_T) { - type.to_xml(xml) - } - end - doc = Nokogiri::XML(builder.to_xml) - errors = [] - schema.validate(doc).each do |error| - puts error.message - errors << error - end - assert_equal(errors.size, 0, "[Content Types].xml Invalid" + errors.map{ |e| e.message }.to_s) + doc = Nokogiri::XML(type.to_xml_string) + assert_equal(doc.xpath("Default[@ContentType='#{Axlsx::XML_CT}']").size, 1) + assert_equal(doc.xpath("Default[@Extension='xml']").size, 1) end diff --git a/test/content_type/tc_override.rb b/test/content_type/tc_override.rb index 5005d12d..920f1667 100644 --- a/test/content_type/tc_override.rb +++ b/test/content_type/tc_override.rb @@ -2,10 +2,7 @@ require 'tc_helper.rb' class TestOverride < Test::Unit::TestCase - def setup - end - def teardown - end + def test_initialization_requires_Extension_and_ContentType err = "requires PartName and ContentType options" assert_raise(ArgumentError, err) { Axlsx::Override.new } @@ -19,20 +16,10 @@ class TestOverride < Test::Unit::TestCase end def test_to_xml - schema = Nokogiri::XML::Schema(File.open(Axlsx::CONTENT_TYPES_XSD)) type = Axlsx::Override.new :PartName=>"somechart.xml", :ContentType=>Axlsx::CHART_CT - builder = Nokogiri::XML::Builder.new(:encoding => Axlsx::ENCODING) do |xml| - xml.Types(:xmlns => Axlsx::XML_NS_T) { - type.to_xml(xml) - } - end - doc = Nokogiri::XML(builder.to_xml) - errors = [] - schema.validate(doc).each do |error| - puts error.message - errors << error - end - assert_equal(errors.size, 0, "Override content type caused invalid content_type doc" + errors.map{ |e| e.message }.to_s) + doc = Nokogiri::XML(type.to_xml_string) + assert_equal(doc.xpath("Override[@ContentType='#{Axlsx::CHART_CT}']").size, 1) + assert_equal(doc.xpath("Override[@PartName='somechart.xml']").size, 1) end diff --git a/test/doc_props/tc_app.rb b/test/doc_props/tc_app.rb index 31c87c41..bff2bb3d 100644 --- a/test/doc_props/tc_app.rb +++ b/test/doc_props/tc_app.rb @@ -1,14 +1,9 @@ require 'tc_helper.rb' class TestApp < Test::Unit::TestCase - def setup - end - def teardown - end - def test_valid_document schema = Nokogiri::XML::Schema(File.open(Axlsx::APP_XSD)) - doc = Nokogiri::XML(Axlsx::App.new.to_xml) + doc = Nokogiri::XML(Axlsx::App.new.to_xml_string) errors = [] schema.validate(doc).each do |error| errors << error diff --git a/test/doc_props/tc_core.rb b/test/doc_props/tc_core.rb index 5e75d812..e26d1236 100644 --- a/test/doc_props/tc_core.rb +++ b/test/doc_props/tc_core.rb @@ -4,7 +4,7 @@ class TestCore < Test::Unit::TestCase def setup @core = Axlsx::Core.new - @doc = Nokogiri::XML(@core.to_xml) + @doc = Nokogiri::XML(@core.to_xml_string) end def test_valid_document @@ -27,7 +27,7 @@ class TestCore < Test::Unit::TestCase def test_creator_as_option c = Axlsx::Core.new :creator => "some guy" - doc = Nokogiri::XML(c.to_xml) + doc = Nokogiri::XML(c.to_xml_string) assert(doc.xpath('//dc:creator').text == "some guy") end end diff --git a/test/rels/tc_relationships.rb b/test/rels/tc_relationships.rb index 9a76d1e5..356e4691 100644 --- a/test/rels/tc_relationships.rb +++ b/test/rels/tc_relationships.rb @@ -5,7 +5,7 @@ class TestRelationships < Test::Unit::TestCase def test_valid_document @rels = Axlsx::Relationships.new schema = Nokogiri::XML::Schema(File.open(Axlsx::RELS_XSD)) - doc = Nokogiri::XML(@rels.to_xml) + doc = Nokogiri::XML(@rels.to_xml_string) errors = [] schema.validate(doc).each do |error| puts error.message @@ -13,7 +13,7 @@ class TestRelationships < Test::Unit::TestCase end @rels << Axlsx::Relationship.new(Axlsx::WORKSHEET_R, "bar") - doc = Nokogiri::XML(@rels.to_xml) + doc = Nokogiri::XML(@rels.to_xml_string) errors = [] schema.validate(doc).each do |error| puts error.message diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index af2a7889..e6bb48a0 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -50,7 +50,7 @@ class TestWorkbook < Test::Unit::TestCase def test_to_xml schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(@wb.to_xml) + doc = Nokogiri::XML(@wb.to_xml_string) errors = [] schema.validate(doc).each do |error| errors.push error @@ -68,9 +68,19 @@ class TestWorkbook < Test::Unit::TestCase def test_to_xml_adds_worksheet_when_worksheets_is_empty assert(@wb.worksheets.empty?) - @wb.to_xml + @wb.to_xml_string assert(@wb.worksheets.size == 1) end + def test_to_xml_string_defined_names + @wb.add_worksheet do |sheet| + sheet.add_row [1, "two"] + sheet.auto_filter = "A1:B1" + end + doc = Nokogiri::XML(@wb.to_xml_string) + assert_equal(doc.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, @wb.worksheets[0].abs_auto_filter) + end + + end diff --git a/test/workbook/worksheet/table/tc_table.rb b/test/workbook/worksheet/table/tc_table.rb index fc29bf66..d4acf39a 100644 --- a/test/workbook/worksheet/table/tc_table.rb +++ b/test/workbook/worksheet/table/tc_table.rb @@ -56,10 +56,10 @@ class TestTable < Test::Unit::TestCase assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship") end - def test_to_xml + def test_to_xml_string table = @ws.add_table("A1:D5") schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(table.to_xml) + doc = Nokogiri::XML(table.to_xml_string) errors = [] schema.validate(doc).each do |error| errors.push error diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index 499f9209..4db0c7be 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -225,17 +225,14 @@ class TestCell < Test::Unit::TestCase end def test_to_xml_string - builder = Nokogiri::XML::Builder.new(:encoding => Axlsx::ENCODING) do |xml| - @c.to_xml(xml) - end - c_xml = Nokogiri::XML(builder.to_xml(:save_with => 0)) + c_xml = Nokogiri::XML(@c.to_xml_string(1,1)) assert_equal(c_xml.xpath("/c[@s=1]").size, 1) end def test_to_xml # TODO This could use some much more stringent testing related to the xml content generated! row = @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"] schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(@ws.to_xml) + doc = Nokogiri::XML(@ws.to_xml_string) errors = [] schema.validate(doc).each do |error| errors.push error diff --git a/test/workbook/worksheet/tc_page_margins.rb b/test/workbook/worksheet/tc_page_margins.rb index 4c5e43fa..386f47da 100644 --- a/test/workbook/worksheet/tc_page_margins.rb +++ b/test/workbook/worksheet/tc_page_margins.rb @@ -55,9 +55,7 @@ class TestPageMargins < Test::Unit::TestCase @pm.bottom = 1.4 @pm.header = 0.8 @pm.footer = 0.9 - xml = Nokogiri::XML::Builder.new - @pm.to_xml(xml) - doc = Nokogiri::XML.parse(xml.to_xml) + doc = Nokogiri::XML.parse(@pm.to_xml_string) assert_equal(1, doc.xpath(".//pageMargins[@left=1.1][@right=1.2][@top=1.3][@bottom=1.4][@header=0.8][@footer=0.9]").size) end diff --git a/test/workbook/worksheet/tc_row.rb b/test/workbook/worksheet/tc_row.rb index 47b0f054..d1507aa1 100644 --- a/test/workbook/worksheet/tc_row.rb +++ b/test/workbook/worksheet/tc_row.rb @@ -53,9 +53,7 @@ class TestRow < Test::Unit::TestCase end def test_to_xml_without_custom_height - xml = Nokogiri::XML::Builder.new - @row.to_xml(xml) - doc = Nokogiri::XML.parse(xml.to_xml) + doc = Nokogiri::XML.parse(@row.to_xml_string(0)) assert_equal(0, doc.xpath(".//row[@ht]").size) assert_equal(0, doc.xpath(".//row[@customHeight]").size) end @@ -72,12 +70,4 @@ class TestRow < Test::Unit::TestCase assert_equal(r_s_xml.xpath(".//row[@r=1][@ht=20][@customHeight=1]").size, 1) end - def test_to_xml_with_custom_height - @row.height = 20 - xml = Nokogiri::XML::Builder.new - @row.to_xml(xml) - doc = Nokogiri::XML.parse(xml.to_xml) - assert_equal(1, doc.xpath(".//row[@ht=20][@customHeight=1]").size) - end - end diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index 3a3232fe..499df864 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -202,8 +202,6 @@ class TestWorksheet < Test::Unit::TestCase @ws.auto_filter = "A1:B1" doc = Nokogiri::XML(@ws.to_xml_string) assert_equal(doc.xpath('//xmlns:worksheet/xmlns:autoFilter[@ref="A1:B1"]').size, 1) - doc2 = Nokogiri::XML(@wb.to_xml) - assert_equal(doc2.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, @ws.abs_auto_filter) end def test_to_xml_string_merge_cells @@ -240,13 +238,12 @@ class TestWorksheet < Test::Unit::TestCase def test_abs_auto_filter @ws.add_row [1, "two", 3] @ws.auto_filter = "A1:C1" - doc = Nokogiri::XML(@wb.to_xml) - assert_equal(doc.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, "'Sheet1'!$A$1:$C$1") + assert_equal(@ws.abs_auto_filter, "'Sheet1'!$A$1:$C$1") end - def test_to_xml + def test_to_xml_string schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(@ws.to_xml) + doc = Nokogiri::XML(@ws.to_xml_string) errors = [] schema.validate(doc).each do |error| errors.push error @@ -258,7 +255,7 @@ class TestWorksheet < Test::Unit::TestCase def test_valid_with_page_margins @ws.page_margins.set :left => 9 schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD)) - doc = Nokogiri::XML(@ws.to_xml) + doc = Nokogiri::XML(@ws.to_xml_string) errors = [] schema.validate(doc).each do |error| errors.push error @@ -286,20 +283,6 @@ class TestWorksheet < Test::Unit::TestCase assert_nothing_raised { @ws.name = Array.new(31, "A").join('') } end - def test_update_auto_with_data - # small = @ws.workbook.styles.add_style(:sz=>2) - # big = @ws.workbook.styles.add_style(:sz=>10) - - # @ws.add_row ["chasing windmills", "penut"], :style=>small - # assert(@ws.auto_fit_data.size == 2, "a data item for each column") - - # assert_equal(@ws.auto_fit_data[0], {:sz => 2, :longest => "chasing windmills", :fixed=>nil}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column") - - - # @ws.add_row ["mule"], :style=>big - # assert_equal(@ws.auto_fit_data[0], {:sz=>10,:longest=>"mule", :fixed=>nil}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column") - end - def test_set_fixed_width_column @ws.add_row ["mule", "donkey", "horse"], :widths => [20, :ignore, nil] assert(@ws.column_info.size == 3, "a data item for each column") @@ -307,35 +290,11 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(@ws.column_info[1].width, nil, ":ignore does not set any data") end - def test_fixed_widths_with_merged_cells - # @ws.add_row ["hey, I'm like really long and stuff so I think you will merge me."] - # @ws.merge_cells "A1:C1" - # @ws.add_row ["but Im Short!"], :widths=> [14.8] - # assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), 14.8) - end - - def test_fixed_width_to_auto - # @ws.add_row ["hey, I'm like really long and stuff so I think you will merge me."] - # @ws.merge_cells "A1:C1" - # @ws.add_row ["but Im Short!"], :widths=> [14.8] - # assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), 14.8) - # @ws.add_row ["no, I like auto!"], :widths=>[:auto] - # assert_equal(@ws.auto_fit_data[0][:fixed], nil) - end - - def test_auto_width - # assert(@ws.send(:auto_width, {:sz=>11, :longest=>"fisheries"}) > @ws.send(:auto_width, {:sz=>11, :longest=>"fish"}), "longer strings get a longer auto_width at the same font size") - - # assert(@ws.send(:auto_width, {:sz=>11, :longest=>"fish"}) < @ws.send(:auto_width, {:sz=>12, :longest=>"fish"}), "larger fonts produce longer with with same string") - # assert_equal(@ws.send(:auto_width, {:sz=>11, :longest => "This is a really long string", :fixed=>0.2}), 0.2, "fixed rules!") - end - def test_fixed_height @ws.add_row [1, 2, 3], :height => 40 assert_equal(40, @ws.rows[-1].height) end - def test_set_column_width @ws.add_row ["chasing windmills", "penut"] @ws.column_widths nil, 0.5 |
