require "assert"
require 'osheet/xmlss_writer'
module Osheet::Xmlss
class StylesTests < Assert::Context
before do
@writer = Osheet::XmlssWriter.new
@workbook = Osheet::Workbook.new(@writer)
end
subject { @writer }
should "not have a style cache until its bound to an osheet workbook" do
writer = Osheet::XmlssWriter.new
assert_nil writer.style_cache
writer.bind(@workbook)
assert_not_nil writer.style_cache
end
should "return no style if no class or format given" do
assert_nil subject.style(nil)
assert_nil subject.style('')
assert_not_nil subject.style('awesome')
end
end
class StyleMethTests < StylesTests
desc "Xmlss style writer"
before do
@workbook.style('.font.size') { @workbook.font 14 }
@workbook.style('.font.weight') { @workbook.font :bold }
@workbook.style('.font.style') { @workbook.font :italic }
@workbook.style('.align.center') { @workbook.align :center }
end
should "build a style obj and add it to the writers styles" do
xmlss_style = subject.style('awesome')
assert_kind_of ::Xmlss::Style::Base, xmlss_style
assert_equal '.awesome', xmlss_style.id
assert_equal 1, subject.style_cache.size
assert_equal xmlss_style, subject.style_cache[xmlss_style.id]
end
should "write style markup from many matching osheet styles" do
xmlss_style = subject.style('font size weight style align center')
assert_equal '.font.size.weight.style.align.center', xmlss_style.id
end
end
class AlignmentTests < StylesTests
desc "Alignment style writer"
before do
[ :left, :center, :right,
:top, :middle, :bottom,
:wrap
].each do |s|
@workbook.style(".align.#{s}") { @workbook.align s }
end
@workbook.style('.align.rotate') { @workbook.align 90 }
end
should "write style markup with no alignment settings if no alignment style match" do
subject.style('align')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for horizontal alignment settings" do
subject.style('align left')
subject.style('align center')
subject.style('align right')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for vertical alignment settings" do
subject.style('align top')
subject.style('align middle')
subject.style('align bottom')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for text wrap settings" do
subject.style('align wrap')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for text rotation settings" do
subject.style('align rotate')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
end
class FontTests < StylesTests
desc "Font style writer"
before do
[ :underline, :double_underline, :accounting_underline, :double_accounting_underline,
:subscript, :superscript, :shadow, :strikethrough, :wrap,
:bold, :italic
].each do |s|
@workbook.style(".font.#{s}") { @workbook.font s }
end
@workbook.style('.font.size') { @workbook.font 14 }
@workbook.style('.font.color') { @workbook.font '#FF0000' }
@workbook.style('.font.name') { @workbook.font 'Verdana' }
end
should "write style markup with empty font settings if no match" do
subject.style('font')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for font underline settings" do
subject.style('font underline')
subject.style('font double_underline')
subject.style('font accounting_underline')
subject.style('font double_accounting_underline')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for font alignment settings" do
subject.style('font subscript')
subject.style('font superscript')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for font style settings" do
subject.style('font bold')
subject.style('font italic')
subject.style('font strikethrough')
subject.style('font shadow')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for font size" do
subject.style('font size')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for font color" do
subject.style('font color')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for font name" do
subject.style('font name')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
end
class BgTests < StylesTests
desc "Bg writer"
before do
@workbook.style('.bg.color') { @workbook.bg '#FF0000' }
@workbook.style('.bg.pattern-only') { @workbook.bg :solid }
@workbook.style('.bg.pattern-color') { @workbook.bg :horz_stripe => '#0000FF' }
@workbook.style('.bg.color-first') { @workbook.bg '#00FF00', {:horz_stripe => '#0000FF'} }
@workbook.style('.bg.pattern-first') { @workbook.bg({:horz_stripe => '#0000FF'}, '#00FF00') }
end
should "write style markup with empty bg settings when no match" do
subject.style('bg')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for bg color and auto set the pattern to solid" do
subject.style('bg color')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for bg pattern settings" do
subject.style('bg pattern-only')
subject.style('bg pattern-only')
subject.style('bg pattern-color')
subject.style('bg pattern-color')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup setting pattern to solid when setting bg color" do
subject.style('bg color')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup setting pattern to pattern setting when first setting bg color then pattern" do
subject.style('bg color-first')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup setting pattern to pattern setting when first setting bg pattern then color" do
subject.style('bg pattern-first')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
end
class BorderTests < StylesTests
desc "Font border writer"
before do
::Osheet::Style::BORDER_POSITIONS.each do |p|
@workbook.style(".border.#{p}") { @workbook.send("border_#{p}", :thin) }
end
[:hairline, :thin, :medium, :thick].each do |w|
@workbook.style(".border.#{w}") { @workbook.border_top w }
end
[:none, :continuous, :dash, :dot, :dash_dot, :dash_dot_dot].each do |s|
@workbook.style(".border.#{s}") { @workbook.border_top s }
end
@workbook.style('.border.color') { @workbook.border_top '#FF0000' }
@workbook.style('.border.all') { @workbook.border :thick, :dash, '#FF0000' }
end
should "write style markup with empty border settings when no match" do
subject.style('border')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup with identical settings for all positions when using 'border'" do
subject.style('border all')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for border specific positions" do
::Osheet::Style::BORDER_POSITIONS.each do |p|
subject.style("border #{p}")
end
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for border weight settings" do
[:hairline, :thin, :medium, :thick].each do |w|
subject.style("border #{w}")
end
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for border style settings" do
[:none, :continuous, :dash, :dot, :dash_dot, :dash_dot_dot].each do |s|
subject.style("border #{s}")
end
assert_equal(
"",
xmlss_style_markup(subject)
)
end
should "write style markup for border color" do
subject.style('border color')
assert_equal(
"",
xmlss_style_markup(subject)
)
end
end
class NumberFormatTests < StylesTests
desc "Xmlss style number format writer"
should "write style markup with formatting" do
subject.style('', Osheet::Format.new(:text))
subject.style('', Osheet::Format.new(:datetime, 'mm/dd/yy'))
assert_equal(
"",
xmlss_style_markup(subject)
)
end
end
end