axlsx 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/Rakefile +13 -0
  2. data/lib/axlsx.rb +38 -0
  3. data/lib/axlsx/content_type/content_type.rb +23 -0
  4. data/lib/axlsx/content_type/default.rb +32 -0
  5. data/lib/axlsx/content_type/override.rb +30 -0
  6. data/lib/axlsx/doc_props/app.rb +148 -0
  7. data/lib/axlsx/doc_props/core.rb +34 -0
  8. data/lib/axlsx/drawing/axis.rb +78 -0
  9. data/lib/axlsx/drawing/bar_3D_chart.rb +138 -0
  10. data/lib/axlsx/drawing/bar_series.rb +91 -0
  11. data/lib/axlsx/drawing/cat_axis.rb +58 -0
  12. data/lib/axlsx/drawing/chart.rb +120 -0
  13. data/lib/axlsx/drawing/drawing.rb +121 -0
  14. data/lib/axlsx/drawing/graphic_frame.rb +55 -0
  15. data/lib/axlsx/drawing/marker.rb +57 -0
  16. data/lib/axlsx/drawing/pie_3D_chart.rb +59 -0
  17. data/lib/axlsx/drawing/pie_series.rb +88 -0
  18. data/lib/axlsx/drawing/scaling.rb +53 -0
  19. data/lib/axlsx/drawing/series.rb +70 -0
  20. data/lib/axlsx/drawing/title.rb +69 -0
  21. data/lib/axlsx/drawing/two_cell_anchor.rb +88 -0
  22. data/lib/axlsx/drawing/val_axis.rb +34 -0
  23. data/lib/axlsx/drawing/view_3D.rb +72 -0
  24. data/lib/axlsx/package.rb +181 -0
  25. data/lib/axlsx/rels/relationship.rb +43 -0
  26. data/lib/axlsx/rels/relationships.rb +25 -0
  27. data/lib/axlsx/stylesheet/border.rb +52 -0
  28. data/lib/axlsx/stylesheet/border_pr.rb +65 -0
  29. data/lib/axlsx/stylesheet/cell_alignment.rb +96 -0
  30. data/lib/axlsx/stylesheet/cell_protection.rb +33 -0
  31. data/lib/axlsx/stylesheet/cell_style.rb +60 -0
  32. data/lib/axlsx/stylesheet/color.rb +57 -0
  33. data/lib/axlsx/stylesheet/fill.rb +31 -0
  34. data/lib/axlsx/stylesheet/font.rb +128 -0
  35. data/lib/axlsx/stylesheet/gradient_fill.rb +70 -0
  36. data/lib/axlsx/stylesheet/gradient_stop.rb +31 -0
  37. data/lib/axlsx/stylesheet/num_fmt.rb +61 -0
  38. data/lib/axlsx/stylesheet/pattern_fill.rb +64 -0
  39. data/lib/axlsx/stylesheet/styles.rb +296 -0
  40. data/lib/axlsx/stylesheet/table_style.rb +44 -0
  41. data/lib/axlsx/stylesheet/table_style_element.rb +66 -0
  42. data/lib/axlsx/stylesheet/table_styles.rb +39 -0
  43. data/lib/axlsx/stylesheet/xf.rb +117 -0
  44. data/lib/axlsx/util/constants.rb +189 -0
  45. data/lib/axlsx/util/simple_typed_list.rb +150 -0
  46. data/lib/axlsx/util/validators.rb +132 -0
  47. data/lib/axlsx/workbook/workbook.rb +130 -0
  48. data/lib/axlsx/workbook/worksheet/cell.rb +184 -0
  49. data/lib/axlsx/workbook/worksheet/row.rb +92 -0
  50. data/lib/axlsx/workbook/worksheet/worksheet.rb +194 -0
  51. data/lib/schema/dc.xsd +118 -0
  52. data/lib/schema/dcmitype.xsd +50 -0
  53. data/lib/schema/dcterms.xsd +331 -0
  54. data/lib/schema/dml-chart.xsd +1499 -0
  55. data/lib/schema/dml-chartDrawing.xsd +146 -0
  56. data/lib/schema/dml-compatibility.xsd +14 -0
  57. data/lib/schema/dml-diagram.xsd +1091 -0
  58. data/lib/schema/dml-lockedCanvas.xsd +11 -0
  59. data/lib/schema/dml-main.xsd +3048 -0
  60. data/lib/schema/dml-picture.xsd +23 -0
  61. data/lib/schema/dml-spreadsheetDrawing.xsd +185 -0
  62. data/lib/schema/dml-wordprocessingDrawing.xsd +185 -0
  63. data/lib/schema/opc-contentTypes.xsd +42 -0
  64. data/lib/schema/opc-coreProperties.xsd +50 -0
  65. data/lib/schema/opc-digSig.xsd +49 -0
  66. data/lib/schema/opc-relationships.xsd +33 -0
  67. data/lib/schema/pml.xsd +1676 -0
  68. data/lib/schema/shared-additionalCharacteristics.xsd +28 -0
  69. data/lib/schema/shared-bibliography.xsd +144 -0
  70. data/lib/schema/shared-commonSimpleTypes.xsd +166 -0
  71. data/lib/schema/shared-customXmlDataProperties.xsd +25 -0
  72. data/lib/schema/shared-customXmlSchemaProperties.xsd +18 -0
  73. data/lib/schema/shared-documentPropertiesCustom.xsd +59 -0
  74. data/lib/schema/shared-documentPropertiesExtended.xsd +56 -0
  75. data/lib/schema/shared-documentPropertiesVariantTypes.xsd +195 -0
  76. data/lib/schema/shared-math.xsd +582 -0
  77. data/lib/schema/shared-relationshipReference.xsd +25 -0
  78. data/lib/schema/sml.xsd +4430 -0
  79. data/lib/schema/vml-main.xsd +569 -0
  80. data/lib/schema/vml-officeDrawing.xsd +509 -0
  81. data/lib/schema/vml-presentationDrawing.xsd +12 -0
  82. data/lib/schema/vml-spreadsheetDrawing.xsd +108 -0
  83. data/lib/schema/vml-wordprocessingDrawing.xsd +96 -0
  84. data/lib/schema/wml.xsd +3644 -0
  85. data/lib/schema/xml.xsd +117 -0
  86. data/test/content_type/tc_content_type.rb +81 -0
  87. data/test/content_type/tc_content_type.rb~ +81 -0
  88. data/test/content_type/tc_default.rb +40 -0
  89. data/test/content_type/tc_default.rb~ +40 -0
  90. data/test/content_type/tc_override.rb +40 -0
  91. data/test/content_type/tc_override.rb~ +40 -0
  92. data/test/doc_props/tc_app.rb +19 -0
  93. data/test/doc_props/tc_app.rb~ +19 -0
  94. data/test/doc_props/tc_core.rb +34 -0
  95. data/test/drawing/tc_axis.rb +39 -0
  96. data/test/drawing/tc_axis.rb~ +0 -0
  97. data/test/drawing/tc_bar_3D_chart.rb +66 -0
  98. data/test/drawing/tc_bar_3D_chart.rb~ +4 -0
  99. data/test/drawing/tc_bar_series.rb +34 -0
  100. data/test/drawing/tc_bar_series.rb~ +31 -0
  101. data/test/drawing/tc_cat_axis.rb +32 -0
  102. data/test/drawing/tc_cat_axis.rb~ +39 -0
  103. data/test/drawing/tc_chart.rb +59 -0
  104. data/test/drawing/tc_chart.rb~ +58 -0
  105. data/test/drawing/tc_drawing.rb +71 -0
  106. data/test/drawing/tc_graphic_frame.rb +26 -0
  107. data/test/drawing/tc_graphic_frame.rb~ +21 -0
  108. data/test/drawing/tc_marker.rb +45 -0
  109. data/test/drawing/tc_marker.rb~ +26 -0
  110. data/test/drawing/tc_pie_3D_chart.rb +33 -0
  111. data/test/drawing/tc_pie_3D_chart.rb~ +58 -0
  112. data/test/drawing/tc_pie_series.rb +35 -0
  113. data/test/drawing/tc_pie_series.rb~ +26 -0
  114. data/test/drawing/tc_scaling.rb +37 -0
  115. data/test/drawing/tc_scaling.rb~ +45 -0
  116. data/test/drawing/tc_series.rb +24 -0
  117. data/test/drawing/tc_series.rb~ +31 -0
  118. data/test/drawing/tc_title.rb +34 -0
  119. data/test/drawing/tc_title.rb~ +37 -0
  120. data/test/drawing/tc_two_cell_anchor.rb +37 -0
  121. data/test/drawing/tc_two_cell_anchor.rb~ +35 -0
  122. data/test/drawing/tc_val_axis.rb +20 -0
  123. data/test/drawing/tc_val_axis.rb~ +32 -0
  124. data/test/drawing/tc_view_3D.rb +46 -0
  125. data/test/drawing/tc_view_3D.rb~ +37 -0
  126. data/test/rels/tc_relationship.rb +16 -0
  127. data/test/rels/tc_relationship.rb~ +39 -0
  128. data/test/rels/tc_relationships.rb +32 -0
  129. data/test/rels/tc_relationships.rb~ +37 -0
  130. data/test/stylesheet/tc_border.rb +38 -0
  131. data/test/stylesheet/tc_border.rb~ +31 -0
  132. data/test/stylesheet/tc_border_pr.rb +33 -0
  133. data/test/stylesheet/tc_border_pr.rb~ +31 -0
  134. data/test/stylesheet/tc_cell_alignment.rb +77 -0
  135. data/test/stylesheet/tc_cell_alignment.rb~ +38 -0
  136. data/test/stylesheet/tc_cell_protection.rb +30 -0
  137. data/test/stylesheet/tc_cell_protection.rb~ +77 -0
  138. data/test/stylesheet/tc_cell_style.rb +58 -0
  139. data/test/stylesheet/tc_cell_style.rb~ +30 -0
  140. data/test/stylesheet/tc_color.rb +38 -0
  141. data/test/stylesheet/tc_color.rb~ +38 -0
  142. data/test/stylesheet/tc_fill.rb +19 -0
  143. data/test/stylesheet/tc_fill.rb~ +19 -0
  144. data/test/stylesheet/tc_font.rb +114 -0
  145. data/test/stylesheet/tc_font.rb~ +19 -0
  146. data/test/stylesheet/tc_gradient_fill.rb +65 -0
  147. data/test/stylesheet/tc_gradient_fill.rb~ +114 -0
  148. data/test/stylesheet/tc_gradient_stop.rb +32 -0
  149. data/test/stylesheet/tc_gradient_stop.rb~ +65 -0
  150. data/test/stylesheet/tc_num_fmt.rb +31 -0
  151. data/test/stylesheet/tc_num_fmt.rb~ +32 -0
  152. data/test/stylesheet/tc_pattern_fill.rb +38 -0
  153. data/test/stylesheet/tc_pattern_fill.rb~ +31 -0
  154. data/test/stylesheet/tc_styles.rb +64 -0
  155. data/test/stylesheet/tc_table_style.rb +37 -0
  156. data/test/stylesheet/tc_table_style.rb~ +38 -0
  157. data/test/stylesheet/tc_table_style_element.rb +37 -0
  158. data/test/stylesheet/tc_table_style_element.rb~ +37 -0
  159. data/test/stylesheet/tc_table_styles.rb +30 -0
  160. data/test/stylesheet/tc_table_styles.rb~ +37 -0
  161. data/test/stylesheet/tc_xf.rb +121 -0
  162. data/test/stylesheet/tc_xf.rb~ +30 -0
  163. data/test/tc_app.rb~ +19 -0
  164. data/test/tc_border_pr.rb~ +21 -0
  165. data/test/tc_package.rb +70 -0
  166. data/test/tc_package.rb~ +64 -0
  167. data/test/tc_pie_3D_chart.rb~ +66 -0
  168. data/test/tc_relationships.rb~ +37 -0
  169. data/test/tc_series.rb~ +31 -0
  170. data/test/tc_styles.rb~ +64 -0
  171. data/test/tc_validators.rb~ +77 -0
  172. data/test/tc_worksheet.rb~ +85 -0
  173. data/test/util/tc_simple_typed_list.rb +66 -0
  174. data/test/util/tc_validators.rb +76 -0
  175. data/test/workbook/tc_workbook.rb +53 -0
  176. data/test/workbook/worksheet/tc_cell.rb +78 -0
  177. data/test/workbook/worksheet/tc_row.rb +30 -0
  178. data/test/workbook/worksheet/tc_worksheet.rb +85 -0
  179. metadata +378 -0
@@ -0,0 +1,85 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestWorksheet < Test::Unit::TestCase
5
+ def setup
6
+ p = Axlsx::Package.new
7
+ @ws = p.workbook.add_worksheet
8
+ end
9
+
10
+ def test_pn
11
+ assert_equal(@ws.pn, "worksheets/sheet1.xml")
12
+ ws = @ws.workbook.add_worksheet
13
+ assert_equal(ws.pn, "worksheets/sheet2.xml")
14
+ end
15
+
16
+ def test_rels_pn
17
+ assert_equal(@ws.rels_pn, "worksheets/_rels/sheet1.xml.rels")
18
+ ws = @ws.workbook.add_worksheet
19
+ assert_equal(ws.rels_pn, "worksheets/_rels/sheet2.xml.rels")
20
+ end
21
+
22
+ def test_rId
23
+ assert_equal(@ws.rId, "rId1")
24
+ ws = @ws.workbook.add_worksheet
25
+ assert_equal(ws.rId, "rId2")
26
+ end
27
+
28
+ def test_index
29
+ assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws))
30
+ end
31
+
32
+ def test_add_row
33
+ assert(@ws.rows.empty?, "sheet has no rows by default")
34
+ r = @ws.add_row([1,2,3])
35
+ assert_equal(@ws.rows.size, 1, "add_row adds a row")
36
+ assert_equal(@ws.rows.first, r, "the row returned is the row added")
37
+ end
38
+
39
+ def test_add_chart
40
+ assert(@ws.workbook.charts.empty?, "the sheet's workbook should not have any charts by default")
41
+ @ws.add_chart Axlsx::Pie3DChart
42
+ assert_equal(@ws.workbook.charts.size, 1, "add_chart adds a chart to the workbook")
43
+ end
44
+
45
+ def test_drawing
46
+ assert @ws.drawing.is_a? Axlsx::Drawing
47
+ end
48
+
49
+
50
+ def test_to_xml
51
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
52
+ doc = Nokogiri::XML(@ws.to_xml)
53
+ errors = []
54
+ schema.validate(doc).each do |error|
55
+ errors.push error
56
+ puts error.message
57
+ end
58
+ assert(errors.empty?, "error free validation")
59
+ end
60
+
61
+ def test_relationships
62
+ assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
63
+ c = @ws.add_chart Axlsx::Pie3DChart
64
+ assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship")
65
+ c = @ws.add_chart Axlsx::Pie3DChart
66
+ assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship")
67
+ end
68
+
69
+
70
+ def test_update_auto_with_data
71
+ small = @ws.workbook.styles.add_style(:sz=>2)
72
+ big = @ws.workbook.styles.add_style(:sz=>10)
73
+ @ws.add_row ["chasing windmills", "penut"], :style=>small
74
+ assert(@ws.auto_fit_data.size == 2, "a data item for each column")
75
+ assert_equal(@ws.auto_fit_data[0], {:sz=>2,:longest=>"chasing windmills"}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column")
76
+ @ws.add_row ["mule"], :style=>big
77
+ assert_equal(@ws.auto_fit_data[0], {:sz=>10,:longest=>"mule"}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column")
78
+ end
79
+
80
+ def test_auto_width
81
+ 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")
82
+ assert(@ws.send(:auto_width, {:sz=>12, :longest=>"fish"}) > @ws.send(:auto_width, {:sz=>11, :longest=>"fish"}), "larger font size gets a longer auto_width using the same text")
83
+ end
84
+
85
+ end
@@ -0,0 +1,66 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+ class TestSimpleTypedList < Test::Unit::TestCase
4
+ def setup
5
+ @list = Axlsx::SimpleTypedList.new Fixnum
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ def test_type_is_a_class_or_array_of_class
12
+ assert_nothing_raised { Axlsx::SimpleTypedList.new Integer }
13
+ assert_nothing_raised { Axlsx::SimpleTypedList.new [Integer,String] }
14
+ assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new }
15
+ assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new "1" }
16
+ assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new [Integer, "Class"] }
17
+ end
18
+
19
+ def test_indexed_based_assignment
20
+ #should not allow nil assignment
21
+ assert_raise(ArgumentError) { @list[0] = nil }
22
+ assert_raise(ArgumentError) { @list[0] = "1" }
23
+ assert_nothing_raised { @list[0] = 1 }
24
+ end
25
+
26
+ def test_concat_assignment
27
+ assert_raise(ArgumentError) { @list << nil }
28
+ assert_raise(ArgumentError) { @list << "1" }
29
+ assert_nothing_raised { @list << 1 }
30
+ end
31
+
32
+ def test_concat_should_return_index
33
+ assert( @list.size == 0 )
34
+ assert( @list << 1 == 0 )
35
+ assert( @list << 2 == 1 )
36
+ @list.delete_at 0
37
+ assert( @list << 3 == 1 )
38
+ assert( @list.index(2) == 0 )
39
+ end
40
+
41
+ def test_push_should_return_index
42
+ assert( @list.push(1) == 0 )
43
+ assert( @list.push(2) == 1 )
44
+ @list.delete_at 0
45
+ assert( @list.push(3) == 1 )
46
+ assert( @list.index(2) == 0 )
47
+ end
48
+
49
+ def test_locking
50
+ @list.push 1
51
+ @list.push 2
52
+ @list.push 3
53
+ @list.lock
54
+
55
+ assert_raise(ArgumentError) { @list.delete 1 }
56
+ assert_raise(ArgumentError) { @list.delete_at 1 }
57
+ assert_raise(ArgumentError) { @list.delete_at 2 }
58
+ @list.push 4
59
+ assert_nothing_raised { @list.delete_at 3 }
60
+ @list.unlock
61
+ #ignore garbage
62
+ assert_nothing_raised { @list.delete 0 }
63
+ assert_nothing_raised { @list.delete 9 }
64
+ end
65
+
66
+ end
@@ -0,0 +1,76 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+ class TestValidators < Test::Unit::TestCase
4
+ def setup
5
+ end
6
+ def teardown
7
+ end
8
+
9
+ def test_validators
10
+ #unsigned_int
11
+ assert_nothing_raised { Axlsx.validate_unsigned_int 1 }
12
+ assert_nothing_raised { Axlsx.validate_unsigned_int +1 }
13
+ assert_raise(ArgumentError) { Axlsx.validate_unsigned_int -1 }
14
+ assert_raise(ArgumentError) { Axlsx.validate_unsigned_int '1' }
15
+
16
+ #int
17
+ assert_nothing_raised { Axlsx.validate_int 1 }
18
+ assert_nothing_raised { Axlsx.validate_int -1 }
19
+ assert_raise(ArgumentError) { Axlsx.validate_int 'a' }
20
+ assert_raise(ArgumentError) { Axlsx.validate_int Array }
21
+
22
+ #boolean (as 0 or 1, :true, :false, true, false, or "true," "false")
23
+ [0,1,:true, :false, true, false, "true", "false"].each do |v|
24
+ assert_nothing_raised { Axlsx.validate_boolean 0 }
25
+ end
26
+ assert_raise(ArgumentError) { Axlsx.validate_boolean 2 }
27
+
28
+ #string
29
+ assert_nothing_raised { Axlsx.validate_string "1" }
30
+ assert_raise(ArgumentError) { Axlsx.validate_string 2 }
31
+ assert_raise(ArgumentError) { Axlsx.validate_string false }
32
+
33
+ #float
34
+ assert_nothing_raised { Axlsx.validate_float 1.0 }
35
+ assert_raise(ArgumentError) { Axlsx.validate_float 2 }
36
+ assert_raise(ArgumentError) { Axlsx.validate_float false }
37
+
38
+ #pattern_type
39
+ assert_nothing_raised { Axlsx.validate_pattern_type :none }
40
+ assert_raise(ArgumentError) { Axlsx.validate_pattern_type "none" }
41
+ assert_raise(ArgumentError) { Axlsx.validate_pattern_type "crazy_pattern" }
42
+ assert_raise(ArgumentError) { Axlsx.validate_pattern_type false }
43
+
44
+ #gradient_type
45
+ assert_nothing_raised { Axlsx.validate_gradient_type :path }
46
+ assert_raise(ArgumentError) { Axlsx.validate_gradient_type nil }
47
+ assert_raise(ArgumentError) { Axlsx.validate_gradient_type "fractal" }
48
+ assert_raise(ArgumentError) { Axlsx.validate_gradient_type false }
49
+
50
+ #horizontal alignment
51
+ assert_nothing_raised { Axlsx.validate_horizontal_alignment :general }
52
+ assert_raise(ArgumentError) { Axlsx.validate_horizontal_alignment nil }
53
+ assert_raise(ArgumentError) { Axlsx.validate_horizontal_alignment "wavy" }
54
+ assert_raise(ArgumentError) { Axlsx.validate_horizontal_alignment false }
55
+
56
+ #vertical alignment
57
+ assert_nothing_raised { Axlsx.validate_vertical_alignment :top }
58
+ assert_raise(ArgumentError) { Axlsx.validate_vertical_alignment nil }
59
+ assert_raise(ArgumentError) { Axlsx.validate_vertical_alignment "dynamic" }
60
+ assert_raise(ArgumentError) { Axlsx.validate_vertical_alignment false }
61
+
62
+ #contentType
63
+ assert_nothing_raised { Axlsx.validate_content_type Axlsx::WORKBOOK_CT }
64
+ assert_raise(ArgumentError) { Axlsx.validate_content_type nil }
65
+ assert_raise(ArgumentError) { Axlsx.validate_content_type "http://some.url" }
66
+ assert_raise(ArgumentError) { Axlsx.validate_content_type false }
67
+
68
+ #relationshipType
69
+ assert_nothing_raised { Axlsx.validate_relationship_type Axlsx::WORKBOOK_R }
70
+ assert_raise(ArgumentError) { Axlsx.validate_relationship_type nil }
71
+ assert_raise(ArgumentError) { Axlsx.validate_relationship_type "http://some.url" }
72
+ assert_raise(ArgumentError) { Axlsx.validate_relationship_type false }
73
+
74
+ end
75
+ end
76
+
@@ -0,0 +1,53 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestWorkbook < Test::Unit::TestCase
5
+ def setup
6
+ p = Axlsx::Package.new
7
+ @wb = p.workbook
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ def test_date1904
14
+ assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
15
+ @wb.date1904 = :false
16
+ assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
17
+ Axlsx::Workbook.date1904 = :true
18
+ assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
19
+ end
20
+
21
+ def test_add_worksheet
22
+ assert(@wb.worksheets.empty?, "worbook has no worksheets by default")
23
+ ws = @wb.add_worksheet(:name=>"bob")
24
+ assert_equal(@wb.worksheets.size, 1, "add_worksheet adds a worksheet!")
25
+ assert_equal(@wb.worksheets.first, ws, "the worksheet returned is the worksheet added")
26
+ assert_equal(ws.name, "bob", "name option gets passed to worksheet")
27
+ end
28
+ def test_relationships
29
+ #current relationship size is 1 due to style relation
30
+ assert(@wb.relationships.size == 1)
31
+ @wb.add_worksheet
32
+ assert(@wb.relationships.size == 2)
33
+ end
34
+
35
+ def test_to_xml
36
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
37
+ doc = Nokogiri::XML(@wb.to_xml)
38
+ errors = []
39
+ schema.validate(doc).each do |error|
40
+ errors.push error
41
+ puts error.message
42
+ end
43
+ assert(errors.empty?, "error free validation")
44
+ end
45
+
46
+ def test_to_xml_adds_worksheet_when_worksheets_is_empty
47
+ assert(@wb.worksheets.empty?)
48
+ @wb.to_xml
49
+ assert(@wb.worksheets.size == 1)
50
+ end
51
+
52
+
53
+ end
@@ -0,0 +1,78 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestCell < Test::Unit::TestCase
5
+
6
+ def setup
7
+ p = Axlsx::Package.new
8
+ @ws = p.workbook.add_worksheet :name=>"hmmm"
9
+ p.workbook.styles.add_style :sz=>20
10
+ @row = @ws.add_row
11
+ @c = @row.add_cell 1, :type=>:float, :style=>1
12
+ end
13
+
14
+ def test_initialize
15
+ assert_equal(@row.cells.last, @c, "the cell was added to the row")
16
+ assert_equal(@c.type, :float, "type option is applied")
17
+ assert_equal(@c.style, 1, "style option is applied")
18
+ assert_equal(@c.value, 1.0, "type option is applied and value is casted")
19
+ end
20
+
21
+ def test_index
22
+ assert_equal(@c.index, @row.cells.index(@c))
23
+ end
24
+
25
+ def test_r
26
+ assert_equal(@c.r, "A1", "calculate cell reference")
27
+ end
28
+
29
+ def test_r_abs
30
+ assert_equal(@c.r_abs,"$A$1", "calculate absolute cell reference")
31
+ end
32
+
33
+ def test_style
34
+ assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style=3 }
35
+ assert_nothing_raised("must allow valid style index changes") {@c.style=1}
36
+ assert_equal(@c.style, 1)
37
+ end
38
+
39
+ def test_type
40
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
41
+ assert_nothing_raised("type can be changed") { @c.type = :string }
42
+ assert_equal(@c.value, "1.0", "changing type casts the value")
43
+
44
+ assert_equal(@row.add_cell(Time.now).type, :time, 'time should be time')
45
+ end
46
+
47
+ def test_value
48
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
49
+ assert_nothing_raised("type can be changed") { @c.type = :string }
50
+ assert_equal(@c.value, "1.0", "changing type casts the value")
51
+ end
52
+
53
+ def test_col_ref
54
+ assert_equal(@c.send(:col_ref), "A")
55
+ end
56
+
57
+ def test_cell_type_from_value
58
+ assert_equal(@c.send(:cell_type_from_value, 1.0), :float)
59
+ assert_equal(@c.send(:cell_type_from_value, 1), :integer)
60
+ assert_equal(@c.send(:cell_type_from_value, Time.now), :time)
61
+ assert_equal(@c.send(:cell_type_from_value, []), :string)
62
+ assert_equal(@c.send(:cell_type_from_value, "d"), :string)
63
+ assert_equal(@c.send(:cell_type_from_value, nil), :string)
64
+ assert_equal(@c.send(:cell_type_from_value, -1), :integer)
65
+ end
66
+
67
+ def test_cast_value
68
+ @c.type = :string
69
+ assert_equal(@c.send(:cast_value, 1.0), "1.0")
70
+ @c.type = :integer
71
+ assert_equal(@c.send(:cast_value, 1.0), 1)
72
+ @c.type = :float
73
+ assert_equal(@c.send(:cast_value, "1.0"), 1.0)
74
+ @c.type = :string
75
+ assert_equal(@c.send(:cast_value, nil), "")
76
+
77
+ end
78
+ end
@@ -0,0 +1,30 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestRow < Test::Unit::TestCase
5
+
6
+ def setup
7
+ p = Axlsx::Package.new
8
+ @ws = p.workbook.add_worksheet :name=>"hmmm"
9
+ @row = @ws.add_row
10
+ end
11
+
12
+ def test_initialize
13
+ assert(@row.cells.empty?, "no cells by default")
14
+ assert_equal(@row.worksheet, @ws, "has a reference to the worksheet")
15
+ end
16
+
17
+ def test_index
18
+ assert_equal(@row.index, @row.worksheet.rows.index(@row))
19
+ end
20
+
21
+ def test_add_cell
22
+ c = @row.add_cell(1)
23
+ assert_equal(@row.cells.last, c)
24
+ end
25
+
26
+ def test_array_to_cells
27
+ r = @ws.add_row :values=>[1,2,3], :style=>0, :types=>:integer
28
+ assert_equal(r.cells.size, 3)
29
+ end
30
+ end
@@ -0,0 +1,85 @@
1
+ require 'test/unit'
2
+ require 'axlsx.rb'
3
+
4
+ class TestWorksheet < Test::Unit::TestCase
5
+ def setup
6
+ p = Axlsx::Package.new
7
+ @ws = p.workbook.add_worksheet
8
+ end
9
+
10
+ def test_pn
11
+ assert_equal(@ws.pn, "worksheets/sheet1.xml")
12
+ ws = @ws.workbook.add_worksheet
13
+ assert_equal(ws.pn, "worksheets/sheet2.xml")
14
+ end
15
+
16
+ def test_rels_pn
17
+ assert_equal(@ws.rels_pn, "worksheets/_rels/sheet1.xml.rels")
18
+ ws = @ws.workbook.add_worksheet
19
+ assert_equal(ws.rels_pn, "worksheets/_rels/sheet2.xml.rels")
20
+ end
21
+
22
+ def test_rId
23
+ assert_equal(@ws.rId, "rId1")
24
+ ws = @ws.workbook.add_worksheet
25
+ assert_equal(ws.rId, "rId2")
26
+ end
27
+
28
+ def test_index
29
+ assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws))
30
+ end
31
+
32
+ def test_add_row
33
+ assert(@ws.rows.empty?, "sheet has no rows by default")
34
+ r = @ws.add_row(:values=>[1,2,3])
35
+ assert_equal(@ws.rows.size, 1, "add_row adds a row")
36
+ assert_equal(@ws.rows.first, r, "the row returned is the row added")
37
+ end
38
+
39
+ def test_add_chart
40
+ assert(@ws.workbook.charts.empty?, "the sheet's workbook should not have any charts by default")
41
+ @ws.add_chart Axlsx::Pie3DChart
42
+ assert_equal(@ws.workbook.charts.size, 1, "add_chart adds a chart to the workbook")
43
+ end
44
+
45
+ def test_drawing
46
+ assert @ws.drawing.is_a? Axlsx::Drawing
47
+ end
48
+
49
+
50
+ def test_to_xml
51
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
52
+ doc = Nokogiri::XML(@ws.to_xml)
53
+ errors = []
54
+ schema.validate(doc).each do |error|
55
+ errors.push error
56
+ puts error.message
57
+ end
58
+ assert(errors.empty?, "error free validation")
59
+ end
60
+
61
+ def test_relationships
62
+ assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
63
+ c = @ws.add_chart Axlsx::Pie3DChart
64
+ assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship")
65
+ c = @ws.add_chart Axlsx::Pie3DChart
66
+ assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship")
67
+ end
68
+
69
+
70
+ def test_update_auto_with_data
71
+ small = @ws.workbook.styles.add_style(:sz=>2)
72
+ big = @ws.workbook.styles.add_style(:sz=>10)
73
+ @ws.add_row :values=>["chasing windmills", "penut"], :style=>small
74
+ assert(@ws.auto_fit_data.size == 2, "a data item for each column")
75
+ assert_equal(@ws.auto_fit_data[0], {:sz=>2,:longest=>"chasing windmills"}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column")
76
+ @ws.add_row :values=> ["mule"], :style=>big
77
+ assert_equal(@ws.auto_fit_data[0], {:sz=>10,:longest=>"mule"}, "adding a row updates auto_fit_data if the product of the string length and font is greater for the column")
78
+ end
79
+
80
+ def test_auto_width
81
+ 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")
82
+ assert(@ws.send(:auto_width, {:sz=>12, :longest=>"fish"}) > @ws.send(:auto_width, {:sz=>11, :longest=>"fish"}), "larger font size gets a longer auto_width using the same text")
83
+ end
84
+
85
+ end