summaryrefslogtreecommitdiffhomepage
path: root/test/workbook
diff options
context:
space:
mode:
authorJean Jacques Warmerdam <[email protected]>2013-07-24 12:32:54 +0200
committerJean Jacques Warmerdam <[email protected]>2013-07-24 12:32:54 +0200
commit88ee2b1ca8aee6bd14c838f247654f43c073fd2e (patch)
treef5414ad991971477ac7b0286ebb3067dc5ca2cf7 /test/workbook
parent35d3cc8b21bce0c4ce7b9ec4e29d52df4b5f9cc4 (diff)
parent7fb6629b6f1e56b3e012613ec8cfcda8628c0ca5 (diff)
downloadcaxlsx-88ee2b1ca8aee6bd14c838f247654f43c073fd2e.tar.gz
caxlsx-88ee2b1ca8aee6bd14c838f247654f43c073fd2e.zip
Merge branch 'master' of https://github.com/randym/axlsx
Diffstat (limited to 'test/workbook')
-rw-r--r--test/workbook/tc_shared_strings_table.rb6
-rw-r--r--test/workbook/tc_workbook.rb24
-rw-r--r--test/workbook/worksheet/tc_comment.rb27
-rw-r--r--test/workbook/worksheet/tc_comments.rb4
-rw-r--r--test/workbook/worksheet/tc_conditional_formatting.rb9
-rw-r--r--test/workbook/worksheet/tc_pivot_table.rb49
-rw-r--r--test/workbook/worksheet/tc_pivot_table_cache_definition.rb2
-rw-r--r--test/workbook/worksheet/tc_table.rb4
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb19
-rw-r--r--test/workbook/worksheet/tc_worksheet_hyperlink.rb13
10 files changed, 106 insertions, 51 deletions
diff --git a/test/workbook/tc_shared_strings_table.rb b/test/workbook/tc_shared_strings_table.rb
index e3c9bf7b..7a333f4f 100644
--- a/test/workbook/tc_shared_strings_table.rb
+++ b/test/workbook/tc_shared_strings_table.rb
@@ -24,6 +24,12 @@ class TestSharedStringsTable < Test::Unit::TestCase
assert_equal(sst.unique_count, 4)
end
+ def test_uses_workbook_xml_space
+ assert_equal(@p.workbook.xml_space, @p.workbook.shared_strings.xml_space)
+ @p.workbook.xml_space = :default
+ assert_equal(:default, @p.workbook.shared_strings.xml_space)
+ end
+
def test_valid_document
schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string)
diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb
index 1a9b9669..32b6935a 100644
--- a/test/workbook/tc_workbook.rb
+++ b/test/workbook/tc_workbook.rb
@@ -9,6 +9,23 @@ class TestWorkbook < Test::Unit::TestCase
def teardown
end
+ def test_worksheet_users_xml_space
+ sheet = @wb.add_worksheet(:name => 'foo')
+ ws_xml = Nokogiri::XML(sheet.to_xml_string)
+ assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='preserve'"))
+
+ @wb.xml_space = :default
+ ws_xml = Nokogiri::XML(sheet.to_xml_string)
+ assert(ws_xml.xpath("//xmlns:worksheet/@xml:space='default'"))
+ end
+
+ def test_xml_space
+ assert_equal(:preserve, @wb.xml_space)
+ @wb.xml_space = :default
+ assert_equal(:default, @wb.xml_space)
+ assert_raise(ArgumentError) { @wb.xml_space = :none }
+ end
+
def test_no_autowidth
assert_equal(@wb.use_autowidth, true)
assert_raise(ArgumentError) {@wb.use_autowidth = 0.1}
@@ -99,5 +116,10 @@ class TestWorkbook < Test::Unit::TestCase
assert_equal(doc.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, @wb.worksheets[0].auto_filter.defined_name)
end
-
+ def test_to_xml_uses_correct_rIds_for_pivotCache
+ ws = @wb.add_worksheet
+ pivot_table = ws.add_pivot_table('G5:G6', 'A1:D5')
+ doc = Nokogiri::XML(@wb.to_xml_string)
+ assert_equal pivot_table.cache_definition.rId, doc.xpath("//xmlns:pivotCache").first["r:id"]
+ end
end
diff --git a/test/workbook/worksheet/tc_comment.rb b/test/workbook/worksheet/tc_comment.rb
index 0cb79bcc..e66abb9a 100644
--- a/test/workbook/worksheet/tc_comment.rb
+++ b/test/workbook/worksheet/tc_comment.rb
@@ -5,7 +5,7 @@ class TestComment < Test::Unit::TestCase
p = Axlsx::Package.new
wb = p.workbook
@ws = wb.add_worksheet
- @c1 = @ws.add_comment :ref => 'A1', :text => 'penut machine', :author => 'crank'
+ @c1 = @ws.add_comment :ref => 'A1', :text => 'text with special char <', :author => 'author with special char <', :visible => false
@c2 = @ws.add_comment :ref => 'C3', :text => 'rust bucket', :author => 'PO'
end
@@ -14,12 +14,12 @@ class TestComment < Test::Unit::TestCase
end
def test_author
- assert(@c1.author == 'crank')
+ assert(@c1.author == 'author with special char <')
assert(@c2.author == 'PO')
end
def test_text
- assert(@c1.text == 'penut machine')
+ assert(@c1.text == 'text with special char <')
assert(@c2.text == 'rust bucket')
end
@@ -28,6 +28,10 @@ class TestComment < Test::Unit::TestCase
assert_equal(@c2.author_index, 0)
end
+ def test_visible
+ assert_equal(false, @c1.visible)
+ assert_equal(true, @c2.visible)
+ end
def test_ref
assert(@c1.ref == 'A1')
assert(@c2.ref == 'C3')
@@ -45,13 +49,24 @@ class TestComment < Test::Unit::TestCase
assert(@c1.vml_shape.bottom_row == pos[1]+4)
end
- def to_xml_string
+ def test_to_xml_string
doc = Nokogiri::XML(@c1.to_xml_string)
assert_equal(doc.xpath("//comment[@ref='#{@c1.ref}']").size, 1)
- assert_equal(doc.xpath("//comment[@authorId='#{@c1.author_index.to}']").size, 1)
- assert_equal(doc.xpath("//t[text()='#{@c1.author}']").size, 1)
+ assert_equal(doc.xpath("//comment[@authorId='#{@c1.author_index.to_s}']").size, 1)
+ assert_equal(doc.xpath("//t[text()='#{@c1.author}:\n']").size, 1)
assert_equal(doc.xpath("//t[text()='#{@c1.text}']").size, 1)
end
+ def test_comment_text_contain_author_and_text
+ comment = @ws.add_comment :ref => 'C4', :text => 'some text', :author => 'Bob'
+ doc = Nokogiri::XML(comment.to_xml_string)
+ assert_equal("Bob:\nsome text", doc.xpath("//comment/text").text)
+ end
+
+ def test_comment_text_does_not_contain_stray_colon_if_author_blank
+ comment = @ws.add_comment :ref => 'C5', :text => 'some text', :author => ''
+ doc = Nokogiri::XML(comment.to_xml_string)
+ assert_equal("some text", doc.xpath("//comment/text").text)
+ end
end
diff --git a/test/workbook/worksheet/tc_comments.rb b/test/workbook/worksheet/tc_comments.rb
index 665f3598..acadf73d 100644
--- a/test/workbook/worksheet/tc_comments.rb
+++ b/test/workbook/worksheet/tc_comments.rb
@@ -25,9 +25,9 @@ class TestComments < Test::Unit::TestCase
end
def test_authors
assert_equal(@ws.comments.authors.size, @ws.comments.size)
- @ws.add_comment(:text => 'Yes We Can!', :author => :bob, :ref => 'F1')
+ @ws.add_comment(:text => 'Yes We Can!', :author => 'bob', :ref => 'F1')
assert_equal(@ws.comments.authors.size, 3)
- @ws.add_comment(:text => 'Yes We Can!', :author => :bob, :ref => 'F1')
+ @ws.add_comment(:text => 'Yes We Can!', :author => 'bob', :ref => 'F1')
assert_equal(@ws.comments.authors.size, 3, 'only unique authors are returned')
end
def test_pn
diff --git a/test/workbook/worksheet/tc_conditional_formatting.rb b/test/workbook/worksheet/tc_conditional_formatting.rb
index 087fd40e..9e5e01cf 100644
--- a/test/workbook/worksheet/tc_conditional_formatting.rb
+++ b/test/workbook/worksheet/tc_conditional_formatting.rb
@@ -130,6 +130,15 @@ class TestConditionalFormatting < Test::Unit::TestCase
assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='greaterThan']//xmlns:formula='0.5'")
end
+ def test_multiple_formulas
+ @ws.add_conditional_formatting "B3:B3", { :type => :cellIs, :dxfId => 0, :priority => 1, :operator => :between, :formula => ["1 <> 2","5"] }
+ doc = Nokogiri::XML.parse(@ws.to_xml_string)
+ p doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']")
+
+ assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']//xmlns:formula='1 <> 2'")
+ assert doc.xpath("//xmlns:worksheet/xmlns:conditionalFormatting//xmlns:cfRule[@type='cellIs'][@dxfId=0][@priority=1][@operator='between']//xmlns:formula='5'")
+ end
+
def test_sqref
assert_raise(ArgumentError) { @cf.sqref = 10 }
assert_nothing_raised { @cf.sqref = "A1:A1" }
diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb
index 9adbaf93..ee90bec2 100644
--- a/test/workbook/worksheet/tc_pivot_table.rb
+++ b/test/workbook/worksheet/tc_pivot_table.rb
@@ -1,5 +1,17 @@
require 'tc_helper.rb'
+
+def shared_test_pivot_table_xml_validity(pivot_table)
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
+ doc = Nokogiri::XML(pivot_table.to_xml_string)
+ errors = []
+ schema.validate(doc).each do |error|
+ errors.push error
+ puts error.message
+ end
+ assert(errors.empty?, "error free validation")
+end
+
class TestPivotTable < Test::Unit::TestCase
def setup
p = Axlsx::Package.new
@@ -33,10 +45,17 @@ class TestPivotTable < Test::Unit::TestCase
end
assert_equal(['Year', 'Month'], pivot_table.rows)
assert_equal(['Type'], pivot_table.columns)
- assert_equal(['Sales'], pivot_table.data)
+ assert_equal([{:ref=>"Sales"}], pivot_table.data)
assert_equal(['Region'], pivot_table.pages)
end
+ def test_add_pivot_table_with_options_on_data_field
+ pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') do |pt|
+ pt.data = [{:ref=>"Sales", :subtotal => 'average'}]
+ end
+ assert_equal([{:ref=>"Sales", :subtotal => 'average'}], pivot_table.data)
+ end
+
def test_header_indices
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5')
assert_equal(0, pivot_table.header_index_of('Year' ))
@@ -53,11 +72,6 @@ class TestPivotTable < Test::Unit::TestCase
assert_equal(@ws.pivot_tables.first.pn, "pivotTables/pivotTable1.xml")
end
- def test_rId
- @ws.add_pivot_table('G5:G6', 'A1:D5')
- assert_equal(@ws.pivot_tables.first.rId, "rId1")
- end
-
def test_index
@ws.add_pivot_table('G5:G6', 'A1:D5')
assert_equal(@ws.pivot_tables.first.index, @ws.workbook.pivot_tables.index(@ws.pivot_tables.first))
@@ -73,14 +87,7 @@ class TestPivotTable < Test::Unit::TestCase
def test_to_xml_string
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5')
- schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
- doc = Nokogiri::XML(pivot_table.to_xml_string)
- errors = []
- schema.validate(doc).each do |error|
- errors.push error
- puts error.message
- end
- assert(errors.empty?, "error free validation")
+ shared_test_pivot_table_xml_validity(pivot_table)
end
def test_to_xml_string_with_configuration
@@ -90,13 +97,13 @@ class TestPivotTable < Test::Unit::TestCase
pt.data = ['Sales']
pt.pages = ['Region']
end
- schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
- doc = Nokogiri::XML(pivot_table.to_xml_string)
- errors = []
- schema.validate(doc).each do |error|
- errors.push error
- puts error.message
+ shared_test_pivot_table_xml_validity(pivot_table)
+ end
+
+ def test_to_xml_string_with_options_on_data_field
+ pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt|
+ pt.data = [{:ref=>"Sales", :subtotal => 'average'}]
end
- assert(errors.empty?, "error free validation")
+ shared_test_pivot_table_xml_validity(pivot_table)
end
end
diff --git a/test/workbook/worksheet/tc_pivot_table_cache_definition.rb b/test/workbook/worksheet/tc_pivot_table_cache_definition.rb
index 2b4389b7..a38e808a 100644
--- a/test/workbook/worksheet/tc_pivot_table_cache_definition.rb
+++ b/test/workbook/worksheet/tc_pivot_table_cache_definition.rb
@@ -21,7 +21,7 @@ class TestPivotTableCacheDefinition < Test::Unit::TestCase
end
def test_rId
- assert_equal('rId1', @cache_definition.rId)
+ assert_equal @pivot_table.relationships.for(@cache_definition).Id, @cache_definition.rId
end
def test_index
diff --git a/test/workbook/worksheet/tc_table.rb b/test/workbook/worksheet/tc_table.rb
index de86b886..6f39bb13 100644
--- a/test/workbook/worksheet/tc_table.rb
+++ b/test/workbook/worksheet/tc_table.rb
@@ -36,8 +36,8 @@ class TestTable < Test::Unit::TestCase
end
def test_rId
- @ws.add_table("A1:D5")
- assert_equal(@ws.tables.first.rId, "rId1")
+ table = @ws.add_table("A1:D5")
+ assert_equal @ws.relationships.for(table).Id, table.rId
end
def test_index
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index ed8c7cab..00983fec 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -123,9 +123,7 @@ class TestWorksheet < Test::Unit::TestCase
end
def test_rId
- assert_equal(@ws.rId, "rId1")
- ws = @ws.workbook.add_worksheet
- assert_equal(ws.rId, "rId2")
+ assert_equal @ws.workbook.relationships.for(@ws).Id, @ws.rId
end
def test_index
@@ -205,13 +203,20 @@ class TestWorksheet < Test::Unit::TestCase
def test_cols
@ws.add_row [1,2,3,4]
@ws.add_row [1,2,3,4]
- @ws.add_row [1,2,3,4]
+ @ws.add_row [1,2,3]
@ws.add_row [1,2,3,4]
c = @ws.cols[1]
assert_equal(c.size, 4)
assert_equal(c[0].value, 2)
end
+ def test_cols_with_block
+ @ws.add_row [1,2,3]
+ @ws.add_row [1]
+ cols = @ws.cols {|row, column| :foo }
+ assert_equal(:foo, cols[1][1])
+ end
+
def test_row_style
@ws.add_row [1,2,3,4]
@ws.add_row [1,2,3,4]
@@ -334,16 +339,16 @@ class TestWorksheet < Test::Unit::TestCase
def test_to_xml_string_drawing
@ws.add_chart Axlsx::Pie3DChart
doc = Nokogiri::XML(@ws.to_xml_string)
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:drawing[@r:id="rId1"]').size, 1)
+ assert_equal @ws.send(:worksheet_drawing).relationship.Id, doc.xpath('//xmlns:worksheet/xmlns:drawing').first["r:id"]
end
def test_to_xml_string_tables
@ws.add_row ["one", "two"]
@ws.add_row [1, 2]
- @ws.add_table "A1:B2"
+ table = @ws.add_table "A1:B2"
doc = Nokogiri::XML(@ws.to_xml_string)
assert_equal(doc.xpath('//xmlns:worksheet/xmlns:tableParts[@count="1"]').size, 1)
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:tableParts/xmlns:tablePart[@r:id="rId1"]').size, 1)
+ assert_equal table.rId, doc.xpath('//xmlns:worksheet/xmlns:tableParts/xmlns:tablePart').first["r:id"]
end
def test_to_xml_string
diff --git a/test/workbook/worksheet/tc_worksheet_hyperlink.rb b/test/workbook/worksheet/tc_worksheet_hyperlink.rb
index 278c5add..748082ac 100644
--- a/test/workbook/worksheet/tc_worksheet_hyperlink.rb
+++ b/test/workbook/worksheet/tc_worksheet_hyperlink.rb
@@ -32,22 +32,13 @@ class TestWorksheetHyperlink < Test::Unit::TestCase
assert_equal(@options[:ref], @a.ref)
end
- def test_id
- @a.target = :external
-
- assert_equal("rId1", @a.id)
- @a.target = :internal
- assert_equal(nil, @a.id)
- end
-
-
def test_to_xml_string_with_non_external
doc = Nokogiri::XML(@ws.to_xml_string)
assert_equal(doc.xpath("//xmlns:hyperlink[@ref='#{@a.ref}']").size, 1)
assert_equal(doc.xpath("//xmlns:hyperlink[@tooltip='#{@a.tooltip}']").size, 1)
assert_equal(doc.xpath("//xmlns:hyperlink[@location='#{@a.location}']").size, 1)
assert_equal(doc.xpath("//xmlns:hyperlink[@display='#{@a.display}']").size, 1)
- assert_equal(doc.xpath("//xmlns:hyperlink[@r:id='#{@a.id}']").size, 0)
+ assert_equal(doc.xpath("//xmlns:hyperlink[@r:id]").size, 0)
end
def test_to_xml_stirng_with_external
@@ -57,7 +48,7 @@ class TestWorksheetHyperlink < Test::Unit::TestCase
assert_equal(doc.xpath("//xmlns:hyperlink[@tooltip='#{@a.tooltip}']").size, 1)
assert_equal(doc.xpath("//xmlns:hyperlink[@display='#{@a.display}']").size, 1)
assert_equal(doc.xpath("//xmlns:hyperlink[@location='#{@a.location}']").size, 0)
- assert_equal(doc.xpath("//xmlns:hyperlink[@r:id='#{@a.id}']").size, 1)
+ assert_equal(doc.xpath("//xmlns:hyperlink[@r:id='#{@a.relationship.Id}']").size, 1)
end
end