axlsx 1.0.17 → 1.0.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'test/unit'
3
+ require 'axlsx.rb'
4
+
5
+ class TestDateTimeConverter < Test::Unit::TestCase
6
+ def setup
7
+ @margin_of_error = 0.000_001
8
+ @extended_time_range = begin
9
+ Time.parse "1893-08-05"
10
+ Time.parse "9999-12-31T23:59:59Z"
11
+ true
12
+ rescue
13
+ false
14
+ end
15
+ end
16
+
17
+ def test_date_to_serial_1900
18
+ Axlsx::Workbook.date1904 = false
19
+ tests = if @extended_time_range
20
+ { # examples taken straight from the spec
21
+ "1893-08-05" => -2338.0,
22
+ "1900-01-01" => 2.0,
23
+ "1910-02-03" => 3687.0,
24
+ "2006-02-01" => 38749.0,
25
+ "9999-12-31" => 2958465.0
26
+ }
27
+ else
28
+ { # examples taken inside the possible values
29
+ "1970-01-01" => 25569.0, # Unix epoch
30
+ "1970-01-02" => 25570.0,
31
+ "2006-02-01" => 38749.0,
32
+ "2038-01-19" => 50424.0, # max date using signed timestamp in 32bit
33
+ }
34
+ end
35
+ tests.each do |date_string, expected|
36
+ serial = Axlsx::DateTimeConverter::date_to_serial Date.parse(date_string)
37
+ assert_equal expected, serial
38
+ end
39
+ end
40
+
41
+ def test_date_to_serial_1904
42
+ Axlsx::Workbook.date1904 = true
43
+ tests = if @extended_time_range
44
+ { # examples taken straight from the spec
45
+ "1893-08-05" => -3800.0,
46
+ "1904-01-01" => 0.0,
47
+ "1910-02-03" => 2225.0,
48
+ "2006-02-01" => 37287.0,
49
+ "9999-12-31" => 2957003.0
50
+ }
51
+ else
52
+ { # examples taken inside the possible values
53
+ "1970-01-01" => 24107.0, # Unix epoch
54
+ "1970-01-02" => 24108.0,
55
+ "2006-02-01" => 37287.0,
56
+ "2038-01-19" => 48962.0, # max date using signed timestamp in 32bit
57
+ }
58
+ end
59
+ tests.each do |date_string, expected|
60
+ serial = Axlsx::DateTimeConverter::date_to_serial Date.parse(date_string)
61
+ assert_equal expected, serial
62
+ end
63
+ end
64
+
65
+ def test_time_to_serial_1900
66
+ Axlsx::Workbook.date1904 = false
67
+ tests = if @extended_time_range
68
+ { # examples taken straight from the spec
69
+ "1893-08-05T00:00:01Z" => -2337.999989,
70
+ "1899-12-28T18:00:00Z" => -1.25,
71
+ "1910-02-03T10:05:54Z" => 3687.4207639,
72
+ "1900-01-01T12:00:00Z" => 2.5, # wrongly indicated as 1.5 in the spec!
73
+ "9999-12-31T23:59:59Z" => 2958465.9999884
74
+ }
75
+ else
76
+ { # examples taken inside the possible values
77
+ "1970-01-01T00:00:00Z" => 25569.0, # Unix epoch
78
+ "1970-01-01T12:00:00Z" => 25569.5,
79
+ "2000-01-01T00:00:00Z" => 36526.0,
80
+ "2038-01-19T03:14:07Z" => 50424.134803, # max signed timestamp in 32bit
81
+ }
82
+ end
83
+ tests.each do |time_string, expected|
84
+ serial = Axlsx::DateTimeConverter::time_to_serial Time.parse(time_string)
85
+ assert_in_delta expected, serial, @margin_of_error
86
+ end
87
+ end
88
+
89
+ def test_time_to_serial_1904
90
+ Axlsx::Workbook.date1904 = true
91
+ # ruby 1.8.7 cannot parse dates prior to epoch. see http://ruby-doc.org/core-1.8.7/Time.html
92
+
93
+ tests = if @extended_time_range
94
+ { # examples taken straight from the spec
95
+ "1893-08-05T00:00:01Z" => -3799.999989,
96
+ "1910-02-03T10:05:54Z" => 2225.4207639,
97
+ "1904-01-01T12:00:00Z" => 0.5000000,
98
+ "9999-12-31T23:59:59Z" => 2957003.9999884
99
+ }
100
+ else
101
+ { # examples taken inside the possible values
102
+ "1970-01-01T00:00:00Z" => 24107.0, # Unix epoch
103
+ "1970-01-01T12:00:00Z" => 24107.5,
104
+ "2000-01-01T00:00:00Z" => 35064.0,
105
+ "2038-01-19T03:14:07Z" => 48962.134803, # max signed timestamp in 32bit
106
+ }
107
+ end
108
+ tests.each do |time_string, expected|
109
+ serial = Axlsx::DateTimeConverter::time_to_serial Time.parse(time_string)
110
+ assert_in_delta expected, serial, @margin_of_error
111
+ end
112
+ end
113
+
114
+ def test_timezone
115
+ utc = Time.utc 2012 # January 1st, 2012 at 0:00 UTC
116
+ local = begin
117
+ Time.new 2012, 1, 1, 1, 0, 0, 3600 # January 1st, 2012 at 1:00 GMT+1
118
+ rescue ArgumentError
119
+ Time.parse "2012-01-01 01:00:00 +0100"
120
+ end
121
+ assert_equal local, utc
122
+ assert_equal Axlsx::DateTimeConverter::time_to_serial(local), Axlsx::DateTimeConverter::time_to_serial(utc)
123
+ Axlsx::Workbook.date1904 = true
124
+ assert_equal Axlsx::DateTimeConverter::time_to_serial(local), Axlsx::DateTimeConverter::time_to_serial(utc)
125
+ end
126
+
127
+ end
@@ -0,0 +1,69 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestConverter < Test::Unit::TestCase
5
+ def setup
6
+ @converter = Axlsx::Converter.new
7
+ @margin_of_error = 0.000_001
8
+ end
9
+
10
+ def test_date_to_serial_1900
11
+ { # examples taken straight from the spec
12
+ "1893-08-05" => -2338.0,
13
+ "1900-01-01" => 2.0,
14
+ "1910-02-03" => 3687.0,
15
+ "2006-02-01" => 38749.0,
16
+ "9999-12-31" => 2958465.0,
17
+ }.each do |date_string, expected|
18
+ serial = @converter.date_to_serial Date.parse(date_string)
19
+ assert_equal serial, expected
20
+ end
21
+ end
22
+
23
+ def test_date_to_serial_1904
24
+ { # examples taken straight from the spec
25
+ "1893-08-05" => -3800.0,
26
+ "1904-01-01" => 0.0,
27
+ "1910-02-03" => 2225.0,
28
+ "2006-02-01" => 37287.0,
29
+ "9999-12-31" => 2957003.0,
30
+ }.each do |date_string, expected|
31
+ serial = @converter.date_to_serial Date.parse(date_string), true
32
+ assert_equal serial, expected
33
+ end
34
+ end
35
+
36
+ def test_time_to_serial_1900
37
+ { # examples taken straight from the spec
38
+ "1893-08-05T00:00:01Z" => -2337.999989,
39
+ "1899-12-28T18:00:00Z" => -1.25,
40
+ "1910-02-03T10:05:54Z" => 3687.4207639,
41
+ "1900-01-01T12:00:00Z" => 2.5, # wrongly indicated as 1.5 in the spec!
42
+ "9999-12-31T23:59:59Z" => 2958465.9999884,
43
+ }.each do |time_string, expected|
44
+ serial = @converter.time_to_serial Time.parse(time_string)
45
+ assert_in_delta serial, expected, @margin_of_error
46
+ end
47
+ end
48
+
49
+ def test_time_to_serial_1904
50
+ { # examples taken straight from the spec
51
+ "1893-08-05T00:00:01Z" => -3799.999989,
52
+ "1910-02-03T10:05:54Z" => 2225.4207639,
53
+ "1904-01-01T12:00:00Z" => 0.5000000,
54
+ "9999-12-31T23:59:59Z" => 2957003.9999884,
55
+ }.each do |time_string, expected|
56
+ serial = @converter.time_to_serial Time.parse(time_string), true
57
+ assert_in_delta serial, expected, @margin_of_error
58
+ end
59
+ end
60
+
61
+ def test_timezone
62
+ utc = Time.utc 2012 # January 1st, 2012 at 0:00 UTC
63
+ local = Time.new 2012, 1, 1, 1, 0, 0, 3600 # January 1st, 2012 at 1:00 GMT+1
64
+ assert_equal local, utc
65
+ assert_equal @converter.time_to_serial(local), @converter.time_to_serial(utc)
66
+ assert_equal @converter.time_to_serial(local, true), @converter.time_to_serial(utc, true)
67
+ end
68
+
69
+ end
@@ -0,0 +1,100 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestPageMargins < Test::Unit::TestCase
5
+
6
+ def setup
7
+ p = Axlsx::Package.new
8
+ ws = p.workbook.add_worksheet :name=>"hmmm"
9
+ @pm = ws.page_margins
10
+ end
11
+
12
+ def test_initialize
13
+ assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.left)
14
+ assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.right)
15
+ assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.top)
16
+ assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.bottom)
17
+ assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.header)
18
+ assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.footer)
19
+ end
20
+
21
+ def test_initialize_with_options
22
+ optioned = Axlsx::PageMargins.new(:left => 2, :right => 3, :top => 2, :bottom => 1, :header => 0.1, :footer => 0.1)
23
+ assert_equal(2, optioned.left)
24
+ assert_equal(3, optioned.right)
25
+ assert_equal(2, optioned.top)
26
+ assert_equal(1, optioned.bottom)
27
+ assert_equal(0.1, optioned.header)
28
+ assert_equal(0.1, optioned.footer)
29
+ end
30
+
31
+
32
+ def test_set_all_values
33
+ @pm.set(:left => 1.1, :right => 1.2, :top => 1.3, :bottom => 1.4, :header => 0.8, :footer => 0.9)
34
+ assert_equal(1.1, @pm.left)
35
+ assert_equal(1.2, @pm.right)
36
+ assert_equal(1.3, @pm.top)
37
+ assert_equal(1.4, @pm.bottom)
38
+ assert_equal(0.8, @pm.header)
39
+ assert_equal(0.9, @pm.footer)
40
+ end
41
+
42
+ def test_set_some_values
43
+ @pm.set(:left => 1.1, :right => 1.2)
44
+ assert_equal(1.1, @pm.left)
45
+ assert_equal(1.2, @pm.right)
46
+ assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.top)
47
+ assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.bottom)
48
+ assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.header)
49
+ assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.footer)
50
+ end
51
+
52
+ def test_to_xml
53
+ @pm.left = 1.1
54
+ @pm.right = 1.2
55
+ @pm.top = 1.3
56
+ @pm.bottom = 1.4
57
+ @pm.header = 0.8
58
+ @pm.footer = 0.9
59
+ xml = Nokogiri::XML::Builder.new
60
+ @pm.to_xml(xml)
61
+ doc = Nokogiri::XML.parse(xml.to_xml)
62
+ assert_equal(1, doc.xpath(".//pageMargins[@left=1.1][@right=1.2][@top=1.3][@bottom=1.4][@header=0.8][@footer=0.9]").size)
63
+ end
64
+
65
+ def test_left
66
+ assert_raise(ArgumentError) { @pm.left = -1.2 }
67
+ assert_nothing_raised { @pm.left = 1.5 }
68
+ assert_equal(@pm.left, 1.5)
69
+ end
70
+
71
+ def test_right
72
+ assert_raise(ArgumentError) { @pm.right = -1.2 }
73
+ assert_nothing_raised { @pm.right = 1.5 }
74
+ assert_equal(@pm.right, 1.5)
75
+ end
76
+
77
+ def test_top
78
+ assert_raise(ArgumentError) { @pm.top = -1.2 }
79
+ assert_nothing_raised { @pm.top = 1.5 }
80
+ assert_equal(@pm.top, 1.5)
81
+ end
82
+
83
+ def test_bottom
84
+ assert_raise(ArgumentError) { @pm.bottom = -1.2 }
85
+ assert_nothing_raised { @pm.bottom = 1.5 }
86
+ assert_equal(@pm.bottom, 1.5)
87
+ end
88
+
89
+ def test_header
90
+ assert_raise(ArgumentError) { @pm.header = -1.2 }
91
+ assert_nothing_raised { @pm.header = 1.5 }
92
+ assert_equal(@pm.header, 1.5)
93
+ end
94
+
95
+ def test_footer
96
+ assert_raise(ArgumentError) { @pm.footer = -1.2 }
97
+ assert_nothing_raised { @pm.footer = 1.5 }
98
+ assert_equal(@pm.footer, 1.5)
99
+ end
100
+ end
@@ -12,6 +12,14 @@ class TestRow < Test::Unit::TestCase
12
12
  def test_initialize
13
13
  assert(@row.cells.empty?, "no cells by default")
14
14
  assert_equal(@row.worksheet, @ws, "has a reference to the worksheet")
15
+ assert_nil(@row.height, "height defaults to nil")
16
+ assert(!@row.custom_height?, "no custom height by default")
17
+ end
18
+
19
+ def test_initialize_with_fixed_height
20
+ row = @ws.add_row([1,2,3,4,5], :height=>40)
21
+ assert_equal(40, row.height)
22
+ assert(row.custom_height?)
15
23
  end
16
24
 
17
25
  def test_style
@@ -33,4 +41,32 @@ class TestRow < Test::Unit::TestCase
33
41
  r = @ws.add_row [1,2,3], :style=>0, :types=>:integer
34
42
  assert_equal(r.cells.size, 3)
35
43
  end
44
+
45
+ def test_custom_height
46
+ @row.height = 20
47
+ assert(@row.custom_height?)
48
+ end
49
+
50
+ def test_height
51
+ assert_raise(ArgumentError) { @row.height = -3 }
52
+ assert_nothing_raised { @row.height = 15 }
53
+ assert_equal(15, @row.height)
54
+ end
55
+
56
+ def test_to_xml_without_custom_height
57
+ xml = Nokogiri::XML::Builder.new
58
+ @row.to_xml(xml)
59
+ doc = Nokogiri::XML.parse(xml.to_xml)
60
+ assert_equal(0, doc.xpath(".//row[@ht]").size)
61
+ assert_equal(0, doc.xpath(".//row[@customHeight]").size)
62
+ end
63
+
64
+ def test_to_xml_with_custom_height
65
+ @row.height = 20
66
+ xml = Nokogiri::XML::Builder.new
67
+ @row.to_xml(xml)
68
+ doc = Nokogiri::XML.parse(xml.to_xml)
69
+ assert_equal(1, doc.xpath(".//row[@ht=20][@customHeight=1]").size)
70
+ end
71
+
36
72
  end
@@ -13,6 +13,29 @@ class TestWorksheet < Test::Unit::TestCase
13
13
  assert_equal(ws.pn, "worksheets/sheet2.xml")
14
14
  end
15
15
 
16
+ def test_page_margins
17
+ assert(@ws.page_margins.is_a? Axlsx::PageMargins)
18
+ end
19
+
20
+ def test_page_margins_yeild
21
+ @ws.page_margins do |pm|
22
+ assert(pm.is_a? Axlsx::PageMargins)
23
+ assert(@ws.page_margins == pm)
24
+ end
25
+ end
26
+
27
+ def test_initialization_options
28
+ page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2}
29
+ optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins)
30
+ assert_equal(optioned.page_margins.left, page_margins[:left])
31
+ assert_equal(optioned.page_margins.right, page_margins[:right])
32
+ assert_equal(optioned.page_margins.top, page_margins[:top])
33
+ assert_equal(optioned.page_margins.bottom, page_margins[:bottom])
34
+ assert_equal(optioned.page_margins.header, page_margins[:header])
35
+ assert_equal(optioned.page_margins.footer, page_margins[:footer])
36
+ assert_equal(optioned.name, 'bob')
37
+ end
38
+
16
39
  def test_rels_pn
17
40
  assert_equal(@ws.rels_pn, "worksheets/_rels/sheet1.xml.rels")
18
41
  ws = @ws.workbook.add_worksheet
@@ -39,6 +62,10 @@ class TestWorksheet < Test::Unit::TestCase
39
62
  @ws.add_row [1, 2, 3]
40
63
  @ws.add_row [4, 5, 6]
41
64
  range = @ws["A1:C2"]
65
+ first_row = @ws[1]
66
+ last_row = @ws[2]
67
+ assert_equal(@ws.rows[0],first_row)
68
+ assert_equal(@ws.rows[1],last_row)
42
69
  assert_equal(range.size, 6)
43
70
  assert_equal(range.first, @ws.rows.first.cells.first)
44
71
  assert_equal(range.last, @ws.rows.last.cells.last)
@@ -116,6 +143,19 @@ class TestWorksheet < Test::Unit::TestCase
116
143
  assert(errors.empty?, "error free validation")
117
144
  end
118
145
 
146
+ def test_valid_with_page_margins
147
+ @ws.page_margins.set :left => 9
148
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
149
+ doc = Nokogiri::XML(@ws.to_xml)
150
+ errors = []
151
+ schema.validate(doc).each do |error|
152
+ errors.push error
153
+ puts error.message
154
+ end
155
+ assert(errors.empty?, "error free validation")
156
+
157
+ end
158
+
119
159
  def test_relationships
120
160
  assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
121
161
  c = @ws.add_chart Axlsx::Pie3DChart
@@ -129,6 +169,11 @@ class TestWorksheet < Test::Unit::TestCase
129
169
  assert_raise(ArgumentError, "worksheet name must be unique") { n = @ws.name; @ws.workbook.add_worksheet(:name=> @ws) }
130
170
  end
131
171
 
172
+ def test_name_size
173
+ assert_raise(ArgumentError, "name too long!") { @ws.name = Array.new(32, "A").join('') }
174
+ assert_nothing_raised { @ws.name = Array.new(31, "A").join('') }
175
+ end
176
+
132
177
  def test_update_auto_with_data
133
178
  small = @ws.workbook.styles.add_style(:sz=>2)
134
179
  big = @ws.workbook.styles.add_style(:sz=>10)
@@ -143,6 +188,33 @@ class TestWorksheet < Test::Unit::TestCase
143
188
  assert_equal(@ws.auto_fit_data[0], {:sz=>10,:longest=>"mule", :fixed=>nil}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column")
144
189
  end
145
190
 
191
+ def test_set_fixed_width_column
192
+ @ws.add_row ["mule", "donkey", "horse"], :widths => [20, :ignore, nil]
193
+ assert(@ws.auto_fit_data.size == 3, "a data item for each column")
194
+ assert_equal({:sz=>11, :longest=>"mule", :fixed=>20 }, @ws.auto_fit_data[0], "adding a row with fixed width updates :fixed attribute")
195
+ assert_equal({:sz=>11, :longest=>"", :fixed=>nil}, @ws.auto_fit_data[1], ":ignore does not set any data")
196
+ assert_equal({:sz=>11, :longest=>"horse", :fixed=>nil}, @ws.auto_fit_data[2], "nil, well really anything else just works as normal")
197
+ @ws.add_row ["mule", "donkey", "horse"]
198
+ assert_equal({:sz=>11, :longest=>"donkey", :fixed=>nil}, @ws.auto_fit_data[1])
199
+
200
+ end
201
+
202
+ def test_fixed_widths_with_merged_cells
203
+ @ws.add_row ["hey, I'm like really long and stuff so I think you will merge me."]
204
+ @ws.merge_cells "A1:C1"
205
+ @ws.add_row ["but Im Short!"], :widths=> [14.8]
206
+ assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), 14.8)
207
+ end
208
+
209
+ def test_fixed_width_to_auto
210
+ @ws.add_row ["hey, I'm like really long and stuff so I think you will merge me."]
211
+ @ws.merge_cells "A1:C1"
212
+ @ws.add_row ["but Im Short!"], :widths=> [14.8]
213
+ assert_equal(@ws.send(:auto_width, @ws.auto_fit_data[0]), 14.8)
214
+ @ws.add_row ["no, I like auto!"], :widths=>[:auto]
215
+ assert_equal(@ws.auto_fit_data[0][:fixed], nil)
216
+ end
217
+
146
218
  def test_auto_width
147
219
  assert(@ws.send(:auto_width, {:sz=>11, :longest=>"fisheries"}) > @ws.send(:auto_width, {:sz=>11, :longest=>"fish"}), "longer strings get a longer auto_width at the same font size")
148
220
 
@@ -150,6 +222,12 @@ class TestWorksheet < Test::Unit::TestCase
150
222
  assert_equal(@ws.send(:auto_width, {:sz=>11, :longest => "This is a really long string", :fixed=>0.2}), 0.2, "fixed rules!")
151
223
  end
152
224
 
225
+ def test_fixed_height
226
+ @ws.add_row [1, 2, 3], :height => 40
227
+ assert_equal(40, @ws.rows[-1].height)
228
+ end
229
+
230
+
153
231
  def test_set_column_width
154
232
  @ws.add_row ["chasing windmills", "penut"]
155
233
  assert_equal(@ws.auto_fit_data[0][:fixed], nil, 'no fixed by default')
@@ -160,7 +238,6 @@ class TestWorksheet < Test::Unit::TestCase
160
238
  assert_raise(ArgumentError, 'only accept Integer, Float or Fixnum') { @ws.column_widths 2, 7, "-1" }
161
239
  end
162
240
 
163
-
164
241
  def test_merge_cells
165
242
  assert(@ws.merged_cells.is_a?(Array))
166
243
  assert_equal(@ws.merged_cells.size, 0)