diff options
| author | Randy Morgan <[email protected]> | 2012-05-04 16:57:00 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2012-05-04 16:57:00 +0900 |
| commit | 754e2226d618260c4895cf15e54f5c8190345f8e (patch) | |
| tree | e1c433db807764c1816e1d823437009f3b3e35e3 /test | |
| parent | 5d221c2e01336b8771d15cf89a87fcb0f6e424b5 (diff) | |
| parent | 58effe424218aa8ba6aa8157b49233df05646308 (diff) | |
| download | caxlsx-754e2226d618260c4895cf15e54f5c8190345f8e.tar.gz caxlsx-754e2226d618260c4895cf15e54f5c8190345f8e.zip | |
Merge branch 'master' of github.com:randym/axlsx
Diffstat (limited to 'test')
| -rw-r--r-- | test/util/tc_validators.rb | 29 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_page_setup.rb | 103 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_print_options.rb | 72 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 61 |
4 files changed, 258 insertions, 7 deletions
diff --git a/test/util/tc_validators.rb b/test/util/tc_validators.rb index d4f98d05..2a0a0229 100644 --- a/test/util/tc_validators.rb +++ b/test/util/tc_validators.rb @@ -70,6 +70,35 @@ class TestValidators < Test::Unit::TestCase assert_raise(ArgumentError) { Axlsx.validate_relationship_type "http://some.url" } assert_raise(ArgumentError) { Axlsx.validate_relationship_type false } + #number_with_unit + assert_nothing_raised { Axlsx.validate_number_with_unit "210mm" } + assert_nothing_raised { Axlsx.validate_number_with_unit "8.5in" } + assert_nothing_raised { Axlsx.validate_number_with_unit "29.7cm" } + assert_nothing_raised { Axlsx.validate_number_with_unit "120pt" } + assert_nothing_raised { Axlsx.validate_number_with_unit "0pc" } + assert_nothing_raised { Axlsx.validate_number_with_unit "12.34pi" } + assert_raise(ArgumentError) { Axlsx.validate_number_with_unit nil } + assert_raise(ArgumentError) { Axlsx.validate_number_with_unit "210" } + assert_raise(ArgumentError) { Axlsx.validate_number_with_unit 210 } + assert_raise(ArgumentError) { Axlsx.validate_number_with_unit "mm" } + assert_raise(ArgumentError) { Axlsx.validate_number_with_unit "-29cm" } + + #page_scale + assert_nothing_raised { Axlsx.validate_page_scale 10 } + assert_nothing_raised { Axlsx.validate_page_scale 100 } + assert_nothing_raised { Axlsx.validate_page_scale 400 } + assert_raise(ArgumentError) { Axlsx.validate_page_scale 9 } + assert_raise(ArgumentError) { Axlsx.validate_page_scale 10.0 } + assert_raise(ArgumentError) { Axlsx.validate_page_scale 400.1 } + assert_raise(ArgumentError) { Axlsx.validate_page_scale "99" } + + #page_orientation + assert_nothing_raised { Axlsx.validate_page_orientation :default } + assert_nothing_raised { Axlsx.validate_page_orientation :landscape } + assert_nothing_raised { Axlsx.validate_page_orientation :portrait } + assert_raise(ArgumentError) { Axlsx.validate_page_orientation nil } + assert_raise(ArgumentError) { Axlsx.validate_page_orientation 1 } + assert_raise(ArgumentError) { Axlsx.validate_page_orientation "landscape" } end end diff --git a/test/workbook/worksheet/tc_page_setup.rb b/test/workbook/worksheet/tc_page_setup.rb new file mode 100644 index 00000000..ff323c75 --- /dev/null +++ b/test/workbook/worksheet/tc_page_setup.rb @@ -0,0 +1,103 @@ +require 'tc_helper.rb' + +class TestPageSetup < Test::Unit::TestCase + + def setup + p = Axlsx::Package.new + ws = p.workbook.add_worksheet :name => "hmmm" + @ps = ws.page_setup + end + + def test_initialize + assert_equal(nil, @ps.fit_to_height) + assert_equal(nil, @ps.fit_to_width) + assert_equal(nil, @ps.orientation) + assert_equal(nil, @ps.paper_height) + assert_equal(nil, @ps.paper_width) + assert_equal(nil, @ps.scale) + end + + def test_initialize_with_options + optioned = Axlsx::PageSetup.new(:fit_to_height => 1, :fit_to_width => 2, :orientation => :landscape, :paper_height => "297mm", :paper_width => "210mm", :scale => 50) + assert_equal(1, optioned.fit_to_height) + assert_equal(2, optioned.fit_to_width) + assert_equal(:landscape, optioned.orientation) + assert_equal("297mm", optioned.paper_height) + assert_equal("210mm", optioned.paper_width) + assert_equal(50, optioned.scale) + end + + def test_set_all_values + @ps.set(:fit_to_height => 1, :fit_to_width => 2, :orientation => :landscape, :paper_height => "297mm", :paper_width => "210mm", :scale => 50) + assert_equal(1, @ps.fit_to_height) + assert_equal(2, @ps.fit_to_width) + assert_equal(:landscape, @ps.orientation) + assert_equal("297mm", @ps.paper_height) + assert_equal("210mm", @ps.paper_width) + assert_equal(50, @ps.scale) + end + + def test_set_some_values + @ps.set(:fit_to_width => 2, :orientation => :portrait) + assert_equal(2, @ps.fit_to_width) + assert_equal(:portrait, @ps.orientation) + assert_equal(nil, @ps.fit_to_height) + assert_equal(nil, @ps.paper_height) + assert_equal(nil, @ps.paper_width) + assert_equal(nil, @ps.scale) + end + + def test_to_xml_all_values + @ps.set(:fit_to_height => 1, :fit_to_width => 2, :orientation => :landscape, :paper_height => "297mm", :paper_width => "210mm", :scale => 50) + doc = Nokogiri::XML.parse(@ps.to_xml_string) + assert_equal(1, doc.xpath(".//pageSetup[@fitToHeight='1'][@fitToWidth='2'][@orientation='landscape'][@paperHeight='297mm'][@paperWidth='210mm'][@scale='50']").size) + end + + def test_to_xml_some_values + @ps.set(:orientation => :portrait) + doc = Nokogiri::XML.parse(@ps.to_xml_string) + assert_equal(1, doc.xpath(".//pageSetup[@orientation='portrait']").size) + assert_equal(0, doc.xpath(".//pageSetup[@fitToHeight]").size) + assert_equal(0, doc.xpath(".//pageSetup[@fitToWidth]").size) + assert_equal(0, doc.xpath(".//pageSetup[@paperHeight]").size) + assert_equal(0, doc.xpath(".//pageSetup[@paperWidth]").size) + assert_equal(0, doc.xpath(".//pageSetup[@scale]").size) + end + + def test_fit_to_height + assert_raise(ArgumentError) { @ps.fit_to_height = 1.5 } + assert_nothing_raised { @ps.fit_to_height = 2 } + assert_equal(2, @ps.fit_to_height) + end + + def test_fit_to_width + assert_raise(ArgumentError) { @ps.fit_to_width = false } + assert_nothing_raised { @ps.fit_to_width = 1 } + assert_equal(1, @ps.fit_to_width) + end + + def test_orientation + assert_raise(ArgumentError) { @ps.orientation = "" } + assert_nothing_raised { @ps.orientation = :default } + assert_equal(:default, @ps.orientation) + end + + def test_paper_height + assert_raise(ArgumentError) { @ps.paper_height = 99 } + assert_nothing_raised { @ps.paper_height = "11in" } + assert_equal("11in", @ps.paper_height) + end + + def test_paper_width + assert_raise(ArgumentError) { @ps.paper_width = "22" } + assert_nothing_raised { @ps.paper_width = "29.7cm" } + assert_equal("29.7cm", @ps.paper_width) + end + + def test_scale + assert_raise(ArgumentError) { @ps.scale = 50.5 } + assert_nothing_raised { @ps.scale = 99 } + assert_equal(99, @ps.scale) + end + +end diff --git a/test/workbook/worksheet/tc_print_options.rb b/test/workbook/worksheet/tc_print_options.rb new file mode 100644 index 00000000..37ff7fe5 --- /dev/null +++ b/test/workbook/worksheet/tc_print_options.rb @@ -0,0 +1,72 @@ +require 'tc_helper.rb' + +class TestPrintOptions < Test::Unit::TestCase + + def setup + p = Axlsx::Package.new + ws = p.workbook.add_worksheet :name => "hmmm" + @po = ws.print_options + end + + def test_initialize + assert_equal(false, @po.grid_lines) + assert_equal(false, @po.headings) + assert_equal(false, @po.horizontal_centered) + assert_equal(false, @po.vertical_centered) + end + + def test_initialize_with_options + optioned = Axlsx::PrintOptions.new(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true) + assert_equal(true, optioned.grid_lines) + assert_equal(true, optioned.headings) + assert_equal(true, optioned.horizontal_centered) + assert_equal(true, optioned.vertical_centered) + end + + def test_set_all_values + @po.set(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true) + assert_equal(true, @po.grid_lines) + assert_equal(true, @po.headings) + assert_equal(true, @po.horizontal_centered) + assert_equal(true, @po.vertical_centered) + end + + def test_set_some_values + @po.set(:grid_lines => true, :headings => true) + assert_equal(true, @po.grid_lines) + assert_equal(true, @po.headings) + assert_equal(false, @po.horizontal_centered) + assert_equal(false, @po.vertical_centered) + end + + def test_to_xml + @po.set(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true) + doc = Nokogiri::XML.parse(@po.to_xml_string) + assert_equal(1, doc.xpath(".//printOptions[@gridLines='true'][@headings='true'][@horizontalCentered='true'][@verticalCentered='true']").size) + end + + def test_grid_lines + assert_raise(ArgumentError) { @po.grid_lines = 99 } + assert_nothing_raised { @po.grid_lines = true } + assert_equal(@po.grid_lines, true) + end + + def test_headings + assert_raise(ArgumentError) { @po.headings = 99 } + assert_nothing_raised { @po.headings = true } + assert_equal(@po.headings, true) + end + + def test_horizontal_centered + assert_raise(ArgumentError) { @po.horizontal_centered = 99 } + assert_nothing_raised { @po.horizontal_centered = true } + assert_equal(@po.horizontal_centered, true) + end + + def test_vertical_centered + assert_raise(ArgumentError) { @po.vertical_centered = 99 } + assert_nothing_raised { @po.vertical_centered = true } + assert_equal(@po.vertical_centered, true) + end + +end diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index d5416e64..dbb48887 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -25,6 +25,28 @@ class TestWorksheet < Test::Unit::TestCase end end + def test_page_setup + assert(@ws.page_setup.is_a? Axlsx::PageSetup) + end + + def test_page_setup_yield + @ws.page_setup do |ps| + assert(ps.is_a? Axlsx::PageSetup) + assert(@ws.page_setup == ps) + end + end + + def test_print_options + assert(@ws.print_options.is_a? Axlsx::PrintOptions) + end + + def test_print_options_yield + @ws.print_options do |po| + assert(po.is_a? Axlsx::PrintOptions) + assert(@ws.print_options == po) + end + end + def test_no_autowidth @ws.workbook.use_autowidth = false @ws.add_row [1,2,3,4] @@ -33,13 +55,18 @@ class TestWorksheet < Test::Unit::TestCase def test_initialization_options page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2} - optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :selected => true, :show_gridlines => false) - assert_equal(optioned.page_margins.left, page_margins[:left]) - assert_equal(optioned.page_margins.right, page_margins[:right]) - assert_equal(optioned.page_margins.top, page_margins[:top]) - assert_equal(optioned.page_margins.bottom, page_margins[:bottom]) - assert_equal(optioned.page_margins.header, page_margins[:header]) - assert_equal(optioned.page_margins.footer, page_margins[:footer]) + page_setup = {:fit_to_height => 1, :fit_to_width => 1, :orientation => :landscape, :paper_width => "210mm", :paper_height => "297mm", :scale => 80} + print_options = {:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true} + optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :page_setup => page_setup, :print_options => print_options, :selected => true, :show_gridlines => false) + page_margins.keys.each do |key| + assert_equal(page_margins[key], optioned.page_margins.send(key)) + end + page_setup.keys.each do |key| + assert_equal(page_setup[key], optioned.page_setup.send(key)) + end + print_options.keys.each do |key| + assert_equal(print_options[key], optioned.print_options.send(key)) + end assert_equal(optioned.name, 'bob') assert_equal(optioned.selected, true) assert_equal(optioned.show_gridlines, false) @@ -223,6 +250,24 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageMargins[@left="9"][@right="7"]').size, 1) end + def test_to_xml_string_page_setup + @ws.page_setup do |ps| + ps.paper_width = "210mm" + ps.scale = 80 + end + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageSetup[@paperWidth="210mm"][@scale="80"]').size, 1) + end + + def test_to_xml_string_print_options + @ws.print_options do |po| + po.grid_lines = true + po.horizontal_centered = true + end + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:printOptions[@gridLines="true"][@horizontalCentered="true"]').size, 1) + end + def test_to_xml_string_drawing c = @ws.add_chart Axlsx::Pie3DChart doc = Nokogiri::XML(@ws.to_xml_string) @@ -259,6 +304,8 @@ class TestWorksheet < Test::Unit::TestCase # is generated in correct order. def test_valid_with_optional_elements @ws.page_margins.set :left => 9 + @ws.page_setup.set :fit_to_width => 1 + @ws.print_options.set :headings => true @ws.auto_filter = "A1:C3" @ws.merge_cells "A4:A5" @ws.add_chart Axlsx::Pie3DChart |
