1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# frozen_string_literal: true
require 'tc_helper'
class TestBarChart < Test::Unit::TestCase
def setup
@p = Axlsx::Package.new
ws = @p.workbook.add_worksheet
@row = ws.add_row ["one", 1, Time.now]
@chart = ws.add_chart Axlsx::BarChart, title: "fishery"
end
def teardown; end
def test_initialization
assert_equal(:clustered, @chart.grouping, "grouping defualt incorrect")
assert_equal(@chart.series_type, Axlsx::BarSeries, "series type incorrect")
assert_equal(:bar, @chart.bar_dir, " bar direction incorrect")
assert(@chart.cat_axis.is_a?(Axlsx::CatAxis), "category axis not created")
assert(@chart.val_axis.is_a?(Axlsx::ValAxis), "value access not created")
end
def test_bar_direction
assert_raise(ArgumentError, "require valid bar direction") { @chart.bar_dir = :left }
assert_nothing_raised("allow valid bar direction") { @chart.bar_dir = :col }
assert_equal(:col, @chart.bar_dir)
end
def test_grouping
assert_raise(ArgumentError, "require valid grouping") { @chart.grouping = :inverted }
assert_nothing_raised("allow valid grouping") { @chart.grouping = :standard }
assert_equal(:standard, @chart.grouping)
end
def test_gap_width
assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = -1 }
assert_raise(ArgumentError, "require valid gap width") { @chart.gap_width = 501 }
assert_nothing_raised("allow valid gap width") { @chart.gap_width = 200 }
assert_equal(200, @chart.gap_width, 'gap width is incorrect')
end
def test_overlap
assert_raise(ArgumentError, "require valid overlap") { @chart.overlap = -101 }
assert_raise(ArgumentError, "require valid overlap") { @chart.overlap = 101 }
assert_nothing_raised("allow valid overlap") { @chart.overlap = 100 }
assert_equal(100, @chart.overlap, 'overlap is incorrect')
end
def test_shape
assert_raise(ArgumentError, "require valid shape") { @chart.shape = :star }
assert_nothing_raised("allow valid shape") { @chart.shape = :cone }
assert_equal(:cone, @chart.shape)
end
def test_to_xml_string
schema = Nokogiri::XML::Schema(File.open(Axlsx::DRAWING_XSD))
doc = Nokogiri::XML(@chart.to_xml_string)
errors = []
schema.validate(doc).each do |error|
errors.push error
puts error.message
end
assert_empty(errors, "error free validation")
end
def test_to_xml_string_has_axes_in_correct_order
str = @chart.to_xml_string
cat_axis_position = str.index(@chart.axes[:cat_axis].id.to_s)
val_axis_position = str.index(@chart.axes[:val_axis].id.to_s)
assert(cat_axis_position < val_axis_position, "cat_axis must occur earlier than val_axis in the XML")
end
def test_to_xml_string_has_gap_width
gap_width_value = rand(0..500)
@chart.gap_width = gap_width_value
doc = Nokogiri::XML(@chart.to_xml_string)
assert_equal(doc.xpath("//c:barChart/c:gapWidth").first.attribute('val').value, gap_width_value.to_s)
end
def test_to_xml_string_has_overlap
overlap_value = rand(-100..100)
@chart.overlap = overlap_value
doc = Nokogiri::XML(@chart.to_xml_string)
assert_equal(doc.xpath("//c:barChart/c:overlap").first.attribute('val').value, overlap_value.to_s)
end
end
|