summaryrefslogtreecommitdiffhomepage
path: root/test/stylesheet/tc_styles.rb
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2012-04-24 08:54:56 +0900
committerRandy Morgan <[email protected]>2012-04-24 08:54:56 +0900
commit80e61961ce4115f74a30a5b19847993b2a1dbc0e (patch)
treefb7c7ef9a3c94f1b1829dcb04b7cb1f4bbfdb28d /test/stylesheet/tc_styles.rb
parente044cfd495d8d7335b44b48d91f1f63be41dad26 (diff)
downloadcaxlsx-80e61961ce4115f74a30a5b19847993b2a1dbc0e.tar.gz
caxlsx-80e61961ce4115f74a30a5b19847993b2a1dbc0e.zip
add_style refactoring.
Diffstat (limited to 'test/stylesheet/tc_styles.rb')
-rw-r--r--test/stylesheet/tc_styles.rb116
1 files changed, 92 insertions, 24 deletions
diff --git a/test/stylesheet/tc_styles.rb b/test/stylesheet/tc_styles.rb
index b556ef0f..18235fff 100644
--- a/test/stylesheet/tc_styles.rb
+++ b/test/stylesheet/tc_styles.rb
@@ -46,36 +46,104 @@ class TestStyles < Test::Unit::TestCase
num_fmt = {:num_fmt => 5}
both = { :format_code => "#000", :num_fmt => 0 }
assert_equal(@styles.parse_num_fmt_options, nil, 'noop if neither :format_code or :num_fmt exist')
- assert_equal(@styles.parse_num_fmt_options(f_code).numFmtId, ((@styles.numFmts.map{ |nf| nf.numFmtId }).max + 1), "new numfmts gets next available id")
- assert(@styles.parse_num_fmt_options(f_code).is_a?(Axlsx::NumFmt), "Must create a NumFmt object when format_code key exists.")
- assert(@styles.parse_num_fmt_options(num_fmt).is_a?(Integer), "Should return the provided num_fmt if not dxf and format_code is not set")
- assert(@styles.parse_num_fmt_options(num_fmt.merge({:type => :dxf})).is_a?(Axlsx::NumFmt), "Makes a new NumFmt if dxf and only num_fmt specified")
- assert(@styles.parse_num_fmt_options(both).is_a?(Axlsx::NumFmt), "builds a new number format if format_code and num_fmt are specified")
+ max = @styles.numFmts.map{ |num_fmt| num_fmt.numFmtId }.max
+ @styles.parse_num_fmt_options(f_code)
+ assert_equal(@styles.numFmts.last.numFmtId, max + 1, "new numfmts gets next available id")
+ assert(@styles.parse_num_fmt_options(num_fmt).is_a?(Integer), "Should return the provided num_fmt if not dxf")
+ assert(@styles.parse_num_fmt_options(num_fmt.merge({:type => :dxf})).is_a?(Axlsx::NumFmt), "Makes a new NumFmt if dxf")
end
- def test_parse_border_options_hash
- b_opts = {:border => { :edges => [:left, :right], :color => "FFDADADA", :style => :thick } }
+ def test_parse_border_options_hash_required_keys
+ assert_raise(ArgumentError, "Require color key") { @styles.parse_border_options(:border => { :style => :thin }) }
+ assert_raise(ArgumentError, "Require style key") { @styles.parse_border_options(:border => { :color => "FF0d0d0d" }) }
+ assert_nothing_raised { @styles.parse_border_options(:border => { :style => :thin, :color => "FF000000"} ) }
+ end
+
+ def test_parse_border_basic_options
+ b_opts = {:border => { :diagonalUp => 1, :edges => [:left, :right], :color => "FFDADADA", :style => :thick } }
b = @styles.parse_border_options b_opts
- assert(b.is_a? Axlsx::Border)
- assert_raise(ArgumentError, "Require color key") { @styles.parse_border_options({:border => {:style => :thin}}) }
- assert_raise(ArgumentError, "Require style key") { @styles.parse_border_options({:border => {:color => "FF0d0d0d"}}) }
+ assert(b.is_a? Integer)
+ assert_equal(@styles.parse_border_options(b_opts.merge({:type => :dxf})).class,Axlsx::Border)
+ assert(@styles.borders.last.diagonalUp == 1, "border options are passed in to the initializer")
+ end
+
+ def test_parse_border_options_edges
+ b_opts = {:border => { :diagonalUp => 1, :edges => [:left, :right], :color => "FFDADADA", :style => :thick } }
+ @styles.parse_border_options b_opts
+ b = @styles.borders.last
left = b.prs.select { |bpr| bpr.name == :left }[0]
right = b.prs.select { |bpr| bpr.name == :right }[0]
top = b.prs.select { |bpr| bpr.name == :top }[0]
bottom = b.prs.select { |bpr| bpr.name == :bottom }[0]
- assert_equal(top, nil)
- assert_equal(bottom, nil)
- assert left.is_a? Axlsx::BorderPr
- assert right.is_a? Axlsx::BorderPr
- assert_equal(left.style,right.style)
- assert_equal(left.style, :thick)
- assert_equal(right.color.rgb,left.color.rgb)
- assert_equal(right.color.rgb,"FFDADADA")
- assert_equal(@styles.parse_border_options({}), nil)
+ assert_equal(top, nil, "unspecified top edge should not be created")
+ assert_equal(bottom, nil, "unspecified bottom edge should not be created")
+ assert(left.is_a?(Axlsx::BorderPr), "specified left edge is set")
+ assert(right.is_a?(Axlsx::BorderPr), "specified right edge is set")
+ assert_equal(left.style,right.style, "edge parts have the same style")
+ assert_equal(left.style, :thick, "the style is THICK")
+ assert_equal(right.color.rgb,left.color.rgb, "edge parts are colors are the same")
+ assert_equal(right.color.rgb,"FFDADADA", "edge color rgb is correct")
+ end
+
+ def test_parse_border_options_noop
+ assert_equal(@styles.parse_border_options({}), nil, "noop if the border key is not in options")
+ end
+
+ def test_parse_border_options_integer_xf
+ assert_equal(@styles.parse_border_options(:border => 1), 1)
+ assert_raise(ArgumentError, "unknown border index") {@styles.parse_border_options(:border => 100) }
end
- def test_parse_border_options_integer
+ def test_parse_border_options_integer_dxf
+ b_opts = { :border => { :edges => [:left, :right], :color => "FFFFFFFF", :style=> :thick } }
+ b = @styles.parse_border_options(b_opts)
+ b2 = @styles.parse_border_options(:border => b, :type => :dxf)
+ assert(b2.is_a?(Axlsx::Border), "Cloned existing border object")
+ end
+
+ def test_parse_alignment_options
+ assert_equal(@styles.parse_alignment_options {}, nil, "noop if :alignment is not set")
+ assert(@styles.parse_alignment_options(:alignment => {}).is_a?(Axlsx::CellAlignment))
+ end
+
+ def test_parse_font_options
+ options = {
+ :fg_color => "FF050505",
+ :sz => 20,
+ :b => 1,
+ :i => 1,
+ :u => 1,
+ :strike => 1,
+ :outline => 1,
+ :shadow => 1,
+ :charset => 9,
+ :family => 1,
+ :font_name => "woot font"
+ }
+ assert_equal(@styles.parse_font_options {}, nil, "noop if no font keys are set")
+ assert_equal(@styles.parse_font_options(:b=>1).class, Fixnum, "return index of font if not :dxf type")
+ assert_equal(@styles.parse_font_options(:b=>1, :type => :dxf).class, Axlsx::Font, "return font object if :dxf type")
+
+ f = @styles.parse_font_options(options.merge(:type => :dxf))
+ color = options.delete(:fg_color)
+ options[:name] = options.delete(:font_name)
+ options.each do |key, value|
+ assert_equal(f.send(key), value, "assert that #{key} was parsed")
+ end
+ assert_equal(f.color.rgb, color)
+ end
+
+ def test_parse_fill_options
+ assert_equal(@styles.parse_fill_options {}, nil, "noop if no fill keys are set")
+ assert_equal(@styles.parse_fill_options(:bg_color => "DE").class, Fixnum, "return index of fill if not :dxf type")
+ assert_equal(@styles.parse_fill_options(:bg_color => "DE", :type => :dxf).class, Axlsx::Fill, "return fill object if :dxf type")
+ f = @styles.parse_fill_options(:bg_color => "DE", :type => :dxf)
+ assert(f.fill_type.fgColor.rgb == "FFDEDEDE")
+ end
+ def test_parse_protection_options
+ assert_equal(@styles.parse_protection_options {}, nil, "noop if no protection keys are set")
+ assert_equal(@styles.parse_protection_options(:hidden => 1).class, Axlsx::CellProtection, "creates a new cell protection object")
end
def test_add_style
@@ -105,10 +173,10 @@ class TestStyles < Test::Unit::TestCase
assert_raise(ArgumentError, "should reject invalid borderId") { @styles.add_style :border => 2 }
- assert_equal(xf.applyProtection, 1, "protection applied")
- assert_equal(xf.applyBorder, 1, "border applied")
- assert_equal(xf.applyNumberFormat,1, "number format applied")
- assert_equal(xf.applyAlignment, 1, "alignment applied")
+ assert_equal(xf.applyProtection, true, "protection applied")
+ assert_equal(xf.applyBorder, true, "border applied")
+ assert_equal(xf.applyNumberFormat,true, "number format applied")
+ assert_equal(xf.applyAlignment, true, "alignment applied")
end
def test_basic_add_style_dxf