workbook 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +21 -0
  3. data/.gitignore +4 -1
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +4 -4
  6. data/CHANGELOG.md +7 -0
  7. data/Gemfile +2 -2
  8. data/README.md +9 -7
  9. data/Rakefile +6 -6
  10. data/lib/workbook/book.rb +73 -62
  11. data/lib/workbook/cell.rb +58 -13
  12. data/lib/workbook/column.rb +31 -28
  13. data/lib/workbook/format.rb +23 -24
  14. data/lib/workbook/generatetypes.rb +4 -4
  15. data/lib/workbook/modules/cache.rb +6 -7
  16. data/lib/workbook/modules/cell.rb +77 -100
  17. data/lib/workbook/modules/diff_sort.rb +92 -83
  18. data/lib/workbook/modules/raw_objects_storage.rb +6 -8
  19. data/lib/workbook/modules/type_parser.rb +30 -22
  20. data/lib/workbook/nil_value.rb +4 -9
  21. data/lib/workbook/readers/csv_reader.rb +7 -10
  22. data/lib/workbook/readers/ods_reader.rb +48 -50
  23. data/lib/workbook/readers/txt_reader.rb +6 -8
  24. data/lib/workbook/readers/xls_reader.rb +21 -33
  25. data/lib/workbook/readers/xls_shared.rb +106 -117
  26. data/lib/workbook/readers/xlsx_reader.rb +45 -46
  27. data/lib/workbook/row.rb +99 -84
  28. data/lib/workbook/sheet.rb +47 -38
  29. data/lib/workbook/table.rb +96 -72
  30. data/lib/workbook/template.rb +12 -15
  31. data/lib/workbook/types/false.rb +0 -1
  32. data/lib/workbook/types/nil.rb +0 -1
  33. data/lib/workbook/types/nil_class.rb +1 -1
  34. data/lib/workbook/types/numeric.rb +1 -1
  35. data/lib/workbook/types/string.rb +1 -1
  36. data/lib/workbook/types/time.rb +1 -1
  37. data/lib/workbook/types/true.rb +0 -1
  38. data/lib/workbook/types/true_class.rb +1 -1
  39. data/lib/workbook/version.rb +2 -3
  40. data/lib/workbook/writers/csv_table_writer.rb +10 -13
  41. data/lib/workbook/writers/html_writer.rb +34 -38
  42. data/lib/workbook/writers/json_table_writer.rb +8 -11
  43. data/lib/workbook/writers/xls_writer.rb +30 -36
  44. data/lib/workbook/writers/xlsx_writer.rb +45 -29
  45. data/lib/workbook.rb +16 -15
  46. data/test/helper.rb +6 -5
  47. data/test/test_book.rb +41 -38
  48. data/test/test_column.rb +26 -24
  49. data/test/test_format.rb +51 -55
  50. data/test/test_functional.rb +7 -8
  51. data/test/test_modules_cache.rb +18 -17
  52. data/test/test_modules_cell.rb +47 -46
  53. data/test/test_modules_table_diff_sort.rb +55 -64
  54. data/test/test_modules_type_parser.rb +61 -31
  55. data/test/test_readers_csv_reader.rb +48 -42
  56. data/test/test_readers_ods_reader.rb +28 -31
  57. data/test/test_readers_txt_reader.rb +21 -23
  58. data/test/test_readers_xls_reader.rb +20 -23
  59. data/test/test_readers_xls_shared.rb +2 -3
  60. data/test/test_readers_xlsx_reader.rb +44 -37
  61. data/test/test_row.rb +105 -109
  62. data/test/test_sheet.rb +41 -35
  63. data/test/test_table.rb +82 -60
  64. data/test/test_template.rb +16 -15
  65. data/test/test_types_date.rb +4 -6
  66. data/test/test_writers_csv_writer.rb +24 -0
  67. data/test/test_writers_html_writer.rb +42 -41
  68. data/test/test_writers_json_writer.rb +9 -9
  69. data/test/test_writers_xls_writer.rb +50 -35
  70. data/test/test_writers_xlsx_writer.rb +62 -34
  71. data/workbook.gemspec +26 -27
  72. metadata +92 -27
@@ -1,83 +1,85 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- encoding : utf-8 -*-
4
- require File.join(File.dirname(__FILE__), 'helper')
3
+ require File.join(File.dirname(__FILE__), "helper")
5
4
 
6
5
  class TestModulesCell < Minitest::Test
7
-
8
-
9
6
  def test_init
10
7
  w = Workbook::Cell.new nil
11
8
  assert_nil(w.value)
12
9
  w = Workbook::Cell.new "asdf"
13
- assert_equal("asdf",w.value)
10
+ assert_equal("asdf", w.value)
14
11
 
15
12
  assert_raises(ArgumentError) { w = Workbook::Cell.new Workbook::Row }
16
13
 
17
14
  t = Time.now
18
15
  w = Workbook::Cell.new t
19
- assert_equal(t,w.value)
20
-
16
+ assert_equal(t, w.value)
21
17
  end
22
18
 
23
19
  def test_value
24
20
  w = Workbook::Cell.new nil
25
21
  assert_nil(w.value)
26
- w.value = "asdf"
27
- assert_equal("asdf",w.value)
28
- w.value = Date.new
29
- assert_equal(Date.new,w.value)
30
- w.value = 1
31
- assert_equal(1,w.value)
32
- assert(["Integer", "Fixnum"].include?(w.value.class.to_s))
33
- w.value = 1.0
34
- assert_equal(1.0,w.value)
35
- assert_equal(Float,w.value.class)
22
+
23
+ {"asdf" => :string, Date.new => :date, 1 => :integer, 1.0 => :float, BigDecimal("1.0") => :decimal}.each do |value, value_type|
24
+ w.value = value
25
+ assert_equal(value, w.value)
26
+ assert_equal(value_type, w.cell_type)
27
+ end
28
+ # w.value = "asdf"
29
+ # assert_equal("asdf", w.value)
30
+ # w.value = Date.new
31
+ # assert_equal(Date.new, w.value)
32
+ # w.value = 1
33
+ # assert_equal(1, w.value)
34
+ # assert(["Integer", "Integer"].include?(w.value.class.to_s))
35
+ # w.value = 1.0
36
+ # assert_equal(1.0, w.value)
37
+ # assert_equal(Float, w.value.class)
36
38
  end
37
39
 
38
40
  def test_importance_of_class
39
41
  a = Workbook::Cell.new
40
- assert_equal(4, a.importance_of_class("a"))
41
- assert_equal(5, a.importance_of_class(:a))
42
+ assert_equal(5, a.importance_of_class("a"))
43
+ assert_equal(6, a.importance_of_class(:a))
42
44
  end
43
45
 
44
46
  def test_comp
45
47
  a = Workbook::Cell.new 1
46
48
  b = Workbook::Cell.new 2
47
- assert_equal(-1, a<=>b)
49
+ assert_equal(-1, a <=> b)
48
50
  a = Workbook::Cell.new "c"
49
51
  b = Workbook::Cell.new "bsdf"
50
- assert_equal(1, a<=>b)
52
+ assert_equal(1, a <=> b)
51
53
  a = Workbook::Cell.new "c"
52
54
  b = Workbook::Cell.new "c"
53
- assert_equal(0, a<=>b)
55
+ assert_equal(0, a <=> b)
54
56
  a = Workbook::Cell.new true
55
57
  b = Workbook::Cell.new false
56
- assert_equal(-1, a<=>b)
58
+ assert_equal(-1, a <=> b)
57
59
  a = Workbook::Cell.new "true"
58
60
  b = Workbook::Cell.new "false"
59
- assert_equal(1, a<=>b)
61
+ assert_equal(1, a <=> b)
60
62
  a = Workbook::Cell.new 1
61
63
  b = Workbook::Cell.new "a"
62
- assert_equal(-1, a<=>b)
64
+ assert_equal(-1, a <=> b)
63
65
  a = Workbook::Cell.new nil
64
66
  b = Workbook::Cell.new "a"
65
- assert_equal(1, a<=>b)
67
+ assert_equal(1, a <=> b)
66
68
  a = Workbook::Cell.new nil
67
69
  b = Workbook::Cell.new nil
68
- assert_equal(0, a<=>b)
70
+ assert_equal(0, a <=> b)
69
71
  end
70
72
 
71
73
  def test_cloning_as_expected?
72
74
  a = Workbook::Cell.new 1
73
- a.format = Workbook::Format.new({:value=>1})
75
+ a.format = Workbook::Format.new({value: 1})
74
76
  b = a.clone
75
77
  b.value = 2
76
- b.format[:value]=2
77
- assert_equal(1,a.value)
78
- assert_equal(2,b.value)
79
- assert_equal(2,a.format[:value])
80
- assert_equal(2,b.format[:value])
78
+ b.format[:value] = 2
79
+ assert_equal(1, a.value)
80
+ assert_equal(2, b.value)
81
+ assert_equal(2, a.format[:value])
82
+ assert_equal(2, b.format[:value])
81
83
  end
82
84
 
83
85
  def test_to_sym
@@ -91,15 +93,14 @@ class TestModulesCell < Minitest::Test
91
93
  "éåšžÌ?" => :easzi?,
92
94
  1 => :num1,
93
95
  1.0 => :num1_0,
94
- 'test ' => :test,
95
- 'test ' => :test
96
+ "test " => :test,
97
+ "test " => :test
96
98
 
97
99
  }
98
- examples.each do |k,v|
100
+ examples.each do |k, v|
101
+ assert_equal(v, Workbook::Cell::value_to_sym(k))
99
102
  assert_equal(v, Workbook::Cell.new(k).to_sym)
100
103
  end
101
-
102
-
103
104
  end
104
105
 
105
106
  def test_nil
@@ -119,32 +120,32 @@ class TestModulesCell < Minitest::Test
119
120
  assert_nil(c.rowspan)
120
121
  c.colspan = 3
121
122
  c.rowspan = "4"
122
- assert_equal(3,c.colspan)
123
+ assert_equal(3, c.colspan)
123
124
  c.rowspan = 0
124
125
  assert_nil(c.rowspan)
125
- assert_equal(3,c.colspan)
126
+ assert_equal(3, c.colspan)
126
127
  c.colspan = 0
127
128
  c.rowspan = 3
128
- assert_equal(3,c.rowspan)
129
+ assert_equal(3, c.rowspan)
129
130
  assert_nil(c.colspan)
130
131
  end
131
132
 
132
133
  def test_cell_type
133
- {1 => :integer, 3.2 => :float, true => :boolean, "asdf" => :string}.each do |k,v|
134
+ {1 => :integer, 3.2 => :float, true => :boolean, "asdf" => :string}.each do |k, v|
134
135
  c = Workbook::Cell.new(k)
135
- assert_equal(v,c.cell_type)
136
+ assert_equal(v, c.cell_type)
136
137
  end
137
138
  end
138
139
 
139
140
  def test_index
140
- t = Workbook::Table.new [[:a,:b,:c],[1,2,3],[4,5,6]]
141
+ t = Workbook::Table.new [[:a, :b, :c], [1, 2, 3], [4, 5, 6]]
141
142
  assert_equal(2, t[2][2].index)
142
143
  end
143
144
 
144
145
  def test_key
145
- t = Workbook::Table.new [[:a,:b,:c],[1,2,3],[4,5,6]]
146
+ t = Workbook::Table.new [[:a, :b, :c], [1, 2, 3], [4, 5, 6]]
146
147
  assert_equal(:c, t[2][2].key)
147
- t = Workbook::Table.new [[:d,nil,nil],[:a,:b,:c],[1,2,3],[4,5,6]]
148
+ t = Workbook::Table.new [[:d, nil, nil], [:a, :b, :c], [1, 2, 3], [4, 5, 6]]
148
149
  assert_nil(t[2][2].key)
149
150
  assert_equal(:d, t[2][0].key)
150
151
  t.header = t[1]
@@ -1,85 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- encoding : utf-8 -*-
4
- require File.join(File.dirname(__FILE__), 'helper')
3
+ require File.join(File.dirname(__FILE__), "helper")
5
4
  module Modules
6
5
  class TestTableDiffSort < Minitest::Test
7
6
  def test_sort
8
7
  time = Time.now
9
- b = Workbook::Book.new [[1,2,3],[2,2,3],[true,false,true],["asdf","sdf","as"],[time,2,3],[2,2,2],[22,2,3],[1,2,233]]
8
+ b = Workbook::Book.new [[1, 2, 3], [2, 2, 3], [true, false, true], ["asdf", "sdf", "as"], [time, 2, 3], [2, 2, 2], [22, 2, 3], [1, 2, 233]]
10
9
  t = b.sheet.table
11
10
  assert_equal(
12
- [ [Workbook::Cell.new(1),Workbook::Cell.new(2),Workbook::Cell.new(3)],
13
- [Workbook::Cell.new(1),Workbook::Cell.new(2),Workbook::Cell.new(233)],
14
- [Workbook::Cell.new(2),Workbook::Cell.new(2),Workbook::Cell.new(2)],
15
- [Workbook::Cell.new(2),Workbook::Cell.new(2),Workbook::Cell.new(3)],
16
- [Workbook::Cell.new(22),Workbook::Cell.new(2),Workbook::Cell.new(3)],
17
- [Workbook::Cell.new("asdf"),Workbook::Cell.new("sdf"),Workbook::Cell.new("as")],
18
- [Workbook::Cell.new(time),Workbook::Cell.new(2),Workbook::Cell.new(3)],
19
- [Workbook::Cell.new(true),Workbook::Cell.new(false),Workbook::Cell.new(true)]
20
- ],
21
- t.sort)
22
-
23
- ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
11
+ [[Workbook::Cell.new(1), Workbook::Cell.new(2), Workbook::Cell.new(3)],
12
+ [Workbook::Cell.new(1), Workbook::Cell.new(2), Workbook::Cell.new(233)],
13
+ [Workbook::Cell.new(2), Workbook::Cell.new(2), Workbook::Cell.new(2)],
14
+ [Workbook::Cell.new(2), Workbook::Cell.new(2), Workbook::Cell.new(3)],
15
+ [Workbook::Cell.new(22), Workbook::Cell.new(2), Workbook::Cell.new(3)],
16
+ [Workbook::Cell.new("asdf"), Workbook::Cell.new("sdf"), Workbook::Cell.new("as")],
17
+ [Workbook::Cell.new(time), Workbook::Cell.new(2), Workbook::Cell.new(3)],
18
+ [Workbook::Cell.new(true), Workbook::Cell.new(false), Workbook::Cell.new(true)]],
19
+ t.sort.rows.map(&:cells)
20
+ )
21
+
22
+ ba = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [4, 2, 3, 4], [3, 2, 3, 4]]
24
23
  sba = ba.sheet.table.sort
25
- refute_equal(Workbook::Table.new([['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]),sba)
26
- assert_equal(Workbook::Table.new([['a','b','c','d'],[1,2,3,4],[3,2,3,4],[4,2,3,4]]),sba)
27
-
24
+ refute_equal(Workbook::Table.new([["a", "b", "c", "d"], [1, 2, 3, 4], [4, 2, 3, 4], [3, 2, 3, 4]]), sba)
25
+ assert_equal(Workbook::Table.new([["a", "b", "c", "d"], [1, 2, 3, 4], [3, 2, 3, 4], [4, 2, 3, 4]]), sba)
28
26
  end
29
27
 
30
28
  def test_align
31
- ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
32
- bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
33
- tself = ba.sheet.table
34
- tother = bb.sheet.table
35
-
36
- ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
37
- bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
29
+ ba = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [4, 2, 3, 4], [3, 2, 3, 4]]
30
+ bb = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4]]
38
31
  tself = ba.sheet.table
39
32
  tother = bb.sheet.table
40
33
  align_result = tself.align tother
41
- assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
42
- assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n4,2,3,4\n\n",align_result[:self].to_csv)
34
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n\n5,2,3,4\n", align_result[:other].to_csv)
35
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n4,2,3,4\n\n", align_result[:self].to_csv)
43
36
 
44
- ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
45
- bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[2,2,3,4],[5,2,3,4]]
37
+ ba = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4]]
38
+ bb = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [2, 2, 3, 4], [5, 2, 3, 4]]
46
39
  tself = ba.sheet.table
47
40
  tother = bb.sheet.table
48
41
  align_result = tself.align tother
49
- assert_equal("a,b,c,d\n1,2,3,4\n\n3,2,3,4\n5,2,3,4\n",align_result[:self].to_csv)
50
- assert_equal("a,b,c,d\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
42
+ assert_equal("a,b,c,d\n1,2,3,4\n\n3,2,3,4\n5,2,3,4\n", align_result[:self].to_csv)
43
+ assert_equal("a,b,c,d\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n", align_result[:other].to_csv)
51
44
  align_result = tother.align tself
52
- assert_equal("a,b,c,d\n1,2,3,4\n\n3,2,3,4\n5,2,3,4\n",align_result[:other].to_csv)
53
- assert_equal("a,b,c,d\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n",align_result[:self].to_csv)
45
+ assert_equal("a,b,c,d\n1,2,3,4\n\n3,2,3,4\n5,2,3,4\n", align_result[:other].to_csv)
46
+ assert_equal("a,b,c,d\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n", align_result[:self].to_csv)
54
47
 
55
- tself = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,3,3,4], [3,2,3,4],[5,2,3,4]]).sheet.table
56
- tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4], [2,2,3,4], [5,2,3,4]]).sheet.table
48
+ tself = Workbook::Book.new([["a", "b", "c", "d"], [1, 2, 3, 4], [1, 3, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
49
+ tother = Workbook::Book.new([["a", "b", "c", "d"], [1, 2, 3, 4], [2, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
57
50
  align_result = tself.align tother
58
- assert_equal("a,b,c,d\n1,2,3,4\n1,3,3,4\n\n3,2,3,4\n5,2,3,4\n",align_result[:self].to_csv)
59
- assert_equal("a,b,c,d\n1,2,3,4\n\n2,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
60
- tself = Workbook::Book.new( [['a','b','c','d'],[1,2,3,4], [3,2,3,4],[5,2,3,4]]).sheet.table
61
- tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,2,3,4],[1,2,3,4],[2,2,3,4], [5,2,3,4]]).sheet.table
51
+ assert_equal("a,b,c,d\n1,2,3,4\n1,3,3,4\n\n3,2,3,4\n5,2,3,4\n", align_result[:self].to_csv)
52
+ assert_equal("a,b,c,d\n1,2,3,4\n\n2,2,3,4\n\n5,2,3,4\n", align_result[:other].to_csv)
53
+ tself = Workbook::Book.new([["a", "b", "c", "d"], [1, 2, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
54
+ tother = Workbook::Book.new([["a", "b", "c", "d"], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [2, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
62
55
  align_result = tself.align tother
63
- assert_equal("a,b,c,d\n1,2,3,4\n\n\n\n3,2,3,4\n5,2,3,4\n",align_result[:self].to_csv)
64
- assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
65
- tself = Workbook::Book.new([['a','b','c','d'],[9,2,3,4],[3,2,3,4],[5,2,3,4]]).sheet.table
66
- tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,2,3,4],[1,2,3,4],[2,2,3,4],[5,2,3,4]]).sheet.table
56
+ assert_equal("a,b,c,d\n1,2,3,4\n\n\n\n3,2,3,4\n5,2,3,4\n", align_result[:self].to_csv)
57
+ assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n", align_result[:other].to_csv)
58
+ tself = Workbook::Book.new([["a", "b", "c", "d"], [9, 2, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
59
+ tother = Workbook::Book.new([["a", "b", "c", "d"], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [2, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
67
60
  align_result = tself.align tother
68
- assert_equal("a,b,c,d\n\n\n\n\n3,2,3,4\n5,2,3,4\n9,2,3,4\n",align_result[:self].to_csv)
69
- assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n\n",align_result[:other].to_csv)
70
- tself = Workbook::Book.new([['a','b','c','d'],[49,2,3,4],[10,2,3,4],[45,2,3,4]]).sheet.table
71
- tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,2,3,4],[1,2,3,4],[2,2,3,4],[5,2,3,4]]).sheet.table
61
+ assert_equal("a,b,c,d\n\n\n\n\n3,2,3,4\n5,2,3,4\n9,2,3,4\n", align_result[:self].to_csv)
62
+ assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n\n", align_result[:other].to_csv)
63
+ tself = Workbook::Book.new([["a", "b", "c", "d"], [49, 2, 3, 4], [10, 2, 3, 4], [45, 2, 3, 4]]).sheet.table
64
+ tother = Workbook::Book.new([["a", "b", "c", "d"], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [2, 2, 3, 4], [5, 2, 3, 4]]).sheet.table
72
65
  align_result = tself.align tother
73
- assert_equal("a,b,c,d\n\n\n\n\n\n10,2,3,4\n45,2,3,4\n49,2,3,4\n",align_result[:self].to_csv)
74
- assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n5,2,3,4\n\n\n\n",align_result[:other].to_csv)
75
-
66
+ assert_equal("a,b,c,d\n\n\n\n\n\n10,2,3,4\n45,2,3,4\n49,2,3,4\n", align_result[:self].to_csv)
67
+ assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n5,2,3,4\n\n\n\n", align_result[:other].to_csv)
76
68
  end
77
69
 
78
70
  def test_with_nil_header
79
- a = Workbook::Book.new([['a','b',nil],[1,2,3]])
80
- b = Workbook::Book.new([['a','b','c'],[1,2,3]])
71
+ a = Workbook::Book.new([["a", "b", nil], [1, 2, 3]])
72
+ b = Workbook::Book.new([["a", "b", "c"], [1, 2, 3]])
81
73
  c = a.sheet.table.diff b.sheet.table
82
- assert_equal("a,b,(was: c)\n1,2,(was: 3)\n",c.to_csv)
74
+ assert_equal("a,b,(was: c)\n1,2,(was: 3)\n", c.to_csv)
83
75
  end
84
76
 
85
77
  # def test_diff_file
@@ -94,8 +86,8 @@ module Modules
94
86
  # end
95
87
 
96
88
  def test_diff
97
- ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4],[3,3,3,4]]
98
- bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4],[3,2,3,4]]
89
+ ba = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [4, 2, 3, 4], [3, 2, 3, 4], [3, 3, 3, 4]]
90
+ bb = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4], [3, 2, 3, 4]]
99
91
  # Start:
100
92
  # ba = [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4],[3,3,3,4]]
101
93
  # bb = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4],[3,2,3,4]]
@@ -110,21 +102,20 @@ module Modules
110
102
  tself = ba.sheet.table
111
103
  tother = bb.sheet.table
112
104
  diff_result = tself.diff tother
113
- assert_equal('a',diff_result.sheet.table.header[0].value)
114
- assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n3,3 (was: 2),3,4\n4,2,3,4\n(was: 5),(was: 2),(was: 3),(was: 4)\n",diff_result.to_csv)
105
+ assert_equal("a", diff_result.sheet.table.header[0].value)
106
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n3,3 (was: 2),3,4\n4,2,3,4\n(was: 5),(was: 2),(was: 3),(was: 4)\n", diff_result.to_csv)
115
107
  diff_result.sheet.book.write_to_xls
116
108
  end
117
109
 
118
110
  def test_workbook_diff
119
- ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4],[3,3,3,4]]
120
- bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4],[3,2,3,4]]
121
- ba << [['e','f','g','h'],[1,2,3,4],[4,2,3,4],[3,2,3,4],[3,3,3,4]]
122
- bb << [['e','f','g','h'],[1,2,2,2],[4,2,3,4],[3,2,3,4],[3,3,3,6]]
111
+ ba = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [4, 2, 3, 4], [3, 2, 3, 4], [3, 3, 3, 4]]
112
+ bb = Workbook::Book.new [["a", "b", "c", "d"], [1, 2, 3, 4], [3, 2, 3, 4], [5, 2, 3, 4], [3, 2, 3, 4]]
113
+ ba << [["e", "f", "g", "h"], [1, 2, 3, 4], [4, 2, 3, 4], [3, 2, 3, 4], [3, 3, 3, 4]]
114
+ bb << [["e", "f", "g", "h"], [1, 2, 2, 2], [4, 2, 3, 4], [3, 2, 3, 4], [3, 3, 3, 6]]
123
115
  diff_result = ba.diff bb
124
116
 
125
- assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n3,3 (was: 2),3,4\n4,2,3,4\n(was: 5),(was: 2),(was: 3),(was: 4)\n",diff_result[0][0].to_csv)
126
- assert_equal("e,f,g,h\n1,2,3 (was: 2),4 (was: 2)\n3,2,3,4\n3,3,3,4 (was: 6)\n4,2,3,4\n",diff_result[1][0].to_csv)
127
-
117
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n3,3 (was: 2),3,4\n4,2,3,4\n(was: 5),(was: 2),(was: 3),(was: 4)\n", diff_result[0][0].to_csv)
118
+ assert_equal("e,f,g,h\n1,2,3 (was: 2),4 (was: 2)\n3,2,3,4\n3,3,3,4 (was: 6)\n4,2,3,4\n", diff_result[1][0].to_csv)
128
119
  end
129
120
  end
130
121
  end
@@ -1,68 +1,98 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # -*- encoding : utf-8 -*-
4
- require File.join(File.dirname(__FILE__), 'helper')
3
+ require File.join(File.dirname(__FILE__), "helper")
5
4
  module Modules
6
5
  class TestTypeParser < Minitest::Test
7
6
  def examples
8
7
  {
9
- "2312"=>2312,
10
- "12-12-2012"=>Date.new(2012,12,12),
11
- "12-12-2012 12:24"=>DateTime.new(2012,12,12,12,24),
12
- "2012-12-12 12:24"=>DateTime.new(2012,12,12,12,24),
13
- "2011-05-19T15_37_49 - 52349.xml"=>"2011-05-19T15_37_49 - 52349.xml",
14
- "20-2-2012 20:52"=>DateTime.new(2012,2,20,20,52),
15
- "1-11-2011"=>Date.new(2011,11,1),
16
- "12/12/2012"=>Date.new(2012,12,12),
8
+ "2312" => 2312,
9
+ "12-12-2012" => Date.new(2012, 12, 12),
10
+ "12-12-2012 12:24" => DateTime.new(2012, 12, 12, 12, 24),
11
+ "2012-12-12 12:24" => DateTime.new(2012, 12, 12, 12, 24),
12
+ "2012-12-12T12:24" => DateTime.new(2012, 12, 12, 12, 24),
13
+ "2011-05-19T15_37_49 - 52349.xml" => "2011-05-19T15_37_49 - 52349.xml",
14
+ "20-2-2012 20:52" => DateTime.new(2012, 2, 20, 20, 52),
15
+ "1-11-2011" => Date.new(2011, 11, 1),
16
+ "12/12/2012" => Date.new(2012, 12, 12),
17
17
  # "12/23/1980"=>Date.new(1980,12,23), #TODO: should probably depend on locale, see: http://bugs.ruby-lang.org/issues/634#note-10
18
- "jA"=>"jA",
19
- "n"=>"n",
20
- ""=>nil,
21
- " "=> nil,
22
- "12 bomen"=>"12 bomen",
23
- "12 bomenasdfasdfsadf"=>"12 bomenasdfasdfsadf",
24
- "mailto:sadf@asdf.as"=>"sadf@asdf.as",
25
- "012-3456789"=>"012-3456789",
26
- "TRUE"=>true
18
+ "jA" => "jA",
19
+ "n" => "n",
20
+ "" => nil,
21
+ " " => nil,
22
+ "12 bomen" => "12 bomen",
23
+ "12 bomenasdfasdfsadf" => "12 bomenasdfasdfsadf",
24
+ "mailto:sadf@asdf.as" => "sadf@asdf.as",
25
+ "012-3456789" => "012-3456789",
26
+ "TRUE" => true
27
27
  }
28
28
  end
29
29
 
30
30
  def test_parse
31
- examples.each do |k,v|
32
- if v == nil
33
- assert_nil(Workbook::Cell.new(k).parse({:detect_date => true}))
31
+ examples.each do |k, v|
32
+ if v.nil?
33
+ assert_nil(Workbook::Cell.new(k).parse({detect_date: true}))
34
34
  else
35
- assert_equal(v, Workbook::Cell.new(k).parse({:detect_date => true}))
35
+ assert_equal(v, Workbook::Cell.new(k).parse({detect_date: true}))
36
36
  end
37
37
  end
38
38
  end
39
39
 
40
40
  def test_custom_parse
41
- customparsers = [proc{|v| "#{v}2" }]
42
- examples.each do |k,v|
41
+ customparsers = [proc { |v| "#{v}2" }]
42
+ examples.each do |k, v|
43
43
  c = Workbook::Cell.new(k)
44
44
  c.string_parsers = customparsers
45
- refute_equal(v,c.parse)
45
+ refute_equal(v, c.parse)
46
46
  end
47
47
  c = Workbook::Cell.new("233")
48
48
  c.string_parsers = customparsers
49
- assert_equal("2332",c.parse)
49
+ assert_equal("2332", c.parse)
50
50
  c.value = "v"
51
- assert_equal("v2",c.parse)
51
+ assert_equal("v2", c.parse)
52
+ end
53
+
54
+ def test_american_date_format
55
+ c = Workbook::Cell.new("01-11-2012")
56
+ c.string_parsers << :string_american_date_converter
57
+ assert_equal(Date.new(2012, 1, 11), c.parse)
58
+ c = Workbook::Cell.new("12-31-2012")
59
+ c.string_parsers << :string_american_date_converter
60
+ assert_equal(Date.new(2012, 12, 31), c.parse)
61
+ c = Workbook::Cell.new("12/31/2012")
62
+ c.string_parsers << :string_american_date_converter
63
+ assert_equal(Date.new(2012, 12, 31), c.parse)
64
+ c = Workbook::Cell.new("12/1/2012")
65
+ c.string_parsers << :string_american_date_converter
66
+ assert_equal(Date.new(2012, 12, 1), c.parse)
67
+ end
68
+
69
+ def test_non_american_date_format
70
+ c = Workbook::Cell.new("01-11-2012")
71
+ c.string_parsers << :string_non_american_date_converter
72
+ assert_equal(Date.new(2012, 11, 1), c.parse)
73
+ c = Workbook::Cell.new("31/12/2012")
74
+ c.string_parsers << :string_non_american_date_converter
75
+ assert_equal(Date.new(2012, 12, 31), c.parse)
76
+ c = Workbook::Cell.new("31.12.2012")
77
+ c.string_parsers << :string_non_american_date_converter
78
+ assert_equal(Date.new(2012, 12, 31), c.parse)
79
+ c = Workbook::Cell.new("12-1-2012")
80
+ c.string_parsers << :string_non_american_date_converter
81
+ assert_equal(Date.new(2012, 1, 12), c.parse)
52
82
  end
53
83
 
54
84
  def test_parse!
55
- r= Workbook::Row.new
85
+ r = Workbook::Row.new
56
86
  r[0] = Workbook::Cell.new "xls_cell"
57
87
  r[0].parse!
58
- assert_equal("xls_cell",r[0].value)
88
+ assert_equal("xls_cell", r[0].value)
59
89
  r[1] = Workbook::Cell.new ""
60
90
  r[1].parse!
61
91
  assert_nil(r[1].value)
62
92
  end
63
93
 
64
94
  def test_once_failing_files
65
- w = Workbook::Book.open(File.join(File.dirname(__FILE__), 'artifacts/failing_import1.xls')) # TRUE wasn't parsed properly
95
+ w = Workbook::Book.open(File.join(File.dirname(__FILE__), "artifacts/failing_import1.xls")) # TRUE wasn't parsed properly
66
96
  assert_equal(Workbook::Book, w.class)
67
97
  end
68
98
  end