diff options
| author | Stephen Pike <[email protected]> | 2012-04-20 15:05:01 -0400 |
|---|---|---|
| committer | Stephen Pike <[email protected]> | 2012-04-20 15:05:01 -0400 |
| commit | f96205df2b10c68d10a2a6882fe77928358ed362 (patch) | |
| tree | ba6539559d00ae055ece38427d22ad8f64da5e07 /test/stylesheet | |
| parent | 6b0c1e3c59e581525161491d85a153a537711370 (diff) | |
| download | caxlsx-f96205df2b10c68d10a2a6882fe77928358ed362.tar.gz caxlsx-f96205df2b10c68d10a2a6882fe77928358ed362.zip | |
[#33] Add support for Dxf elements to enable conditional formatting
New Dxf class implements 18.8.14. Conditional formatting now "works".
Add :type option to add_styles, defaults to :xf
when add_styles is called with :dxf type, new styles are not added
to the global @styles set. Dxf child elements only exist inside the
`<dxf>` chunk.
Added tests and an example.
Diffstat (limited to 'test/stylesheet')
| -rw-r--r-- | test/stylesheet/tc_dxf.rb | 81 | ||||
| -rw-r--r-- | test/stylesheet/tc_styles.rb | 44 |
2 files changed, 125 insertions, 0 deletions
diff --git a/test/stylesheet/tc_dxf.rb b/test/stylesheet/tc_dxf.rb new file mode 100644 index 00000000..e94a1614 --- /dev/null +++ b/test/stylesheet/tc_dxf.rb @@ -0,0 +1,81 @@ +require 'tc_helper.rb' + +class TestDxf < Test::Unit::TestCase + + def setup + @item = Axlsx::Dxf.new + @styles = Axlsx::Styles.new + end + + def teardown + end + + def test_initialiation + assert_equal(@item.alignment, nil) + assert_equal(@item.protection, nil) + assert_equal(@item.numFmt, nil) + assert_equal(@item.font, nil) + assert_equal(@item.fill, nil) + assert_equal(@item.border, nil) + end + + def test_alignment + assert_raise(ArgumentError) { @item.alignment = -1.1 } + assert_nothing_raised { @item.alignment = Axlsx::CellAlignment.new } + assert(@item.alignment.is_a?(Axlsx::CellAlignment)) + end + + def test_protection + assert_raise(ArgumentError) { @item.protection = -1.1 } + assert_nothing_raised { @item.protection = Axlsx::CellProtection.new } + assert(@item.protection.is_a?(Axlsx::CellProtection)) + end + + def test_numFmt + assert_raise(ArgumentError) { @item.numFmt = 1 } + assert_nothing_raised { @item.numFmt = Axlsx::NumFmt.new } + assert @item.numFmt.is_a? Axlsx::NumFmt + end + + def test_fill + assert_raise(ArgumentError) { @item.fill = 1 } + assert_nothing_raised { @item.fill = Axlsx::Fill.new(Axlsx::PatternFill.new(:patternType =>:solid, :fgColor=> Axlsx::Color.new(:rgb => "FF000000"))) } + assert @item.fill.is_a? Axlsx::Fill + end + + def test_font + assert_raise(ArgumentError) { @item.font = 1 } + assert_nothing_raised { @item.font = Axlsx::Font.new } + assert @item.font.is_a? Axlsx::Font + end + + def test_border + assert_raise(ArgumentError) { @item.border = 1 } + assert_nothing_raised { @item.border = Axlsx::Border.new } + assert @item.border.is_a? Axlsx::Border + end + + def test_to_xml + @item.border = Axlsx::Border.new + doc = Nokogiri::XML.parse(@item.to_xml_string) + assert_equal(1, doc.xpath(".//dxf//border").size) + assert_equal(0, doc.xpath(".//dxf//font").size) + end + + def test_many_options_xml + @item.border = Axlsx::Border.new + @item.alignment = Axlsx::CellAlignment.new + @item.fill = Axlsx::Fill.new(Axlsx::PatternFill.new(:patternType =>:solid, :fgColor=> Axlsx::Color.new(:rgb => "FF000000"))) + @item.font = Axlsx::Font.new + @item.protection = Axlsx::CellProtection.new + @item.numFmt = Axlsx::NumFmt.new + + doc = Nokogiri::XML.parse(@item.to_xml_string) + assert_equal(1, doc.xpath(".//dxf//fill//patternFill[@patternType='solid']//fgColor[@rgb='FF000000']").size) + assert_equal(1, doc.xpath(".//dxf//font").size) + assert_equal(1, doc.xpath(".//dxf//protection").size) + assert_equal(1, doc.xpath(".//dxf//numFmt[@numFmtId='0'][@formatCode='']").size) + assert_equal(1, doc.xpath(".//dxf//alignment").size) + assert_equal(1, doc.xpath(".//dxf//border").size) + end +end diff --git a/test/stylesheet/tc_styles.rb b/test/stylesheet/tc_styles.rb index 31461ae3..4f63df9d 100644 --- a/test/stylesheet/tc_styles.rb +++ b/test/stylesheet/tc_styles.rb @@ -73,5 +73,49 @@ class TestStyles < Test::Unit::TestCase assert_equal(xf.applyNumberFormat, true, "number format applied") assert_equal(xf.applyAlignment, true, "alignment applied") end + + def test_basic_add_style_dxf + border_count = @styles.borders.size + s = @styles.add_style :border => {:style => :thin, :color => "FFFF0000"}, :type => :dxf + assert_equal(@styles.borders.size, border_count, "styles borders not affected") + assert_equal(@styles.dxfs.last.border.prs.last.color.rgb, "FFFF0000") + assert_raise(ArgumentError) { @styles.add_style :border => {:color => "FFFF0000"}, :type => :dxf } + assert_equal @styles.borders.last.prs.size, 4 + end + + def test_add_style_dxf + fill_count = @styles.fills.size + font_count = @styles.fonts.size + dxf_count = @styles.dxfs.size + style = @styles.add_style :bg_color=>"FF000000", :fg_color=>"FFFFFFFF", :sz=>13, :alignment=>{:horizontal=>:left}, :border=>{:style => :thin, :color => "FFFF0000"}, :hidden=>true, :locked=>true, :type => :dxf + assert_equal(@styles.dxfs.size, dxf_count+1) + assert_equal(0, style, "returns the zero-based dxfId") + + dxf = @styles.dxfs.last + assert_equal(@styles.dxfs.last.fill.fill_type.fgColor.rgb, "FF000000", "fill created with color") + + assert_equal(font_count, (@styles.fonts.size), "font not created under styles") + assert_equal(fill_count, (@styles.fills.size), "fill not created under styles") + + assert(dxf.border.is_a?(Axlsx::Border), "border is set") + assert_equal(nil, dxf.numFmt, "number format is not set") + + assert(dxf.alignment.is_a?(Axlsx::CellAlignment), "alignment was created") + assert_equal(dxf.alignment.horizontal, :left, "horizontal alignment applied") + assert_equal(dxf.protection.hidden, true, "hidden protection set") + assert_equal(dxf.protection.locked, true, "cell locking set") + assert_raise(ArgumentError, "should reject invalid borderId") { @styles.add_style :border => 3 } + assert_raise(ArgumentError, "should reject num_fmt option") { + @styles.add_style :type=>:dxf, :num_fmt=>Axlsx::NUM_FMT_PERCENT + } + end + + def test_multiple_dxf + # add a second style + style = @styles.add_style :bg_color=>"00000000", :fg_color=>"FFFFFFFF", :sz=>13, :alignment=>{:horizontal=>:left}, :border=>{:style => :thin, :color => "FFFF0000"}, :hidden=>true, :locked=>true, :type => :dxf + assert_equal(0, style, "returns the first dxfId") + style = @styles.add_style :bg_color=>"FF000000", :fg_color=>"FFFFFFFF", :sz=>13, :alignment=>{:horizontal=>:left}, :border=>{:style => :thin, :color => "FFFF0000"}, :hidden=>true, :locked=>true, :type => :dxf + assert_equal(1, style, "returns the second dxfId") + end end |
