forked from randym/axlsx
/
tc_package.rb
113 lines (97 loc) · 5.1 KB
/
tc_package.rb
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'test/unit'
require 'axlsx.rb'
class TestPackage < Test::Unit::TestCase
def setup
@package = Axlsx::Package.new
ws = @package.workbook.add_worksheet
chart = ws.add_chart Axlsx::Pie3DChart
chart.add_series :data=>[1,2,3], :labels=>["a", "b", "c"]
@fname = 'axlsx_test_serialization.xlsx'
end
def test_core_accessor
assert_equal(@package.core, @package.instance_values["core"])
assert_raise(NoMethodError) {@package.core = nil }
end
def test_app_accessor
assert_equal(@package.app, @package.instance_values["app"])
assert_raise(NoMethodError) {@package.app = nil }
end
def test_use_shared_strings
assert_equal(@package.use_shared_strings, nil)
assert_raise(ArgumentError) {@package.use_shared_strings 9}
assert_nothing_raised {@package.use_shared_strings = true}
assert_equal(@package.use_shared_strings, @package.workbook.use_shared_strings)
end
def test_default_objects_are_created
assert(@package.instance_values["app"].is_a?(Axlsx::App), 'App object not created')
assert(@package.instance_values["core"].is_a?(Axlsx::Core), 'Core object not created')
assert(@package.workbook.is_a?(Axlsx::Workbook), 'Workbook object not created')
assert(Axlsx::Package.new.workbook.worksheets.size == 0, 'Workbook should not have sheets by default')
end
def test_serialization
fname = 'axlsx_test_serialization.xlsx'
assert_nothing_raised do
begin
z= @package.serialize(@fname)
zf = Zip::ZipFile.open(@fname)
@package.send(:parts).each{ |part| zf.get_entry(part[:entry]) }
File.delete(@fname)
rescue Errno::EACCES
puts "WARNING:: test_serialization requires write access."
end
end
end
def test_validation
assert_equal(@package.validate.size, 0, @package.validate)
#how to test for failure? the internal validations on the models are so strict I cant break anthing.....
end
def test_parts
p = @package.send(:parts)
p.each do |part|
#all parts must have :doc, :entry, :schema
assert(part.keys.size == 3 && part.keys.reject{ |k| [:doc, :entry, :schema].include? k}.empty?)
end
#all parts have an entry
assert_equal(p.select{ |part| part[:entry] =~ /_rels\/\.rels/ }.size, 1, "rels missing")
assert_equal(p.select{ |part| part[:entry] =~ /docProps\/core\.xml/ }.size, 1, "core missing")
assert_equal(p.select{ |part| part[:entry] =~ /docProps\/app\.xml/ }.size, 1, "app missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/_rels\/workbook\.xml\.rels/ }.size, 1, "workbook rels missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/workbook\.xml/ }.size, 1, "workbook missing")
assert_equal(p.select{ |part| part[:entry] =~ /\[Content_Types\]\.xml/ }.size, 1, "content types missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/styles\.xml/ }.size, 1, "styles missin")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/drawings\/_rels\/drawing\d\.xml\.rels/ }.size, @package.workbook.drawings.size, "one or more drawing rels missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/drawings\/drawing\d\.xml/ }.size, @package.workbook.drawings.size, "one or more drawings missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/charts\/chart\d\.xml/ }.size, @package.workbook.charts.size, "one or more charts missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/worksheets\/sheet\d\.xml/ }.size, @package.workbook.worksheets.size, "one or more sheet missing")
assert_equal(p.select{ |part| part[:entry] =~ /xl\/worksheets\/_rels\/sheet\d\.xml\.rels/ }.size, @package.workbook.worksheets.size, "one or more sheet rels missing")
#no mystery parts
assert_equal(p.size, 12)
end
def test_shared_strings_requires_part
@package.use_shared_strings = true
p = @package.send(:parts)
assert_equal(p.select{ |part| part[:entry] =~/xl\/sharedStrings.xml/}.size, 1, "shared strings table missing")
end
def test_workbook_is_a_workbook
assert @package.workbook.is_a? Axlsx::Workbook
end
def test_base_content_types
ct = @package.send(:base_content_types)
assert(ct.select { |c| c.ContentType == Axlsx::RELS_CT }.size == 1, "rels content type missing")
assert(ct.select { |c| c.ContentType == Axlsx::XML_CT }.size == 1, "xml content type missing")
assert(ct.select { |c| c.ContentType == Axlsx::APP_CT }.size == 1, "app content type missing")
assert(ct.select { |c| c.ContentType == Axlsx::CORE_CT }.size == 1, "core content type missing")
assert(ct.select { |c| c.ContentType == Axlsx::STYLES_CT }.size == 1, "styles content type missing")
assert(ct.select { |c| c.ContentType == Axlsx::WORKBOOK_CT }.size == 1, "workbook content type missing")
assert(ct.size == 6)
end
def test_content_type_added_with_shared_strings
@package.use_shared_strings = true
ct = @package.send(:content_types)
assert(ct.select { |ct| ct.ContentType == Axlsx::SHARED_STRINGS_CT }.size == 1)
end
def test_name_to_indices
assert(Axlsx::name_to_indices('A1') == [0,0])
assert(Axlsx::name_to_indices('A100') == [0,99], 'needs to axcept rows that contain 0')
end
end