axlsx 1.0.17 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. data/CHANGELOG.md +11 -3
  2. data/README.md +159 -51
  3. data/examples/example.csv +1000 -0
  4. data/examples/example.rb +255 -150
  5. data/examples/example.xlsx +0 -0
  6. data/examples/example_streamed.xlsx +0 -0
  7. data/examples/no-use_autowidth.xlsx +0 -0
  8. data/examples/shared_strings_example.xlsx +0 -0
  9. data/lib/axlsx/content_type/content_type.rb +12 -12
  10. data/lib/axlsx/content_type/default.rb +9 -6
  11. data/lib/axlsx/content_type/override.rb +12 -8
  12. data/lib/axlsx/doc_props/app.rb +37 -40
  13. data/lib/axlsx/doc_props/core.rb +12 -17
  14. data/lib/axlsx/drawing/axis.rb +38 -19
  15. data/lib/axlsx/drawing/bar_3D_chart.rb +33 -32
  16. data/lib/axlsx/drawing/bar_series.rb +13 -14
  17. data/lib/axlsx/drawing/cat_axis.rb +15 -14
  18. data/lib/axlsx/drawing/cat_axis_data.rb +16 -18
  19. data/lib/axlsx/drawing/chart.rb +39 -40
  20. data/lib/axlsx/drawing/drawing.rb +15 -12
  21. data/lib/axlsx/drawing/graphic_frame.rb +21 -21
  22. data/lib/axlsx/drawing/hyperlink.rb +12 -11
  23. data/lib/axlsx/drawing/line_3D_chart.rb +30 -28
  24. data/lib/axlsx/drawing/line_series.rb +11 -11
  25. data/lib/axlsx/drawing/marker.rb +10 -8
  26. data/lib/axlsx/drawing/named_axis_data.rb +36 -0
  27. data/lib/axlsx/drawing/one_cell_anchor.rb +17 -16
  28. data/lib/axlsx/drawing/pic.rb +25 -37
  29. data/lib/axlsx/drawing/picture_locking.rb +21 -18
  30. data/lib/axlsx/drawing/pie_3D_chart.rb +10 -8
  31. data/lib/axlsx/drawing/pie_series.rb +15 -12
  32. data/lib/axlsx/drawing/scaling.rb +10 -10
  33. data/lib/axlsx/drawing/scatter_chart.rb +69 -0
  34. data/lib/axlsx/drawing/scatter_series.rb +39 -0
  35. data/lib/axlsx/drawing/ser_axis.rb +10 -10
  36. data/lib/axlsx/drawing/series.rb +15 -15
  37. data/lib/axlsx/drawing/series_title.rb +14 -14
  38. data/lib/axlsx/drawing/title.rb +26 -26
  39. data/lib/axlsx/drawing/two_cell_anchor.rb +18 -20
  40. data/lib/axlsx/drawing/val_axis.rb +8 -7
  41. data/lib/axlsx/drawing/val_axis_data.rb +17 -17
  42. data/lib/axlsx/drawing/view_3D.rb +22 -20
  43. data/lib/axlsx/package.rb +60 -43
  44. data/lib/axlsx/rels/relationship.rb +11 -8
  45. data/lib/axlsx/rels/relationships.rb +7 -1
  46. data/lib/axlsx/stylesheet/#num_fmt.rb# +69 -0
  47. data/lib/axlsx/stylesheet/border.rb +27 -23
  48. data/lib/axlsx/stylesheet/border_pr.rb +16 -15
  49. data/lib/axlsx/stylesheet/cell_alignment.rb +23 -21
  50. data/lib/axlsx/stylesheet/cell_protection.rb +10 -7
  51. data/lib/axlsx/stylesheet/cell_style.rb +8 -5
  52. data/lib/axlsx/stylesheet/color.rb +20 -14
  53. data/lib/axlsx/stylesheet/fill.rb +7 -5
  54. data/lib/axlsx/stylesheet/font.rb +21 -14
  55. data/lib/axlsx/stylesheet/gradient_fill.rb +19 -16
  56. data/lib/axlsx/stylesheet/gradient_stop.rb +9 -5
  57. data/lib/axlsx/stylesheet/num_fmt.rb +12 -6
  58. data/lib/axlsx/stylesheet/pattern_fill.rb +25 -10
  59. data/lib/axlsx/stylesheet/styles.rb +47 -35
  60. data/lib/axlsx/stylesheet/table_style.rb +9 -4
  61. data/lib/axlsx/stylesheet/table_style_element.rb +10 -7
  62. data/lib/axlsx/stylesheet/table_styles.rb +11 -8
  63. data/lib/axlsx/stylesheet/xf.rb +29 -25
  64. data/lib/axlsx/util/constants.rb +20 -14
  65. data/lib/axlsx/util/simple_typed_list.rb +18 -9
  66. data/lib/axlsx/util/validators.rb +13 -6
  67. data/lib/axlsx/version.rb +1 -1
  68. data/lib/axlsx/workbook/shared_strings_table.rb +19 -21
  69. data/lib/axlsx/workbook/workbook.rb +58 -34
  70. data/lib/axlsx/workbook/worksheet/cell.rb +149 -132
  71. data/lib/axlsx/workbook/worksheet/col.rb +114 -0
  72. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  73. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
  74. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  75. data/lib/axlsx/workbook/worksheet/row.rb +40 -23
  76. data/lib/axlsx/workbook/worksheet/table.rb +96 -0
  77. data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
  78. data/lib/axlsx/workbook/worksheet/worksheet.rb +243 -127
  79. data/lib/axlsx.rb +30 -9
  80. data/lib/schema/dc.xsd +5 -5
  81. data/lib/schema/dcmitype.xsd +5 -3
  82. data/lib/schema/dcterms.xsd +15 -15
  83. data/lib/schema/opc-coreProperties.xsd +6 -2
  84. data/lib/schema/xml.xsd +7 -8
  85. data/test/#benchmark.txt# +7 -0
  86. data/test/#tc_helper.rb# +3 -0
  87. data/test/benchmark.rb +81 -0
  88. data/test/benchmark.rb~ +0 -0
  89. data/test/benchmark.txt +6 -0
  90. data/test/benchmark.txt~ +6 -0
  91. data/test/content_type/tc_content_type.rb +30 -32
  92. data/test/content_type/tc_default.rb +8 -23
  93. data/test/content_type/tc_override.rb +7 -21
  94. data/test/doc_props/tc_app.rb +2 -8
  95. data/test/doc_props/tc_core.rb +6 -7
  96. data/test/drawing/tc_axis.rb +7 -3
  97. data/test/drawing/tc_bar_3D_chart.rb +6 -7
  98. data/test/drawing/tc_bar_series.rb +4 -5
  99. data/test/drawing/tc_cat_axis.rb +2 -3
  100. data/test/drawing/tc_cat_axis_data.rb +2 -3
  101. data/test/drawing/tc_chart.rb +13 -14
  102. data/test/drawing/tc_drawing.rb +7 -8
  103. data/test/drawing/tc_graphic_frame.rb +3 -4
  104. data/test/drawing/tc_hyperlink.rb +2 -3
  105. data/test/drawing/tc_line_3d_chart.rb +5 -6
  106. data/test/drawing/tc_line_series.rb +3 -4
  107. data/test/drawing/tc_marker.rb +3 -4
  108. data/test/drawing/tc_one_cell_anchor.rb +6 -7
  109. data/test/drawing/tc_pic.rb +8 -9
  110. data/test/drawing/tc_picture_locking.rb +2 -3
  111. data/test/drawing/tc_pie_3D_chart.rb +5 -6
  112. data/test/drawing/tc_pie_series.rb +4 -5
  113. data/test/drawing/tc_scaling.rb +3 -4
  114. data/test/drawing/tc_scatter_chart.rb +43 -0
  115. data/test/drawing/tc_scatter_series.rb +20 -0
  116. data/test/drawing/tc_ser_axis.rb +2 -3
  117. data/test/drawing/tc_series.rb +4 -5
  118. data/test/drawing/tc_series_title.rb +4 -5
  119. data/test/drawing/tc_title.rb +4 -5
  120. data/test/drawing/tc_two_cell_anchor.rb +4 -5
  121. data/test/drawing/tc_val_axis.rb +2 -3
  122. data/test/drawing/tc_val_axis_data.rb +2 -3
  123. data/test/drawing/tc_view_3D.rb +6 -7
  124. data/test/example.csv +1000 -0
  125. data/test/example.xlsx +0 -0
  126. data/test/example_streamed.xlsx +0 -0
  127. data/test/profile.rb +33 -0
  128. data/test/rels/tc_relationship.rb +5 -6
  129. data/test/rels/tc_relationships.rb +4 -5
  130. data/test/stylesheet/tc_border.rb +3 -4
  131. data/test/stylesheet/tc_border_pr.rb +3 -4
  132. data/test/stylesheet/tc_cell_alignment.rb +10 -6
  133. data/test/stylesheet/tc_cell_protection.rb +2 -3
  134. data/test/stylesheet/tc_cell_style.rb +2 -3
  135. data/test/stylesheet/tc_color.rb +2 -3
  136. data/test/stylesheet/tc_fill.rb +1 -2
  137. data/test/stylesheet/tc_font.rb +12 -5
  138. data/test/stylesheet/tc_gradient_fill.rb +1 -2
  139. data/test/stylesheet/tc_gradient_stop.rb +1 -2
  140. data/test/stylesheet/tc_num_fmt.rb +1 -2
  141. data/test/stylesheet/tc_pattern_fill.rb +3 -4
  142. data/test/stylesheet/tc_styles.rb +16 -6
  143. data/test/stylesheet/tc_table_style.rb +2 -3
  144. data/test/stylesheet/tc_table_style_element.rb +2 -3
  145. data/test/stylesheet/tc_table_styles.rb +3 -4
  146. data/test/stylesheet/tc_xf.rb +16 -17
  147. data/test/tc_axlsx.rb +39 -0
  148. data/test/tc_axlsx.rb~ +0 -0
  149. data/test/tc_helper.rb +3 -0
  150. data/test/tc_helper.rb~ +3 -0
  151. data/test/tc_package.rb +13 -10
  152. data/test/util/tc_simple_typed_list.rb +8 -9
  153. data/test/util/tc_validators.rb +7 -8
  154. data/test/workbook/tc_shared_strings_table.rb +5 -6
  155. data/test/workbook/tc_workbook.rb +24 -6
  156. data/test/workbook/worksheet/table/tc_table.rb +71 -0
  157. data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
  158. data/test/workbook/worksheet/tc_cell.rb +62 -18
  159. data/test/workbook/worksheet/tc_col.rb +59 -0
  160. data/test/workbook/worksheet/tc_col.rb~ +10 -0
  161. data/test/workbook/worksheet/tc_date_time_converter.rb +126 -0
  162. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  163. data/test/workbook/worksheet/tc_row.rb +54 -4
  164. data/test/workbook/worksheet/tc_worksheet.rb +177 -34
  165. metadata +69 -41
  166. data/test/drawing/tc_hyperlink.rb~ +0 -71
  167. data/test/workbook/tc_shared_strings_table.rb~ +0 -8
@@ -1,11 +1,10 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
  class TestSimpleTypedList < Test::Unit::TestCase
4
- def setup
3
+ def setup
5
4
  @list = Axlsx::SimpleTypedList.new Fixnum
6
5
  end
7
6
 
8
- def teardown
7
+ def teardown
9
8
  end
10
9
 
11
10
  def test_type_is_a_class_or_array_of_class
@@ -15,18 +14,18 @@ class TestSimpleTypedList < Test::Unit::TestCase
15
14
  assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new "1" }
16
15
  assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new [Integer, "Class"] }
17
16
  end
18
-
17
+
19
18
  def test_indexed_based_assignment
20
19
  #should not allow nil assignment
21
20
  assert_raise(ArgumentError) { @list[0] = nil }
22
21
  assert_raise(ArgumentError) { @list[0] = "1" }
23
22
  assert_nothing_raised { @list[0] = 1 }
24
23
  end
25
-
24
+
26
25
  def test_concat_assignment
27
26
  assert_raise(ArgumentError) { @list << nil }
28
27
  assert_raise(ArgumentError) { @list << "1" }
29
- assert_nothing_raised { @list << 1 }
28
+ assert_nothing_raised { @list << 1 }
30
29
  end
31
30
 
32
31
  def test_concat_should_return_index
@@ -59,8 +58,8 @@ class TestSimpleTypedList < Test::Unit::TestCase
59
58
  assert_nothing_raised { @list.delete_at 3 }
60
59
  @list.unlock
61
60
  #ignore garbage
62
- assert_nothing_raised { @list.delete 0 }
63
- assert_nothing_raised { @list.delete 9 }
61
+ assert_nothing_raised { @list.delete 0 }
62
+ assert_nothing_raised { @list.delete 9 }
64
63
  end
65
64
 
66
65
  end
@@ -1,30 +1,29 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
  class TestValidators < Test::Unit::TestCase
4
- def setup
3
+ def setup
5
4
  end
6
5
  def teardown
7
6
  end
8
-
7
+
9
8
  def test_validators
10
9
  #unsigned_int
11
10
  assert_nothing_raised { Axlsx.validate_unsigned_int 1 }
12
11
  assert_nothing_raised { Axlsx.validate_unsigned_int +1 }
13
12
  assert_raise(ArgumentError) { Axlsx.validate_unsigned_int -1 }
14
13
  assert_raise(ArgumentError) { Axlsx.validate_unsigned_int '1' }
15
-
14
+
16
15
  #int
17
16
  assert_nothing_raised { Axlsx.validate_int 1 }
18
17
  assert_nothing_raised { Axlsx.validate_int -1 }
19
18
  assert_raise(ArgumentError) { Axlsx.validate_int 'a' }
20
19
  assert_raise(ArgumentError) { Axlsx.validate_int Array }
21
-
20
+
22
21
  #boolean (as 0 or 1, :true, :false, true, false, or "true," "false")
23
22
  [0,1,:true, :false, true, false, "true", "false"].each do |v|
24
23
  assert_nothing_raised { Axlsx.validate_boolean 0 }
25
24
  end
26
25
  assert_raise(ArgumentError) { Axlsx.validate_boolean 2 }
27
-
26
+
28
27
  #string
29
28
  assert_nothing_raised { Axlsx.validate_string "1" }
30
29
  assert_raise(ArgumentError) { Axlsx.validate_string 2 }
@@ -64,7 +63,7 @@ class TestValidators < Test::Unit::TestCase
64
63
  assert_raise(ArgumentError) { Axlsx.validate_content_type nil }
65
64
  assert_raise(ArgumentError) { Axlsx.validate_content_type "http://some.url" }
66
65
  assert_raise(ArgumentError) { Axlsx.validate_content_type false }
67
-
66
+
68
67
  #relationshipType
69
68
  assert_nothing_raised { Axlsx.validate_relationship_type Axlsx::WORKBOOK_R }
70
69
  assert_raise(ArgumentError) { Axlsx.validate_relationship_type nil }
@@ -1,14 +1,13 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
 
4
3
  class TestSharedStringsTable < Test::Unit::TestCase
5
4
 
6
- def setup
5
+ def setup
7
6
  @p = Axlsx::Package.new :use_shared_strings=>true
8
7
  ws = @p.workbook.add_worksheet
9
8
  ws.add_row ['a', 1, 'b']
10
9
  ws.add_row ['b', 1, 'c']
11
- ws.add_row ['c', 1, 'd']
10
+ ws.add_row ['c', 1, 'd']
12
11
  end
13
12
 
14
13
  def test_workbook_has_shared_strings
@@ -25,9 +24,9 @@ class TestSharedStringsTable < Test::Unit::TestCase
25
24
  assert_equal(sst.unique_count, 4)
26
25
  end
27
26
 
28
- def test_valid_document
27
+ def test_valid_document
29
28
  schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
30
- doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml)
29
+ doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string)
31
30
  errors = []
32
31
  schema.validate(doc).each do |error|
33
32
  puts error.message
@@ -1,15 +1,21 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
 
4
3
  class TestWorkbook < Test::Unit::TestCase
5
- def setup
4
+ def setup
6
5
  p = Axlsx::Package.new
7
- @wb = p.workbook
6
+ @wb = p.workbook
8
7
  end
9
8
 
10
9
  def teardown
11
10
  end
12
11
 
12
+ def test_no_autowidth
13
+ assert_equal(@wb.use_autowidth, true)
14
+ assert_raise(ArgumentError) {@wb.use_autowidth = 0.1}
15
+ assert_nothing_raised {@wb.use_autowidth = false}
16
+ assert_equal(@wb.use_autowidth, false)
17
+ end
18
+
13
19
  def test_date1904
14
20
  assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
15
21
  @wb.date1904 = :false
@@ -18,6 +24,8 @@ class TestWorkbook < Test::Unit::TestCase
18
24
  assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
19
25
  end
20
26
 
27
+
28
+
21
29
  def test_shared_strings
22
30
  assert_equal(@wb.use_shared_strings, nil)
23
31
  assert_raise(ArgumentError) {@wb.use_shared_strings = 'bpb'}
@@ -42,7 +50,7 @@ class TestWorkbook < Test::Unit::TestCase
42
50
 
43
51
  def test_to_xml
44
52
  schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
45
- doc = Nokogiri::XML(@wb.to_xml)
53
+ doc = Nokogiri::XML(@wb.to_xml_string)
46
54
  errors = []
47
55
  schema.validate(doc).each do |error|
48
56
  errors.push error
@@ -60,9 +68,19 @@ class TestWorkbook < Test::Unit::TestCase
60
68
 
61
69
  def test_to_xml_adds_worksheet_when_worksheets_is_empty
62
70
  assert(@wb.worksheets.empty?)
63
- @wb.to_xml
71
+ @wb.to_xml_string
64
72
  assert(@wb.worksheets.size == 1)
65
73
  end
66
74
 
67
75
 
76
+ def test_to_xml_string_defined_names
77
+ @wb.add_worksheet do |sheet|
78
+ sheet.add_row [1, "two"]
79
+ sheet.auto_filter = "A1:B1"
80
+ end
81
+ doc = Nokogiri::XML(@wb.to_xml_string)
82
+ assert_equal(doc.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, @wb.worksheets[0].abs_auto_filter)
83
+ end
84
+
85
+
68
86
  end
@@ -0,0 +1,71 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestTable < Test::Unit::TestCase
4
+ def setup
5
+ p = Axlsx::Package.new
6
+ @ws = p.workbook.add_worksheet
7
+ 40.times do
8
+ @ws << ["aa","aa","aa","aa","aa","aa"]
9
+ end
10
+ end
11
+
12
+ def test_initialization
13
+ assert(@ws.workbook.tables.empty?)
14
+ assert(@ws.tables.empty?)
15
+
16
+ end
17
+
18
+ def test_add_table
19
+ name = "test"
20
+ table = @ws.add_table("A1:D5", :name => name)
21
+ assert(table.is_a?(Axlsx::Table), "must create a table")
22
+ assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection")
23
+ assert_equal(@ws.tables.last, table, "must be added to worksheet table collection")
24
+ assert_equal(table.name, name, "options for name are applied")
25
+
26
+ end
27
+
28
+ def test_charts
29
+ assert(@ws.drawing.charts.empty?)
30
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1])
31
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
32
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10])
33
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
34
+ end
35
+
36
+ def test_pn
37
+ table = @ws.add_table("A1:D5")
38
+ assert_equal(@ws.tables.first.pn, "tables/table1.xml")
39
+ end
40
+
41
+ def test_rId
42
+ table = @ws.add_table("A1:D5")
43
+ assert_equal(@ws.tables.first.rId, "rId1")
44
+ end
45
+
46
+ def test_index
47
+ table = @ws.add_table("A1:D5")
48
+ assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first))
49
+ end
50
+
51
+ def test_relationships
52
+ assert(@ws.relationships.empty?)
53
+ table = @ws.add_table("A1:D5")
54
+ assert_equal(@ws.relationships.size, 1, "adding a table adds a relationship")
55
+ table = @ws.add_table("F1:J5")
56
+ assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship")
57
+ end
58
+
59
+ def test_to_xml_string
60
+ table = @ws.add_table("A1:D5")
61
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
62
+ doc = Nokogiri::XML(table.to_xml_string)
63
+ errors = []
64
+ schema.validate(doc).each do |error|
65
+ errors.push error
66
+ puts error.message
67
+ end
68
+ assert(errors.empty?, "error free validation")
69
+ end
70
+
71
+ end
@@ -0,0 +1,72 @@
1
+ require 'test/unit'
2
+ require 'tc_helper.rb'
3
+
4
+ class TestTable < Test::Unit::TestCase
5
+ def setup
6
+ p = Axlsx::Package.new
7
+ @ws = p.workbook.add_worksheet
8
+ 40.times do
9
+ @ws << ["aa","aa","aa","aa","aa","aa"]
10
+ end
11
+ end
12
+
13
+ def test_initialization
14
+ assert(@ws.workbook.tables.empty?)
15
+ assert(@ws.tables.empty?)
16
+
17
+ end
18
+
19
+ def test_add_table
20
+ name = "test"
21
+ table = @ws.add_table("A1:D5", :name => name)
22
+ assert(table.is_a?(Axlsx::Table), "must create a table")
23
+ assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection")
24
+ assert_equal(@ws.tables.last, table, "must be added to worksheet table collection")
25
+ assert_equal(table.name, name, "options for name are applied")
26
+
27
+ end
28
+
29
+ def test_charts
30
+ assert(@ws.drawing.charts.empty?)
31
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1])
32
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
33
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10])
34
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
35
+ end
36
+
37
+ def test_pn
38
+ table = @ws.add_table("A1:D5")
39
+ assert_equal(@ws.tables.first.pn, "tables/table1.xml")
40
+ end
41
+
42
+ def test_rId
43
+ table = @ws.add_table("A1:D5")
44
+ assert_equal(@ws.tables.first.rId, "rId1")
45
+ end
46
+
47
+ def test_index
48
+ table = @ws.add_table("A1:D5")
49
+ assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first))
50
+ end
51
+
52
+ def test_relationships
53
+ assert(@ws.relationships.empty?)
54
+ table = @ws.add_table("A1:D5")
55
+ assert_equal(@ws.relationships.size, 1, "adding a table adds a relationship")
56
+ table = @ws.add_table("F1:J5")
57
+ assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship")
58
+ end
59
+
60
+ def test_to_xml
61
+ table = @ws.add_table("A1:D5")
62
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
63
+ doc = Nokogiri::XML(table.to_xml)
64
+ errors = []
65
+ schema.validate(doc).each do |error|
66
+ errors.push error
67
+ puts error.message
68
+ end
69
+ assert(errors.empty?, "error free validation")
70
+ end
71
+
72
+ end
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
 
4
3
  class TestCell < Test::Unit::TestCase
5
4
 
@@ -9,8 +8,11 @@ class TestCell < Test::Unit::TestCase
9
8
  p.workbook.styles.add_style :sz=>20
10
9
  @row = @ws.add_row
11
10
  @c = @row.add_cell 1, :type=>:float, :style=>1
11
+ data = (0..26).map { |index| index }
12
+ @ws.add_row data
13
+ @cAA = @ws["AA2"]
12
14
  end
13
-
15
+
14
16
  def test_initialize
15
17
  assert_equal(@row.cells.last, @c, "the cell was added to the row")
16
18
  assert_equal(@c.type, :float, "type option is applied")
@@ -18,12 +20,15 @@ class TestCell < Test::Unit::TestCase
18
20
  assert_equal(@c.value, 1.0, "type option is applied and value is casted")
19
21
  end
20
22
 
21
-
22
23
  def test_style_date_data
23
24
  c = Axlsx::Cell.new(@c.row, Time.now)
24
25
  assert_equal(Axlsx::STYLE_DATE, c.style)
25
26
  end
26
27
 
28
+ def test_row
29
+ assert_equal(@c.row, @row)
30
+ end
31
+
27
32
  def test_index
28
33
  assert_equal(@c.index, @row.cells.index(@c))
29
34
  end
@@ -36,45 +41,56 @@ class TestCell < Test::Unit::TestCase
36
41
  assert_equal(@c.r, "A1", "calculate cell reference")
37
42
  end
38
43
 
44
+ def test_wide_r
45
+ assert_equal(@cAA.r, "AA2", "calculate cell reference")
46
+ end
47
+
39
48
  def test_r_abs
40
49
  assert_equal(@c.r_abs,"$A$1", "calculate absolute cell reference")
50
+ assert_equal(@cAA.r_abs,"$AA$2", "needs to accept multi-digit columns")
41
51
  end
42
52
 
43
53
  def test_style
44
54
  assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style=@c.row.worksheet.workbook.styles.cellXfs.size }
45
- assert_nothing_raised("must allow valid style index changes") {@c.style=1}
55
+ assert_nothing_raised("must allow valid style index changes") {@c.style=1}
46
56
  assert_equal(@c.style, 1)
47
57
  end
48
58
 
49
59
  def test_type
50
- assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
60
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :date, :time, :boolean") { @c.type = :array }
51
61
  assert_nothing_raised("type can be changed") { @c.type = :string }
52
62
  assert_equal(@c.value, "1.0", "changing type casts the value")
53
-
63
+
54
64
  assert_equal(@row.add_cell(Time.now).type, :time, 'time should be time')
65
+ assert_equal(@row.add_cell(Date.today).type, :date, 'date should be date')
66
+ assert_equal(@row.add_cell(true).type, :boolean, 'boolean should be boolean')
55
67
  end
56
68
 
57
69
  def test_value
58
- assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
70
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :date, :time, :boolean") { @c.type = :array }
59
71
  assert_nothing_raised("type can be changed") { @c.type = :string }
60
72
  assert_equal(@c.value, "1.0", "changing type casts the value")
61
73
  end
62
74
 
63
75
  def test_col_ref
64
- assert_equal(@c.send(:col_ref), "A")
76
+ #TODO move to axlsx spec
77
+ assert_equal(Axlsx.col_ref(0), "A")
65
78
  end
66
79
 
67
80
  def test_cell_type_from_value
68
81
  assert_equal(@c.send(:cell_type_from_value, 1.0), :float)
69
82
  assert_equal(@c.send(:cell_type_from_value, 1), :integer)
83
+ assert_equal(@c.send(:cell_type_from_value, Date.today), :date)
70
84
  assert_equal(@c.send(:cell_type_from_value, Time.now), :time)
71
85
  assert_equal(@c.send(:cell_type_from_value, []), :string)
72
86
  assert_equal(@c.send(:cell_type_from_value, "d"), :string)
73
87
  assert_equal(@c.send(:cell_type_from_value, nil), :string)
74
88
  assert_equal(@c.send(:cell_type_from_value, -1), :integer)
89
+ assert_equal(@c.send(:cell_type_from_value, true), :boolean)
90
+ assert_equal(@c.send(:cell_type_from_value, false), :boolean)
75
91
  end
76
92
 
77
- def test_cast_value
93
+ def test_cast_value
78
94
  @c.type = :string
79
95
  assert_equal(@c.send(:cast_value, 1.0), "1.0")
80
96
  @c.type = :integer
@@ -82,8 +98,12 @@ class TestCell < Test::Unit::TestCase
82
98
  @c.type = :float
83
99
  assert_equal(@c.send(:cast_value, "1.0"), 1.0)
84
100
  @c.type = :string
85
- assert_equal(@c.send(:cast_value, nil), "")
86
-
101
+ assert_equal(@c.send(:cast_value, nil), nil)
102
+ @c.type = :float
103
+ assert_equal(@c.send(:cast_value, nil), nil)
104
+ @c.type = :boolean
105
+ assert_equal(@c.send(:cast_value, true), 1)
106
+ assert_equal(@c.send(:cast_value, false), 0)
87
107
  end
88
108
 
89
109
  def test_color
@@ -181,7 +201,7 @@ class TestCell < Test::Unit::TestCase
181
201
  @c.row.add_cell 2
182
202
  @c.row.add_cell 3
183
203
  @c.merge "A2"
184
- assert_equal(@c.row.worksheet.merged_cells.last, "A1:A2")
204
+ assert_equal(@c.row.worksheet.merged_cells.last, "A1:A2")
185
205
  end
186
206
 
187
207
  def test_merge_with_cell
@@ -189,19 +209,43 @@ class TestCell < Test::Unit::TestCase
189
209
  @c.row.add_cell 2
190
210
  @c.row.add_cell 3
191
211
  @c.merge @row.cells.last
192
- assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1")
212
+ assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1")
193
213
  end
194
214
 
195
215
  def test_equality
196
- c2 = @row.add_cell 1, :type=>:float, :style=>1
197
- assert(c2.shareable(@c))
216
+ c2 = @row.add_cell 1, :type=>:float, :style=>1
217
+
218
+ assert_equal(c2.shareable_hash,@c.shareable_hash)
198
219
  c3 = @row.add_cell 2, :type=>:float, :style=>1
199
220
  c4 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF"
200
- assert_equal(c4.shareable(c2) ,false)
221
+ assert_equal(c4.shareable_hash == c2.shareable_hash,false)
201
222
  c5 = @row.add_cell 1, :type=>:float, :style=>1, :color => "#FFFFFFFF"
202
- assert(c5.shareable(c4))
223
+ assert_equal(c5.shareable_hash, c4.shareable_hash)
224
+
225
+ end
203
226
 
227
+ def test_ssti
228
+ assert_raise(ArgumentError, "ssti must be an unsigned integer!") { @c.send(:ssti=, -1) }
229
+ @c.send :ssti=, 1
230
+ assert_equal(@c.ssti, 1)
204
231
  end
205
232
 
233
+ def test_to_xml_string
234
+ c_xml = Nokogiri::XML(@c.to_xml_string(1,1))
235
+ assert_equal(c_xml.xpath("/c[@s=1]").size, 1)
236
+ end
237
+ def test_to_xml
238
+ # TODO This could use some much more stringent testing related to the xml content generated!
239
+ row = @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"]
240
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
241
+ doc = Nokogiri::XML(@ws.to_xml_string)
242
+ errors = []
243
+ schema.validate(doc).each do |error|
244
+ errors.push error
245
+ puts error.message
246
+ end
247
+ assert(errors.empty?, "error free validation")
248
+
249
+ end
206
250
 
207
251
  end
@@ -0,0 +1,59 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestCol < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @col = Axlsx::Col.new 1, 1
7
+ end
8
+
9
+ def test_min_max_required
10
+ assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new }
11
+ assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new nil, nil }
12
+ assert_nothing_raised { Axlsx::Col.new 1, 1 }
13
+ end
14
+
15
+ def test_bestFit
16
+ assert_equal(@col.bestFit, nil)
17
+ assert_raise(NoMethodError, 'bestFit is read only') { @col.bestFit = 'bob' }
18
+ @col.width = 1.999
19
+ assert_equal(@col.bestFit, true, 'bestFit should be true when width has been set')
20
+ end
21
+
22
+ def test_collapsed
23
+ assert_equal(@col.collapsed, nil)
24
+ assert_raise(ArgumentError, 'collapsed must be boolean(ish)') { @col.collapsed = 'bob' }
25
+ assert_nothing_raised('collapsed must be boolean(ish)') { @col.collapsed = true }
26
+ end
27
+
28
+ def test_customWidth
29
+ assert_equal(@col.customWidth, nil)
30
+ @col.width = 3
31
+ assert_raise(NoMethodError, 'customWidth is read only') { @col.customWidth = 3 }
32
+ assert_equal(@col.customWidth, true, 'customWidth is true when width is set')
33
+ end
34
+
35
+ def test_hidden
36
+ assert_equal(@col.hidden, nil)
37
+ assert_raise(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = 'bob' }
38
+ assert_nothing_raised(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = true }
39
+ end
40
+
41
+ def test_outlineLevel
42
+ assert_equal(@col.outlineLevel, nil)
43
+ assert_raise(ArgumentError, 'outline level cannot be negative') { @col.outlineLevel = -1 }
44
+ assert_raise(ArgumentError, 'outline level cannot be greater than 7') { @col.outlineLevel = 8 }
45
+ assert_nothing_raised('can set outlineLevel') { @col.outlineLevel = 1 }
46
+ end
47
+
48
+ def test_phonetic
49
+ assert_equal(@col.phonetic, nil)
50
+ assert_raise(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = 'bob' }
51
+ assert_nothing_raised(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = true }
52
+ end
53
+
54
+ def test_style
55
+ assert_equal(@col.style, nil)
56
+ #TODO check that the style specified is actually in the styles xfs collection
57
+ end
58
+
59
+ end
@@ -0,0 +1,10 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestCol < Test::Unit::TestCase
4
+
5
+ def setup
6
+
7
+ end
8
+
9
+
10
+ end