spreadbase 0.1.2 → 0.3.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.
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
-
3
1
  module SpreadBase
4
- VERSION = "0.1.2"
2
+ VERSION = "0.3.0"
5
3
  end
@@ -1,25 +1,5 @@
1
- # encoding: UTF-8
2
-
3
- =begin
4
- Copyright 2012 Saverio Miroddi saverio.pub2 <a-hat!> gmail.com
5
-
6
- This file is part of SpreadBase.
7
-
8
- SpreadBase is free software: you can redistribute it and/or modify it under the
9
- terms of the GNU Lesser General Public License as published by the Free Software
10
- Foundation, either version 3 of the License, or (at your option) any later
11
- version.
12
-
13
- SpreadBase is distributed in the hope that it will be useful, but WITHOUT ANY
14
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15
- PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
-
17
- You should have received a copy of the GNU Lesser General Public License along
18
- with SpreadBase. If not, see <http://www.gnu.org/licenses/>.
19
- =end
20
-
21
- require File.expand_path( '../../../lib/spreadbase', __FILE__ )
22
- require File.expand_path( '../../spec_helpers', __FILE__ )
1
+ require_relative '../../lib/spreadbase'
2
+ require_relative '../spec_helpers'
23
3
 
24
4
  require 'date'
25
5
  require 'bigdecimal'
@@ -33,13 +13,13 @@ describe SpreadBase::Codecs::OpenDocument12 do
33
13
  before :each do
34
14
  table_1 = SpreadBase::Table.new(
35
15
  'abc', [
36
- [ 1, 1.1, T_BIGDECIMAL ],
37
- [ T_DATE, T_DATETIME, T_TIME ],
38
- [ nil, 'a', nil ]
16
+ [1, 1.1, T_BIGDECIMAL],
17
+ [T_DATE, T_DATETIME, T_TIME],
18
+ [nil, nil, 'a']
39
19
  ]
40
20
  )
41
21
 
42
- table_2 = SpreadBase::Table.new( 'cde' )
22
+ table_2 = SpreadBase::Table.new('cde')
43
23
 
44
24
  @sample_document = SpreadBase::Document.new
45
25
 
@@ -49,89 +29,173 @@ describe SpreadBase::Codecs::OpenDocument12 do
49
29
  # :encode/:decode
50
30
  #
51
31
  it "should encode and decode the sample document" do
52
- document_archive = SpreadBase::Codecs::OpenDocument12.new.encode_to_archive( @sample_document )
32
+ document_archive = SpreadBase::Codecs::OpenDocument12.new.encode_to_archive(@sample_document)
53
33
 
54
- document = SpreadBase::Codecs::OpenDocument12.new.decode_archive( document_archive, :floats_as_bigdecimal => true )
34
+ document = SpreadBase::Codecs::OpenDocument12.new.decode_archive(document_archive, floats_as_bigdecimal: true)
55
35
 
56
- assert_size( document.tables, 2 ) do | table_1, table_2 |
36
+ assert_size(document.tables, 2) do | table_1, table_2 |
57
37
 
58
- table_1.name.should == 'abc'
38
+ expect(table_1.name).to eq('abc')
59
39
 
60
- assert_size( table_1.data, 3 ) do | row_1, row_2, row_3 |
40
+ assert_size(table_1.data, 3) do | row_1, row_2, row_3 |
61
41
 
62
- assert_size( row_1, 3 ) do | value_1, value_2, value_3 |
63
- value_1.should == 1
64
- value_1.should be_a( Fixnum )
65
- value_2.should == 1.1
66
- value_2.should be_a( BigDecimal )
67
- value_3.should == T_BIGDECIMAL
68
- value_3.should be_a( BigDecimal )
42
+ assert_size(row_1, 3) do | value_1, value_2, value_3 |
43
+ expect(value_1).to eq(1)
44
+ expect(value_1).to be_a(Integer)
45
+ expect(value_2).to eq(1.1)
46
+ expect(value_2).to be_a(BigDecimal)
47
+ expect(value_3).to eq(T_BIGDECIMAL)
48
+ expect(value_3).to be_a(BigDecimal)
69
49
  end
70
50
 
71
- assert_size( row_2, 3 ) do | value_1, value_2, value_3 |
72
- value_1.should == T_DATE
73
- value_2.should == T_DATETIME
74
- value_3.should == T_DATETIME
51
+ assert_size(row_2, 3) do | value_1, value_2, value_3 |
52
+ expect(value_1).to eq(T_DATE)
53
+ expect(value_2).to eq(T_DATETIME)
54
+ expect(value_3).to eq(T_DATETIME)
75
55
  end
76
56
 
77
- assert_size( row_3, 3 ) do | value_1, value_2, value_3 |
78
- value_1.should == nil
79
- value_2.should == 'a'
80
- value_3.should == nil
57
+ assert_size(row_3, 3) do | value_1, value_2, value_3 |
58
+ expect(value_1).to eq(nil)
59
+ expect(value_2).to eq(nil)
60
+ expect(value_3).to eq('a')
81
61
  end
82
62
 
83
63
  end
84
64
 
85
- table_2.name.should == 'cde'
65
+ expect(table_2.name).to eq('cde')
86
66
 
87
- assert_size( table_2.data, 0 )
67
+ assert_size(table_2.data, 0)
88
68
  end
89
69
  end
90
70
 
91
71
  # Not worth testing in detail; just ensure that the pref
92
72
  #
93
73
  it "should encode the document with makeup (:prettify) - SMOKE" do
94
- formatter = stub_initializer( REXML::Formatters::Pretty )
74
+ formatter = stub_initializer(REXML::Formatters::Pretty)
95
75
 
96
- formatter.should_receive( :write )
76
+ expect(formatter).to receive(:write)
97
77
 
98
- SpreadBase::Codecs::OpenDocument12.new.encode_to_archive( @sample_document, :prettify => true )
78
+ SpreadBase::Codecs::OpenDocument12.new.encode_to_archive(@sample_document, prettify: true)
99
79
  end
100
80
 
101
81
  # Those methods are actually "utility" (read: testing) methods.
102
82
  #
103
83
  it "should encode/decode the content.xml - SMOKE" do
104
- content_xml = SpreadBase::Codecs::OpenDocument12.new.encode_to_content_xml( @sample_document )
84
+ content_xml = SpreadBase::Codecs::OpenDocument12.new.encode_to_content_xml(@sample_document)
105
85
 
106
- document = SpreadBase::Codecs::OpenDocument12.new.decode_content_xml( content_xml )
86
+ document = SpreadBase::Codecs::OpenDocument12.new.decode_content_xml(content_xml)
107
87
 
108
- assert_size( document.tables, 2 )
88
+ assert_size(document.tables, 2)
109
89
  end
110
90
 
111
91
  # If values are not converted to UTF-8, some encodings cause an error to be
112
92
  # raised when assigning a value to a cell.
113
93
  #
114
- # 1.8 tests can't be done, since the official platform is 1.9
115
- #
116
94
  it "should convert to utf-8 before saving" do
117
- string = "à".encode( 'UTF-16' )
95
+ string = "à".encode('utf-16')
118
96
 
119
- @sample_document.tables[ 0 ][ 0, 0 ] = string
97
+ @sample_document.tables[0][0, 0] = string
120
98
 
121
99
  # Doesn't encode correctly if the value is not converted
122
100
  #
123
- SpreadBase::Codecs::OpenDocument12.new.encode_to_content_xml( @sample_document )
101
+ SpreadBase::Codecs::OpenDocument12.new.encode_to_content_xml(@sample_document)
124
102
  end
125
103
 
126
104
  it "should decode as BigDecimal" do
127
- content_xml = SpreadBase::Codecs::OpenDocument12.new.encode_to_content_xml( @sample_document )
105
+ content_xml = SpreadBase::Codecs::OpenDocument12.new.encode_to_content_xml(@sample_document)
106
+
107
+ document = SpreadBase::Codecs::OpenDocument12.new.decode_content_xml(content_xml, floats_as_bigdecimal: true)
108
+
109
+ value = document.tables[0][2, 0]
110
+
111
+ expect(value).to be_a(BigDecimal)
112
+ expect(value).to eq(T_BIGDECIMAL)
113
+ end
114
+
115
+ context "when cells at the end of the row are empty" do
116
+ let(:document_archive) do
117
+ document = SpreadBase::Document.new
118
+
119
+ document.tables << SpreadBase::Table.new(
120
+ 'abc', [
121
+ [nil],
122
+ [nil, nil],
123
+ [1 , nil],
124
+ [nil, 1 , nil]
125
+ ]
126
+ )
127
+
128
+ SpreadBase::Codecs::OpenDocument12.new.encode_to_archive(document)
129
+ end
130
+
131
+ it "should drop such cells" do
132
+ document = SpreadBase::Codecs::OpenDocument12.new.decode_archive(document_archive)
133
+ table = document.tables[0]
128
134
 
129
- document = SpreadBase::Codecs::OpenDocument12.new.decode_content_xml( content_xml, :floats_as_bigdecimal => true )
135
+ assert_size(table.data, 4) do |row_1, row_2, row_3, row_4|
136
+ assert_size(row_1, 0)
130
137
 
131
- value = document.tables[ 0 ][ 2, 0 ]
138
+ assert_size(row_2, 0)
132
139
 
133
- value.should be_a( BigDecimal )
134
- value.should == T_BIGDECIMAL
140
+ assert_size(row_3, 1) do |value_1|
141
+ expect(value_1).to eq(1)
142
+ end
143
+
144
+ assert_size(row_4, 2) do |value_1, value_2|
145
+ expect(value_1).to be_nil
146
+ expect(value_2).to eq(1)
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ context "when cells of the last row are empty" do
153
+ let(:document_archive) do
154
+ document = SpreadBase::Document.new
155
+
156
+ document.tables << SpreadBase::Table.new(
157
+ 'abc', [
158
+ []
159
+ ]
160
+ )
161
+
162
+ document.tables << SpreadBase::Table.new(
163
+ 'def', [
164
+ [nil]
165
+ ]
166
+ )
167
+
168
+ document.tables << SpreadBase::Table.new(
169
+ 'ghi', [
170
+ [nil, nil]
171
+ ]
172
+ )
173
+
174
+ document.tables << SpreadBase::Table.new(
175
+ 'jkl', [
176
+ [nil],
177
+ [1],
178
+ [nil]
179
+ ]
180
+ )
181
+
182
+ SpreadBase::Codecs::OpenDocument12.new.encode_to_archive(document)
183
+ end
184
+
185
+ it "should drop such row" do
186
+ document = SpreadBase::Codecs::OpenDocument12.new.decode_archive(document_archive)
187
+ tables = document.tables
188
+
189
+ assert_size(tables, 4) do |table_1, table_2, table_3, table_4|
190
+ assert_size(table_1.data, 0)
191
+
192
+ assert_size(table_2.data, 0)
193
+
194
+ assert_size(table_3.data, 0)
195
+
196
+ assert_size(table_4.data, 2)
197
+ end
198
+ end
135
199
  end
136
200
 
137
201
  end
@@ -1,25 +1,5 @@
1
- # encoding: utf-8
2
-
3
- =begin
4
- Copyright 2012 Saverio Miroddi saverio.pub2 <a-hat!> gmail.com
5
-
6
- This file is part of SpreadBase.
7
-
8
- SpreadBase is free software: you can redistribute it and/or modify it under the
9
- terms of the GNU Lesser General Public License as published by the Free Software
10
- Foundation, either version 3 of the License, or (at your option) any later
11
- version.
12
-
13
- SpreadBase is distributed in the hope that it will be useful, but WITHOUT ANY
14
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15
- PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
-
17
- You should have received a copy of the GNU Lesser General Public License along
18
- with SpreadBase. If not, see <http://www.gnu.org/licenses/>.
19
- =end
20
-
21
- require File.expand_path( '../../../lib/spreadbase', __FILE__ )
22
- require File.expand_path( '../../spec_helpers', __FILE__ )
1
+ require_relative '../../lib/spreadbase'
2
+ require_relative '../spec_helpers'
23
3
 
24
4
  include SpecHelpers
25
5
 
@@ -30,9 +10,9 @@ describe SpreadBase::Document do
30
10
  @sample_document.tables = [
31
11
  SpreadBase::Table.new(
32
12
  'abc', [
33
- [ 1, 1.1, T_BIGDECIMAL ],
34
- [ T_DATE, T_DATETIME, T_TIME ],
35
- [ true, 'a', nil ]
13
+ [1, 1.1, T_BIGDECIMAL],
14
+ [T_DATE, T_DATETIME, T_TIME],
15
+ [true, 'a', nil]
36
16
  ]
37
17
  )
38
18
  ]
@@ -43,61 +23,59 @@ describe SpreadBase::Document do
43
23
  it "should initialize out of thin air" do
44
24
  document = SpreadBase::Document.new
45
25
 
46
- document.document_path.should == nil
26
+ expect(document.document_path).to be(nil)
47
27
 
48
- document.tables.should be_empty
28
+ expect(document.tables).to be_empty
49
29
  end
50
30
 
51
31
  # A lazy use of stubs
52
32
  #
53
33
  it "should initialize from a file" do
54
- codec = stub_initializer( SpreadBase::Codecs::OpenDocument12 )
34
+ codec = stub_initializer(SpreadBase::Codecs::OpenDocument12)
55
35
 
56
- File.should_receive( :'exists?' ).with( '/pizza/margerita.txt' ).and_return( true )
57
- IO.should_receive( :read ).with( '/pizza/margerita.txt' ).and_return( 'abc' )
58
- codec.should_receive( :decode_archive ).with( 'abc', {} ).and_return( @sample_document )
36
+ expect(File).to receive(:'exist?').with('/pizza/margerita.txt').and_return(true)
37
+ expect(IO).to receive(:read).with('/pizza/margerita.txt').and_return('abc')
38
+ expect(codec).to receive(:decode_archive).with('abc', {}).and_return(@sample_document)
59
39
 
60
- document = SpreadBase::Document.new( '/pizza/margerita.txt' )
40
+ document = SpreadBase::Document.new('/pizza/margerita.txt')
61
41
 
62
- assert_size( document.tables, 1 ) do | table |
63
- table.name.should == 'abc'
64
- table.data.size.should == 3
42
+ assert_size(document.tables, 1) do | table |
43
+ expect(table.name).to eq('abc')
44
+ expect(table.data.size).to eq(3)
65
45
  end
66
46
  end
67
47
 
68
48
  it "should initialize with a non-existing file" do
69
- codec = stub_initializer( SpreadBase::Codecs::OpenDocument12 )
70
-
71
- document = SpreadBase::Document.new( '/pizza/margerita.txt' )
49
+ document = SpreadBase::Document.new('/pizza/margerita.txt')
72
50
 
73
- assert_size( document.tables, 0 )
51
+ assert_size(document.tables, 0)
74
52
  end
75
53
 
76
54
  it "should save to a file" do
77
- codec = stub_initializer( SpreadBase::Codecs::OpenDocument12 )
55
+ codec = stub_initializer(SpreadBase::Codecs::OpenDocument12)
78
56
 
79
57
  document = SpreadBase::Document.new
80
- document.tables << SpreadBase::Table.new( 'Ya-ha!' )
58
+ document.tables << SpreadBase::Table.new('Ya-ha!')
81
59
  document.document_path = '/tmp/abc.ods'
82
60
 
83
- codec.should_receive( :encode_to_archive ).with( document, :prettify => 'abc' ).and_return( 'sob!' )
84
- File.should_receive( :open ).with( '/tmp/abc.ods', 'wb' )
61
+ expect(codec).to receive(:encode_to_archive).with(document, prettify: 'abc').and_return('sob!')
62
+ expect(File).to receive(:open).with('/tmp/abc.ods', 'wb')
85
63
 
86
- document.save( :prettify => 'abc' )
64
+ document.save(prettify: 'abc')
87
65
  end
88
66
 
89
67
  it "should raise an error when trying to save without a filename" do
90
68
  document = SpreadBase::Document.new
91
- document.tables << SpreadBase::Table.new( 'Ya-ha!' )
69
+ document.tables << SpreadBase::Table.new('Ya-ha!')
92
70
 
93
- lambda { document.save }.should raise_error( RuntimeError, "Document path not specified" )
71
+ expect { document.save }.to raise_error(RuntimeError, "Document path not specified")
94
72
  end
95
73
 
96
74
  it "should raise an error when trying to save without tables" do
97
75
  document = SpreadBase::Document.new
98
76
  document.document_path = 'abc.ods'
99
77
 
100
- lambda { document.save }.should raise_error( RuntimeError, "At least one table must be present" )
78
+ expect { document.save }.to raise_error(RuntimeError, "At least one table must be present")
101
79
  end
102
80
 
103
81
  it "should return the data as string (:to_s)" do
@@ -105,14 +83,14 @@ describe SpreadBase::Document do
105
83
  abc:
106
84
 
107
85
  +------------+---------------------------+---------------------------+
108
- | 1 | 1.1 | 0.133E1 |
109
- | 2012-04-10 | 2012-04-11T23:33:42+00:00 | 2012-04-11 23:33:42 +0200 |
110
- | true | a | |
86
+ | 1 | 1.1 | 1.33 |
87
+ | 2012-04-10 | 2012-04-11 23:33:42 +0000 | 2012-04-11 23:33:42 +0200 |
88
+ | true | a | NIL |
111
89
  +------------+---------------------------+---------------------------+
112
90
 
113
91
  "
114
92
 
115
- @sample_document.to_s.should == expected_string
93
+ expect(@sample_document.to_s).to eq(expected_string)
116
94
  end
117
95
 
118
96
  it "should return the data as string, with headers (:to_s)" do
@@ -120,15 +98,38 @@ abc:
120
98
  abc:
121
99
 
122
100
  +------------+---------------------------+---------------------------+
123
- | 1 | 1.1 | 0.133E1 |
101
+ | 1 | 1.1 | 1.33 |
124
102
  +------------+---------------------------+---------------------------+
125
- | 2012-04-10 | 2012-04-11T23:33:42+00:00 | 2012-04-11 23:33:42 +0200 |
126
- | true | a | |
103
+ | 2012-04-10 | 2012-04-11 23:33:42 +0000 | 2012-04-11 23:33:42 +0200 |
104
+ | true | a | NIL |
127
105
  +------------+---------------------------+---------------------------+
128
106
 
129
107
  "
130
108
 
131
- @sample_document.to_s( :with_headers => true ).should == expected_string
109
+ expect(@sample_document.to_s(with_headers: true)).to eq(expected_string)
110
+ end
111
+
112
+ it "should parse a existing ODS file" do
113
+ ods_file = File.expand_path('../fixtures/test.ods', __dir__)
114
+ document = SpreadBase::Document.new(ods_file)
115
+
116
+ expect(document.tables[0].name).to eq('Sheet1')
117
+ expect(document.tables[0].data).to match([
118
+ match(['hoge' ]),
119
+ match([nil , 'fuga' ]),
120
+ match([nil , nil , 'piyo' ]),
121
+ match([nil , nil , nil , 'hogera' ]),
122
+ match([nil , nil , nil , nil , 'hogehoge'])
123
+ ])
124
+
125
+ expect(document.tables[1].name).to eq('Sheet2')
126
+ expect(document.tables[1].data).to match([
127
+ match([nil , nil , nil , nil , 'foo']),
128
+ match([nil , nil , nil , 'bar' ]),
129
+ match([nil , nil , 'baz' ]),
130
+ match([nil , 'foobar' ]),
131
+ match(['qux' ])
132
+ ])
132
133
  end
133
134
 
134
135
  end
@@ -1,36 +1,18 @@
1
- # encoding: UTF-8
2
-
3
- =begin
4
- Copyright 2012 Saverio Miroddi saverio.pub2 <a-hat!> gmail.com
5
-
6
- This file is part of SpreadBase.
7
-
8
- SpreadBase is free software: you can redistribute it and/or modify it under the
9
- terms of the GNU Lesser General Public License as published by the Free Software
10
- Foundation, either version 3 of the License, or (at your option) any later
11
- version.
12
-
13
- SpreadBase is distributed in the hope that it will be useful, but WITHOUT ANY
14
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15
- PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
-
17
- You should have received a copy of the GNU Lesser General Public License along
18
- with SpreadBase. If not, see <http://www.gnu.org/licenses/>.
19
- =end
20
-
21
- require File.expand_path( '../../../lib/spreadbase', __FILE__ )
22
- require File.expand_path( '../../spec_helpers', __FILE__ )
1
+ require_relative '../../lib/spreadbase'
2
+ require_relative '../spec_helpers'
23
3
 
24
4
  include SpecHelpers
25
5
 
6
+ include SpreadBase
7
+
26
8
  describe SpreadBase::Table do
27
9
 
28
10
  before :each do
29
11
  @sample_table = SpreadBase::Table.new(
30
12
  'abc', [
31
- [ 1, 1.1, T_BIGDECIMAL ],
32
- [ T_DATE, T_DATETIME, T_TIME ],
33
- [ true, 'a', nil ]
13
+ [1, 1.1, T_BIGDECIMAL],
14
+ [T_DATE, T_DATETIME, T_TIME],
15
+ [true, Cell.new('a'), nil]
34
16
  ]
35
17
  )
36
18
  end
@@ -39,89 +21,133 @@ describe SpreadBase::Table do
39
21
  # this routine is called, by checking against the index (-1).
40
22
  #
41
23
  it "should check the row index" do
42
- lambda { @sample_table.row( 4 ) }.should raise_error( RuntimeError, "Invalid row index (4) - allowed 0 to 2" )
24
+ expect { @sample_table.row(4) }.to raise_error(RuntimeError, "Invalid row index (4) - allowed 0 to 2")
43
25
 
44
26
  # called with :allow_append
45
- lambda { @sample_table.insert_row( -1, [] ) }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 3" )
46
- lambda { @sample_table.insert_row( 40, [] ) }.should raise_error( RuntimeError, "Invalid row index (40) - allowed 0 to 3" )
27
+ expect { @sample_table.insert_row(-1, []) }.to raise_error(RuntimeError, "Invalid row index (-1) - allowed 0 to 3")
28
+ expect { @sample_table.insert_row(40, []) }.to raise_error(RuntimeError, "Invalid row index (40) - allowed 0 to 3")
47
29
  end
48
30
 
49
- it "should initialize with data" do
31
+ it "should initialize with data, and return the data" do
50
32
  expected_data = [
51
- [ 1, 1.1, T_BIGDECIMAL ],
52
- [ T_DATE, T_DATETIME, T_TIME ],
53
- [ true, 'a', nil ]
33
+ [1, 1.1, T_BIGDECIMAL],
34
+ [T_DATE, T_DATETIME, T_TIME],
35
+ [true, 'a', nil]
54
36
  ]
55
37
 
56
- @sample_table.data.should == expected_data
38
+ expect(@sample_table.data).to eq(expected_data)
39
+ end
40
+
41
+ it "return the data in cell format" do
42
+ expected_data = [
43
+ [Cell.new(1), Cell.new(1.1), Cell.new(T_BIGDECIMAL)],
44
+ [Cell.new(T_DATE), Cell.new(T_DATETIME), Cell.new(T_TIME)],
45
+ [Cell.new(true), Cell.new('a'), Cell.new(nil)]
46
+ ]
47
+
48
+ expect(@sample_table.data(as_cell: true)).to eq(expected_data)
57
49
  end
58
50
 
59
51
  it "should raise an error when the initialization requirements are not met" do
60
- lambda { SpreadBase::Table.new( nil ) }.should raise_error( "Table name required" )
61
- lambda { SpreadBase::Table.new( '' ) }.should raise_error( "Table name required" )
52
+ expect { SpreadBase::Table.new(nil) }.to raise_error("Table name required")
53
+ expect { SpreadBase::Table.new('') }.to raise_error("Table name required")
62
54
 
63
55
  # This is acceptable
64
56
  #
65
- SpreadBase::Table.new( ' ' )
57
+ SpreadBase::Table.new(' ')
66
58
  end
67
59
 
68
60
  it "should access a cell" do
69
- @sample_table[ 'a', 0 ].should == 1
70
- @sample_table[ 1, 0 ].should == 1.1
71
- @sample_table[ 2, 0 ].should == T_BIGDECIMAL
72
- @sample_table[ 0, 1 ].should == T_DATE
73
- @sample_table[ 'B', 1 ].should == T_DATETIME
74
- @sample_table[ 2, 1 ].should == T_TIME
75
- @sample_table[ 0, 2 ].should == true
76
- @sample_table[ 1, 2 ].should == 'a'
77
- @sample_table[ 2, 2 ].should == nil
78
-
79
- lambda { @sample_table[ -1, 0 ] }.should raise_error( RuntimeError, "Negative column indexes not allowed: -1" )
80
- lambda { @sample_table[ 0, -1 ] }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 2" )
81
- lambda { @sample_table[ 3, 0 ] }.should raise_error( RuntimeError, "Invalid column index (3) for the given row - allowed 0 to 2" )
61
+ expect(@sample_table['a', 0]).to eq(1)
62
+ expect(@sample_table[1, 0]).to eq(1.1)
63
+ expect(@sample_table[2, 0]).to eq(T_BIGDECIMAL)
64
+ expect(@sample_table[0, 1]).to eq(T_DATE)
65
+ expect(@sample_table['B', 1]).to eq(T_DATETIME)
66
+ expect(@sample_table[2, 1]).to eq(T_TIME)
67
+ expect(@sample_table[0, 2]).to eq(true)
68
+ expect(@sample_table[1, 2]).to eq('a')
69
+ expect(@sample_table[2, 2]).to eq(nil)
70
+
71
+ expect(@sample_table[1, 2, as_cell: true]).to eq(Cell.new('a'))
72
+
73
+ expect { @sample_table[-1, 0] }.to raise_error(RuntimeError, "Negative column indexes not allowed: -1")
74
+ expect { @sample_table[0, -1] }.to raise_error(RuntimeError, "Invalid row index (-1) - allowed 0 to 2")
75
+ expect { @sample_table[3, 0] }.to raise_error(RuntimeError, "Invalid column index (3) for the given row - allowed 0 to 2")
82
76
  end
83
77
 
84
78
  it "should set a cell value" do
85
- @sample_table[ 0, 0 ] = 10
86
- @sample_table[ 'B', 1 ] = T_TIME
79
+ @sample_table[0, 0] = 10
80
+ @sample_table['B', 1] = Cell.new(T_TIME)
87
81
 
88
- @sample_table.data.should == [
89
- [ 10, 1.1, T_BIGDECIMAL ],
90
- [ T_DATE, T_TIME, T_TIME ],
91
- [ true, 'a', nil ],
92
- ]
82
+ expect(@sample_table.data).to eq([
83
+ [10, 1.1, T_BIGDECIMAL],
84
+ [T_DATE, T_TIME, T_TIME],
85
+ [true, 'a', nil],
86
+ ])
93
87
 
94
- lambda { @sample_table[ 0, -1 ] = 33 }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 2" )
95
- lambda { @sample_table[ 3, 0 ] = 44 }.should raise_error( RuntimeError, "Invalid column index (3) for the given row - allowed 0 to 2" )
88
+ expect { @sample_table[0, -1] = 33 }.to raise_error(RuntimeError, "Invalid row index (-1) - allowed 0 to 2")
89
+ expect { @sample_table[3, 0] = 44 }.to raise_error(RuntimeError, "Invalid column index (3) for the given row - allowed 0 to 2")
96
90
  end
97
91
 
98
92
  it "should access a row" do
99
- @sample_table.row( 0 ).should == [ 1, 1.1, T_BIGDECIMAL ]
100
- @sample_table.row( 1 ).should == [ T_DATE, T_DATETIME, T_TIME ]
93
+ expect(@sample_table.row(0)).to eq([1, 1.1, T_BIGDECIMAL])
94
+
95
+ expect(@sample_table.row(1, as_cell: true)).to eq([Cell.new(T_DATE), Cell.new(T_DATETIME), Cell.new(T_TIME)])
96
+
97
+ expect { @sample_table.row(-1) }.to raise_error(RuntimeError, "Invalid row index (-1) - allowed 0 to 2")
98
+ end
101
99
 
102
- lambda { @sample_table.row( -1 ) }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 2" )
100
+ it "should access a set of rows by range" do
101
+ expect(@sample_table.row(0..1)).to eq([
102
+ [1, 1.1, T_BIGDECIMAL],
103
+ [T_DATE, T_DATETIME, T_TIME],
104
+ ])
105
+
106
+ expect { @sample_table.row(0..5) }.to raise_error(RuntimeError, "Invalid row index (5) - allowed 0 to 2")
107
+ end
108
+
109
+ it "should access a set of rows by range (as cell)" do
110
+ expect(@sample_table.row(0..1, as_cell: true)).to eq([
111
+ [Cell.new(1), Cell.new(1.1), Cell.new(T_BIGDECIMAL)],
112
+ [Cell.new(T_DATE), Cell.new(T_DATETIME), Cell.new(T_TIME)],
113
+ ])
114
+
115
+ expect { @sample_table.row(0..5) }.to raise_error(RuntimeError, "Invalid row index (5) - allowed 0 to 2")
103
116
  end
104
117
 
105
118
  it "should delete a row" do
106
- @sample_table.delete_row( 1 ).should == [ T_DATE, T_DATETIME, T_TIME ]
119
+ expect(@sample_table.delete_row(1)).to eq([T_DATE, T_DATETIME, T_TIME])
107
120
 
108
- @sample_table.data.should == [
109
- [ 1, 1.1, T_BIGDECIMAL ],
110
- [ true, 'a', nil ],
111
- ]
121
+ expect(@sample_table.data).to eq([
122
+ [1, 1.1, T_BIGDECIMAL],
123
+ [true, 'a', nil],
124
+ ])
112
125
 
113
- lambda { @sample_table.delete_row( -1 ) }.should raise_error( RuntimeError, "Invalid row index (-1) - allowed 0 to 1" )
126
+ expect { @sample_table.delete_row(-1) }.to raise_error(RuntimeError, "Invalid row index (-1) - allowed 0 to 1")
127
+ end
128
+
129
+ it "should delete a set of rows by range" do
130
+ expect(@sample_table.delete_row(0..1)).to eq([
131
+ [1, 1.1, T_BIGDECIMAL],
132
+ [T_DATE, T_DATETIME, T_TIME],
133
+ ])
134
+
135
+ expect(@sample_table.data).to eq([
136
+ [true, 'a', nil]
137
+ ])
138
+
139
+ expect { @sample_table.delete_row(0..5) }.to raise_error(RuntimeError, "Invalid row index (5) - allowed 0 to 0")
114
140
  end
115
141
 
116
142
  it "should insert a row" do
117
- @sample_table.insert_row( 1, [ 4, 5, 6 ] )
143
+ @sample_table.insert_row(1, [4, Cell.new(5), 6])
118
144
 
119
- @sample_table.data.should == [
120
- [ 1, 1.1, T_BIGDECIMAL ],
121
- [ 4, 5, 6 ],
122
- [ T_DATE, T_DATETIME, T_TIME ],
123
- [ true, 'a', nil ],
124
- ]
145
+ expect(@sample_table.data).to eq([
146
+ [1, 1.1, T_BIGDECIMAL],
147
+ [4, 5, 6],
148
+ [T_DATE, T_DATETIME, T_TIME],
149
+ [true, 'a', nil],
150
+ ])
125
151
 
126
152
  # illegal row index tested in separate UT
127
153
  end
@@ -129,139 +155,172 @@ describe SpreadBase::Table do
129
155
  it "should insert a row without error if there is no data" do
130
156
  @sample_table.data = []
131
157
 
132
- @sample_table.insert_row( 0, [ 4, 5 ] )
158
+ @sample_table.insert_row(0, [4, 5])
133
159
 
134
- @sample_table.data.size.should == 1
160
+ expect(@sample_table.data.size).to eq(1)
135
161
  end
136
162
 
137
163
  it "should append a row" do
138
- @sample_table.append_row( [ 4, 5, 6 ] )
139
-
140
- @sample_table.data.should == [
141
- [ 1, 1.1, T_BIGDECIMAL ],
142
- [ T_DATE, T_DATETIME, T_TIME ],
143
- [ true, 'a', nil ],
144
- [ 4, 5, 6 ],
145
- ]
164
+ @sample_table.append_row([4, Cell.new(5), 6])
165
+
166
+ expect(@sample_table.data).to eq([
167
+ [1, 1.1, T_BIGDECIMAL],
168
+ [T_DATE, T_DATETIME, T_TIME],
169
+ [true, 'a', nil],
170
+ [4, 5, 6],
171
+ ])
146
172
  end
147
173
 
148
174
  it "should access a column" do
149
- @sample_table.column( 0 ).should == [ 1, T_DATE, true ]
150
- @sample_table.column( 1 ).should == [ 1.1, T_DATETIME, 'a' ]
175
+ expect(@sample_table.column(0)).to eq([1, T_DATE, true])
176
+
177
+ expect(@sample_table.column(1, as_cell: true)).to eq([Cell.new(1.1), Cell.new(T_DATETIME), Cell.new('a')])
178
+
179
+ expect(@sample_table.column(3)).to eq([nil, nil, nil])
180
+ end
181
+
182
+ it "should access a set of columns by range" do
183
+ expect(@sample_table.column(0..1)).to eq([
184
+ [1, T_DATE, true],
185
+ [1.1, T_DATETIME, 'a'],
186
+ ])
151
187
 
152
- @sample_table.column( 3 ).should == [ nil, nil, nil ]
188
+ expect(@sample_table.column('C'..'D')).to eq([
189
+ [T_BIGDECIMAL, T_TIME, nil],
190
+ [nil, nil, nil],
191
+ ])
192
+ end
193
+
194
+ it "should access a set of columns by range (as cell )" do
195
+ expect(@sample_table.column(0..1, as_cell: true)).to eq([
196
+ [Cell.new(1), Cell.new(T_DATE), Cell.new(true)],
197
+ [Cell.new(1.1), Cell.new(T_DATETIME), Cell.new('a')],
198
+ ])
153
199
  end
154
200
 
155
201
  it "should delete a column" do
156
- @sample_table.column_width_styles = [ 'abc', nil, 'cde' ]
202
+ @sample_table.column_width_styles = ['abc', nil, 'cde']
157
203
 
158
- @sample_table.delete_column( 0 ).should == [ 1, T_DATE, true ]
204
+ expect(@sample_table.delete_column(0)).to eq([1, T_DATE, true])
159
205
 
160
- @sample_table.column_width_styles.should == [ nil, 'cde' ]
206
+ expect(@sample_table.column_width_styles).to eq([nil, 'cde'])
161
207
 
162
- @sample_table.delete_column( 3 ).should == [ nil, nil, nil ]
208
+ expect(@sample_table.delete_column(3)).to eq([nil, nil, nil])
163
209
 
164
- @sample_table.column_width_styles.should == [ nil, 'cde' ]
210
+ expect(@sample_table.column_width_styles).to eq([nil, 'cde'])
165
211
 
166
- @sample_table.data.should == [
167
- [ 1.1, T_BIGDECIMAL ],
168
- [ T_DATETIME, T_TIME ],
169
- [ 'a', nil ]
170
- ]
212
+ expect(@sample_table.data).to eq([
213
+ [1.1, T_BIGDECIMAL],
214
+ [T_DATETIME, T_TIME],
215
+ ['a', nil]
216
+ ])
217
+ end
218
+
219
+ it "should delete a set of columns by range" do
220
+ expect(@sample_table.delete_column(0..1)).to eq([
221
+ [1, T_DATE, true],
222
+ [1.1, T_DATETIME, 'a'],
223
+ ])
224
+
225
+ expect(@sample_table.data).to eq([
226
+ [T_BIGDECIMAL],
227
+ [T_TIME],
228
+ [nil],
229
+ ])
171
230
  end
172
231
 
173
232
  it "should insert a column" do
174
233
  # Setup/fill table
175
234
 
176
- @sample_table.column_width_styles = [ 'abc', nil, 'cde' ]
235
+ @sample_table.column_width_styles = ['abc', nil, 'cde']
177
236
 
178
- @sample_table.insert_column( 1, [ 34, 'abc', nil ] )
237
+ @sample_table.insert_column(1, [34, 'abc', Cell.new(nil)])
179
238
 
180
- @sample_table.data.should == [
181
- [ 1, 34, 1.1, T_BIGDECIMAL ],
182
- [ T_DATE, 'abc', T_DATETIME, T_TIME ],
183
- [ true, nil, 'a', nil ],
184
- ]
239
+ expect(@sample_table.data).to eq([
240
+ [1, 34, 1.1, T_BIGDECIMAL],
241
+ [T_DATE, 'abc', T_DATETIME, T_TIME],
242
+ [true, nil, 'a', nil],
243
+ ])
185
244
 
186
- @sample_table.column_width_styles = [ 'abc', nil, nil, 'cde' ]
245
+ @sample_table.column_width_styles = ['abc', nil, nil, 'cde']
187
246
 
188
247
  # Empty table
189
248
 
190
- table = SpreadBase::Table.new( 'abc' )
249
+ table = SpreadBase::Table.new('abc')
191
250
 
192
- table.insert_column( 0, [ 34, 'abc', 1 ] )
251
+ table.insert_column(0, [34, 'abc', 1])
193
252
 
194
- table.data.should == [
195
- [ 34, ],
196
- [ 'abc' ],
197
- [ 1 ],
198
- ]
253
+ expect(table.data).to eq([
254
+ [34,],
255
+ ['abc'],
256
+ [1],
257
+ ])
199
258
 
200
- @sample_table.column_width_styles = [ nil ]
259
+ @sample_table.column_width_styles = [nil]
201
260
  end
202
261
 
203
262
  it "should not insert a column if the size is not correct" do
204
- lambda { @sample_table.insert_column( 1, [ 34, 'abc' ] ) }.should raise_error( RuntimeError, "Inserting column size (2) different than existing columns size (3)" )
263
+ expect { @sample_table.insert_column(1, [34, 'abc']) }.to raise_error(RuntimeError, "Inserting column size (2) different than existing columns size (3)")
205
264
 
206
- @sample_table.data.first.size.should == 3
265
+ expect(@sample_table.data.first.size).to eq(3)
207
266
  end
208
267
 
209
268
  it "should insert a column outside the row boundaries" do
210
- @sample_table.insert_column( 5, [ 34, 'abc', nil ] )
269
+ @sample_table.insert_column(5, [34, 'abc', nil])
211
270
 
212
- @sample_table.data.should == [
213
- [ 1, 1.1, T_BIGDECIMAL, nil, nil, 34 ],
214
- [ T_DATE, T_DATETIME, T_TIME, nil, nil, 'abc' ],
215
- [ true, 'a', nil, nil, nil, nil ],
216
- ]
271
+ expect(@sample_table.data).to eq([
272
+ [1, 1.1, T_BIGDECIMAL, nil, nil, 34],
273
+ [T_DATE, T_DATETIME, T_TIME, nil, nil, 'abc'],
274
+ [true, 'a', nil, nil, nil, nil],
275
+ ])
217
276
  end
218
277
 
219
278
  it "should append a column" do
220
- table = SpreadBase::Table.new( 'abc' )
279
+ table = SpreadBase::Table.new('abc')
221
280
 
222
- table.append_column( [ 34, 'abc', 1 ] )
281
+ table.append_column([Cell.new(34), 'abc', 1])
223
282
 
224
- table.data.should == [
225
- [ 34, ],
226
- [ 'abc' ],
227
- [ 1 ],
228
- ]
283
+ expect(table.data).to eq([
284
+ [34,],
285
+ ['abc'],
286
+ [1],
287
+ ])
229
288
 
230
- table.append_column( [ 'cute', 'little', 'spielerin' ] )
289
+ table.append_column(['cute', 'little', 'spielerin'])
231
290
 
232
- table.data.should == [
233
- [ 34, 'cute' ],
234
- [ 'abc', 'little' ],
235
- [ 1, 'spielerin' ],
236
- ]
291
+ expect(table.data).to eq([
292
+ [34, 'cute'],
293
+ ['abc', 'little'],
294
+ [1, 'spielerin'],
295
+ ])
237
296
  end
238
297
 
239
298
  it "return the data as string (:to_s)" do
240
299
  expected_string = "\
241
300
  +------------+---------------------------+---------------------------+
242
- | 1 | 1.1 | 0.133E1 |
243
- | 2012-04-10 | 2012-04-11T23:33:42+00:00 | 2012-04-11 23:33:42 +0200 |
244
- | true | a | |
301
+ | 1 | 1.1 | 1.33 |
302
+ | 2012-04-10 | 2012-04-11 23:33:42 +0000 | 2012-04-11 23:33:42 +0200 |
303
+ | true | a | NIL |
245
304
  +------------+---------------------------+---------------------------+
246
305
  "
247
- @sample_table.to_s.should == expected_string
306
+ expect(@sample_table.to_s).to eq(expected_string)
248
307
  end
249
308
 
250
309
  it "return the data as string, with headers (:to_s)" do
251
310
  expected_string = "\
252
311
  +------------+---------------------------+---------------------------+
253
- | 1 | 1.1 | 0.133E1 |
312
+ | 1 | 1.1 | 1.33 |
254
313
  +------------+---------------------------+---------------------------+
255
- | 2012-04-10 | 2012-04-11T23:33:42+00:00 | 2012-04-11 23:33:42 +0200 |
256
- | true | a | |
314
+ | 2012-04-10 | 2012-04-11 23:33:42 +0000 | 2012-04-11 23:33:42 +0200 |
315
+ | true | a | NIL |
257
316
  +------------+---------------------------+---------------------------+
258
317
  "
259
318
 
260
- @sample_table.to_s( :with_headers => true ).should == expected_string
319
+ expect(@sample_table.to_s(with_headers: true)).to eq(expected_string)
261
320
 
262
321
  @sample_table.data = []
263
322
 
264
- @sample_table.to_s( :with_headers => true ).should == ""
323
+ expect(@sample_table.to_s(with_headers: true)).to eq("")
265
324
  end
266
325
 
267
326
  end