ruby-measurement 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +4 -0
- data/CHANGELOG.md +21 -0
- data/README.md +2 -0
- data/Rakefile +3 -0
- data/lib/ruby-measurement/definitions/metric.rb +1 -0
- data/lib/ruby-measurement/definitions/metric/capacity.rb +12 -82
- data/lib/ruby-measurement/definitions/metric/volume.rb +82 -12
- data/lib/ruby-measurement/definitions/us_customary.rb +1 -0
- data/lib/ruby-measurement/definitions/us_customary/capacity.rb +29 -0
- data/lib/ruby-measurement/definitions/us_customary/length.rb +0 -20
- data/lib/ruby-measurement/definitions/us_customary/weight.rb +6 -6
- data/lib/ruby-measurement/measurement.rb +5 -14
- data/lib/ruby-measurement/unit.rb +52 -11
- data/lib/ruby-measurement/version.rb +1 -1
- data/ruby-measurement.gemspec +1 -0
- data/spec/ruby-measurement/definitions/metric/area_spec.rb +69 -0
- data/spec/ruby-measurement/definitions/metric/capacity_spec.rb +41 -0
- data/spec/ruby-measurement/definitions/metric/length_spec.rb +261 -0
- data/spec/ruby-measurement/definitions/metric/volume_spec.rb +261 -0
- data/spec/ruby-measurement/definitions/metric/weight_spec.rb +325 -0
- data/spec/ruby-measurement/definitions/us_customary/area_spec.rb +149 -0
- data/spec/ruby-measurement/definitions/us_customary/capacity_spec.rb +69 -0
- data/spec/ruby-measurement/definitions/us_customary/length_spec.rb +261 -0
- data/spec/ruby-measurement/definitions/us_customary/volume_spec.rb +201 -0
- data/spec/ruby-measurement/definitions/us_customary/weight_spec.rb +149 -0
- data/spec/ruby-measurement/measurement_spec.rb +342 -0
- data/spec/ruby-measurement/unit_builder_spec.rb +102 -0
- data/spec/ruby-measurement/unit_spec.rb +120 -0
- data/spec/spec_helper.rb +5 -0
- data/tasks/rspec.rake +6 -0
- metadata +48 -5
@@ -0,0 +1,201 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Measurement do
|
6
|
+
describe 'gallons' do
|
7
|
+
subject { Measurement.parse('1 gal') }
|
8
|
+
|
9
|
+
it 'converts to quarts' do
|
10
|
+
subject.convert_to(:qt).quantity.should eq 4
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'converts to pints' do
|
14
|
+
subject.convert_to(:pt).quantity.should eq 8
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'converts to cups' do
|
18
|
+
subject.convert_to(:c).quantity.should eq 16
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'converts to fluid ounces' do
|
22
|
+
subject.convert_to(:'fl oz').quantity.should eq 128
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'converts to tablespoons' do
|
26
|
+
subject.convert_to(:tbsp).quantity.should eq 256
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'converts to teaspoons' do
|
30
|
+
subject.convert_to(:tsp).quantity.should eq 768
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'quarts' do
|
35
|
+
subject { Measurement.parse('4 qt') }
|
36
|
+
|
37
|
+
it 'converts to gallons' do
|
38
|
+
subject.convert_to(:gal).quantity.should eq 1
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'converts to pints' do
|
42
|
+
subject.convert_to(:pt).quantity.should eq 8
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'converts to cups' do
|
46
|
+
subject.convert_to(:c).quantity.should eq 16
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'converts to fluid ounces' do
|
50
|
+
subject.convert_to(:'fl oz').quantity.should eq 128
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'converts to tablespoons' do
|
54
|
+
subject.convert_to(:tbsp).quantity.should eq 256
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'converts to teaspoons' do
|
58
|
+
subject.convert_to(:tsp).quantity.should eq 768
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'pints' do
|
63
|
+
subject { Measurement.parse('8 pt') }
|
64
|
+
|
65
|
+
it 'converts to gallons' do
|
66
|
+
subject.convert_to(:gal).quantity.should eq 1
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'converts to quarts' do
|
70
|
+
subject.convert_to(:qt).quantity.should eq 4
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'converts to cups' do
|
74
|
+
subject.convert_to(:c).quantity.should eq 16
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'converts to fluid ounces' do
|
78
|
+
subject.convert_to(:'fl oz').quantity.should eq 128
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'converts to tablespoons' do
|
82
|
+
subject.convert_to(:tbsp).quantity.should eq 256
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'converts to teaspoons' do
|
86
|
+
subject.convert_to(:tsp).quantity.should eq 768
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe 'cups' do
|
91
|
+
subject { Measurement.parse('16 c') }
|
92
|
+
|
93
|
+
it 'converts to gallons' do
|
94
|
+
subject.convert_to(:gal).quantity.should eq 1
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'converts to quarts' do
|
98
|
+
subject.convert_to(:qt).quantity.should eq 4
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'converts to pints' do
|
102
|
+
subject.convert_to(:pt).quantity.should eq 8
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'converts to fluid ounces' do
|
106
|
+
subject.convert_to(:'fl oz').quantity.should eq 128
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'converts to tablespoons' do
|
110
|
+
subject.convert_to(:tbsp).quantity.should eq 256
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'converts to teaspoons' do
|
114
|
+
subject.convert_to(:tsp).quantity.should eq 768
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe 'fluid ounces' do
|
119
|
+
subject { Measurement.parse('128 fl oz') }
|
120
|
+
|
121
|
+
it 'converts to gallons' do
|
122
|
+
subject.convert_to(:gal).quantity.should eq 1
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'converts to quarts' do
|
126
|
+
subject.convert_to(:qt).quantity.should eq 4
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'converts to pints' do
|
130
|
+
subject.convert_to(:pt).quantity.should eq 8
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'converts to cups' do
|
134
|
+
subject.convert_to(:c).quantity.should eq 16
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'converts to tablespoons' do
|
138
|
+
subject.convert_to(:tbsp).quantity.should eq 256
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'converts to teaspoons' do
|
142
|
+
subject.convert_to(:tsp).quantity.should eq 768
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe 'tablespoons' do
|
147
|
+
subject { Measurement.parse('256 tbsp') }
|
148
|
+
|
149
|
+
it 'converts to gallons' do
|
150
|
+
subject.convert_to(:gal).quantity.should eq 1
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'converts to quarts' do
|
154
|
+
subject.convert_to(:qt).quantity.should eq 4
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'converts to pints' do
|
158
|
+
subject.convert_to(:pt).quantity.should eq 8
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'converts to cups' do
|
162
|
+
subject.convert_to(:c).quantity.should eq 16
|
163
|
+
end
|
164
|
+
|
165
|
+
it 'converts to fluid ounces' do
|
166
|
+
subject.convert_to(:'fl oz').quantity.should eq 128
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'converts to teaspoons' do
|
170
|
+
subject.convert_to(:tsp).quantity.should eq 768
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe 'teaspoons' do
|
175
|
+
subject { Measurement.parse('768 tsp') }
|
176
|
+
|
177
|
+
it 'converts to gallons' do
|
178
|
+
subject.convert_to(:gal).quantity.should eq 1
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'converts to quarts' do
|
182
|
+
subject.convert_to(:qt).quantity.should eq 4
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'converts to pints' do
|
186
|
+
subject.convert_to(:pt).quantity.should eq 8
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'converts to cups' do
|
190
|
+
subject.convert_to(:c).quantity.should eq 16
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'converts to fluid ounces' do
|
194
|
+
subject.convert_to(:'fl oz').quantity.should eq 128
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'converts to tablespoons' do
|
198
|
+
subject.convert_to(:tbsp).quantity.should eq 256
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Measurement do
|
6
|
+
describe 'tons' do
|
7
|
+
subject { Measurement.parse('1 ton') }
|
8
|
+
|
9
|
+
it 'converts to hundredweights' do
|
10
|
+
subject.convert_to(:cwt).quantity.should eq 20
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'converts to pounds' do
|
14
|
+
subject.convert_to(:lbs).quantity.should eq 2_000
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'converts to ounces' do
|
18
|
+
subject.convert_to(:oz).quantity.should eq 32_000
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'converts to drams' do
|
22
|
+
subject.convert_to(:dr).quantity.should eq 512_000
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'converts to grains' do
|
26
|
+
subject.convert_to(:gr).quantity.should eq 14_000_000
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'hundredweights' do
|
31
|
+
subject { Measurement.parse('20 cwt') }
|
32
|
+
|
33
|
+
it 'converts to tons' do
|
34
|
+
subject.convert_to(:ton).quantity.should eq 1
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'converts to pounds' do
|
38
|
+
subject.convert_to(:lbs).quantity.should eq 2_000
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'converts to ounces' do
|
42
|
+
subject.convert_to(:oz).quantity.should eq 32_000
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'converts to drams' do
|
46
|
+
subject.convert_to(:dr).quantity.should eq 512_000
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'converts to grains' do
|
50
|
+
subject.convert_to(:gr).quantity.should eq 14_000_000
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'pounds' do
|
55
|
+
subject { Measurement.parse('2000 lbs') }
|
56
|
+
|
57
|
+
it 'converts to tons' do
|
58
|
+
subject.convert_to(:ton).quantity.should eq 1
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'converts to hundredweights' do
|
62
|
+
subject.convert_to(:cwt).quantity.should eq 20
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'converts to ounces' do
|
66
|
+
subject.convert_to(:oz).quantity.should eq 32_000
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'converts to drams' do
|
70
|
+
subject.convert_to(:dr).quantity.should eq 512_000
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'converts to grains' do
|
74
|
+
subject.convert_to(:gr).quantity.should eq 14_000_000
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe 'ounces' do
|
79
|
+
subject { Measurement.parse('32000 oz') }
|
80
|
+
|
81
|
+
it 'converts to tons' do
|
82
|
+
subject.convert_to(:ton).quantity.should eq 1
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'converts to hundredweights' do
|
86
|
+
subject.convert_to(:cwt).quantity.should eq 20
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'converts to pounds' do
|
90
|
+
subject.convert_to(:lbs).quantity.should eq 2_000
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'converts to drams' do
|
94
|
+
subject.convert_to(:dr).quantity.should eq 512_000
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'converts to grains' do
|
98
|
+
subject.convert_to(:gr).quantity.should eq 14_000_000
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'drams' do
|
103
|
+
subject { Measurement.parse('512000 dr') }
|
104
|
+
|
105
|
+
it 'converts to tons' do
|
106
|
+
subject.convert_to(:ton).quantity.should eq 1
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'converts to hundredweights' do
|
110
|
+
subject.convert_to(:cwt).quantity.should eq 20
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'converts to pounds' do
|
114
|
+
subject.convert_to(:lbs).quantity.should eq 2_000
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'converts to ounces' do
|
118
|
+
subject.convert_to(:oz).quantity.should eq 32_000
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'converts to grains' do
|
122
|
+
subject.convert_to(:gr).quantity.should eq 14_000_000
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe 'grains' do
|
127
|
+
subject { Measurement.parse('14000000 gr') }
|
128
|
+
|
129
|
+
it 'converts to tons' do
|
130
|
+
subject.convert_to(:ton).quantity.should eq 1
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'converts to hundredweights' do
|
134
|
+
subject.convert_to(:cwt).quantity.should eq 20
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'converts to pounds' do
|
138
|
+
subject.convert_to(:lbs).quantity.should eq 2_000
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'converts to ounces' do
|
142
|
+
subject.convert_to(:oz).quantity.should eq 32_000
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'converts to drams' do
|
146
|
+
subject.convert_to(:dr).quantity.should eq 512_000
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,342 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Measurement do
|
4
|
+
subject { Measurement }
|
5
|
+
|
6
|
+
describe '.new' do
|
7
|
+
describe 'with valid quantity' do
|
8
|
+
it 'sets the quantity' do
|
9
|
+
subject.new(3).quantity.should eq 3
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sets the default unit' do
|
13
|
+
subject.new(3).unit.should eq subject::Unit[:count]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'with valid quantity and unit name' do
|
18
|
+
it 'sets the quantity' do
|
19
|
+
subject.new(2, :dozen).quantity.should eq 2
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'sets the unit' do
|
23
|
+
subject.new(2, :dozen).unit.should eq subject::Unit[:dozen]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'with valid quantity and unit' do
|
28
|
+
it 'sets the quantity' do
|
29
|
+
subject.new(2, subject::Unit[:dozen]).quantity.should eq 2
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'sets the unit' do
|
33
|
+
subject.new(2, subject::Unit[:dozen]).unit.should eq subject::Unit[:dozen]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'with invalid quantity' do
|
38
|
+
it 'raises exception' do
|
39
|
+
expect { subject.new('hi') }.to raise_exception
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'with invalid unit' do
|
44
|
+
it 'raises exception' do
|
45
|
+
expect { subject.new(3, :finklebaum) }.to raise_exception
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '.parse' do
|
51
|
+
describe 'quantity' do
|
52
|
+
it 'parses scientific notation' do
|
53
|
+
m = subject.parse('4.3e12')
|
54
|
+
m.quantity.should eq 4.3e12
|
55
|
+
m.unit.should eq subject::Unit[:count]
|
56
|
+
|
57
|
+
m = subject.parse('4.3e12 dozen')
|
58
|
+
m.quantity.should eq 4.3e12
|
59
|
+
m.unit.should eq subject::Unit[:dozen]
|
60
|
+
|
61
|
+
m = subject.parse('4.3e12doz')
|
62
|
+
m.quantity.should eq 4.3e12
|
63
|
+
m.unit.should eq subject::Unit[:dozen]
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'parses fractions' do
|
67
|
+
m = subject.parse('1/4')
|
68
|
+
m.quantity.should eq 0.25
|
69
|
+
m.unit.should eq subject::Unit[:count]
|
70
|
+
|
71
|
+
m = subject.parse('1/4 dozen')
|
72
|
+
m.quantity.should eq 0.25
|
73
|
+
m.unit.should eq subject::Unit[:dozen]
|
74
|
+
|
75
|
+
m = subject.parse('1/4doz')
|
76
|
+
m.quantity.should eq 0.25
|
77
|
+
m.unit.should eq subject::Unit[:dozen]
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'parses mixed fractions' do
|
81
|
+
m = subject.parse('3 2/5')
|
82
|
+
m.quantity.should eq 3.4
|
83
|
+
m.unit.should eq subject::Unit[:count]
|
84
|
+
|
85
|
+
m = subject.parse('3 2/5 dozen')
|
86
|
+
m.quantity.should eq 3.4
|
87
|
+
m.unit.should eq subject::Unit[:dozen]
|
88
|
+
|
89
|
+
m = subject.parse('3 2/5')
|
90
|
+
m.quantity.should eq 3.4
|
91
|
+
m.unit.should eq subject::Unit[:count]
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'parses decimals' do
|
95
|
+
m = subject.parse('2.1')
|
96
|
+
m.quantity.should eq 2.1
|
97
|
+
m.unit.should eq subject::Unit[:count]
|
98
|
+
|
99
|
+
m = subject.parse('2.1 dozen')
|
100
|
+
m.quantity.should eq 2.1
|
101
|
+
m.unit.should eq subject::Unit[:dozen]
|
102
|
+
|
103
|
+
m = subject.parse('2.1doz')
|
104
|
+
m.quantity.should eq 2.1
|
105
|
+
m.unit.should eq subject::Unit[:dozen]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe 'unit' do
|
110
|
+
it 'converts when defined' do
|
111
|
+
subject.parse('3 dozen').unit.should eq subject::Unit[:dozen]
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'raises exception when undefined' do
|
115
|
+
expect { subject.parse('3 finklebaums') }.to raise_error
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '.define' do
|
121
|
+
it 'delegates to Unit.define' do
|
122
|
+
subject::Unit.should_receive(:define)
|
123
|
+
subject.define(:something)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
describe '#convert_to' do
|
128
|
+
let(:measurement) { subject.new(3) }
|
129
|
+
|
130
|
+
it 'returns copy of self when target unit matches current unit' do
|
131
|
+
result = measurement.convert_to(:count)
|
132
|
+
result.should_not be measurement
|
133
|
+
result.should eq measurement
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'returns target unit if it exists and is convertable' do
|
137
|
+
result = measurement.convert_to(:dozen)
|
138
|
+
result.quantity.should eq 0.25
|
139
|
+
result.unit.should eq subject::Unit[:dozen]
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'raises exception if unit exists and is not convertable' do
|
143
|
+
expect { measurement.convert_to(:inches) }.to raise_error
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'raises exception if unit does not exist' do
|
147
|
+
expect { measurement.convert_to(:finklebaum) }.to raise_error
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe '#convert_to!' do
|
152
|
+
let(:measurement) { subject.new(3) }
|
153
|
+
|
154
|
+
it 'modifies the object' do
|
155
|
+
measurement.convert_to!(:dozen)
|
156
|
+
measurement.quantity.should eq 0.25
|
157
|
+
measurement.unit.should eq subject::Unit[:dozen]
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe '#+' do
|
162
|
+
let(:measurement) { subject.new(3) }
|
163
|
+
|
164
|
+
it 'adds numeric values' do
|
165
|
+
result = measurement + 4
|
166
|
+
result.quantity.should eq 7
|
167
|
+
result.unit.should eq measurement.unit
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'adds units of the same type' do
|
171
|
+
other = subject.new(4)
|
172
|
+
other.unit.should eq measurement.unit
|
173
|
+
|
174
|
+
result = measurement + other
|
175
|
+
result.quantity.should eq 7
|
176
|
+
result.unit.should eq measurement.unit
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'adds units of a convertable type' do
|
180
|
+
other = subject.new(2, :dozen)
|
181
|
+
other.unit.should_not eq measurement.unit
|
182
|
+
|
183
|
+
result = measurement + other
|
184
|
+
result.quantity.should eq 27
|
185
|
+
result.unit.should eq measurement.unit
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'raises exception for incompatible units' do
|
189
|
+
other = subject.new(4, :inches)
|
190
|
+
other.unit.should_not eq measurement.unit
|
191
|
+
expect { measurement + other }.to raise_error
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe '#-' do
|
196
|
+
let(:measurement) { subject.new(3) }
|
197
|
+
|
198
|
+
it 'subtracts numeric values' do
|
199
|
+
result = measurement - 4
|
200
|
+
result.quantity.should eq -1
|
201
|
+
result.unit.should eq measurement.unit
|
202
|
+
end
|
203
|
+
|
204
|
+
it 'subtracts units of the same type' do
|
205
|
+
other = subject.new(4)
|
206
|
+
other.unit.should eq measurement.unit
|
207
|
+
|
208
|
+
result = measurement - other
|
209
|
+
result.quantity.should eq -1
|
210
|
+
result.unit.should eq measurement.unit
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'subtracts units of a convertable type' do
|
214
|
+
other = subject.new(2, :dozen)
|
215
|
+
other.unit.should_not eq measurement.unit
|
216
|
+
|
217
|
+
result = measurement - other
|
218
|
+
result.quantity.should eq -21
|
219
|
+
result.unit.should eq measurement.unit
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'raises exception for incompatible units' do
|
223
|
+
other = subject.new(4, :inches)
|
224
|
+
other.unit.should_not eq measurement.unit
|
225
|
+
expect { measurement - other }.to raise_error
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe '#*' do
|
230
|
+
let(:measurement) { subject.new(3) }
|
231
|
+
|
232
|
+
it 'multiplies numeric values' do
|
233
|
+
result = measurement * 4
|
234
|
+
result.quantity.should eq 12
|
235
|
+
result.unit.should eq measurement.unit
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'multiplies units of the same type' do
|
239
|
+
other = subject.new(4)
|
240
|
+
other.unit.should eq measurement.unit
|
241
|
+
|
242
|
+
result = measurement * other
|
243
|
+
result.quantity.should eq 12
|
244
|
+
result.unit.should eq measurement.unit
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'multiplies units of a convertable type' do
|
248
|
+
other = subject.new(2, :dozen)
|
249
|
+
other.unit.should_not eq measurement.unit
|
250
|
+
|
251
|
+
result = measurement * other
|
252
|
+
result.quantity.should eq 72
|
253
|
+
result.unit.should eq measurement.unit
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'raises exception for incompatible units' do
|
257
|
+
other = subject.new(4, :inches)
|
258
|
+
other.unit.should_not eq measurement.unit
|
259
|
+
expect { measurement * other }.to raise_error
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe '#/' do
|
264
|
+
let(:measurement) { subject.new(12) }
|
265
|
+
|
266
|
+
it 'divides numeric values' do
|
267
|
+
result = measurement / 4
|
268
|
+
result.quantity.should eq 3
|
269
|
+
result.unit.should eq measurement.unit
|
270
|
+
end
|
271
|
+
|
272
|
+
it 'divides units of the same type' do
|
273
|
+
other = subject.new(4)
|
274
|
+
other.unit.should eq measurement.unit
|
275
|
+
|
276
|
+
result = measurement / other
|
277
|
+
result.quantity.should eq 3
|
278
|
+
result.unit.should eq measurement.unit
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'divides units of a convertable type' do
|
282
|
+
other = subject.new(1, :dozen)
|
283
|
+
other.unit.should_not eq measurement.unit
|
284
|
+
|
285
|
+
result = measurement / other
|
286
|
+
result.quantity.should eq 1
|
287
|
+
result.unit.should eq measurement.unit
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'raises exception for incompatible units' do
|
291
|
+
other = subject.new(4, :inches)
|
292
|
+
other.unit.should_not eq measurement.unit
|
293
|
+
expect { measurement / other }.to raise_error
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
describe '#**' do
|
298
|
+
let(:measurement) { subject.new(3) }
|
299
|
+
|
300
|
+
it 'raises to the power of numeric values' do
|
301
|
+
(measurement ** 3).quantity.should eq 27
|
302
|
+
end
|
303
|
+
|
304
|
+
it 'raises exception for non-numeric values' do
|
305
|
+
expect { measurement ** subject.new(3) }.to raise_error
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
describe '#==' do
|
310
|
+
let(:measurement) { subject.new(3) }
|
311
|
+
|
312
|
+
it 'returns true for measurements with same quantity and unit' do
|
313
|
+
(measurement == subject.new(3)).should be_true
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'returns false for measurements with same quantity and different unit' do
|
317
|
+
(measurement == subject.new(3, :dozen)).should be_false
|
318
|
+
end
|
319
|
+
|
320
|
+
it 'returns false for measurements with same unit and different quantity' do
|
321
|
+
(measurement == subject.new(4)).should be_false
|
322
|
+
end
|
323
|
+
|
324
|
+
it 'returns false for non-measurement objects' do
|
325
|
+
(measurement == 3).should be_false
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
describe '#to_s' do
|
330
|
+
it 'returns the quantity and unit' do
|
331
|
+
subject.new(3.5).to_s.should eq '3.5 count'
|
332
|
+
subject.new(2, :dozen).to_s.should eq '2 doz'
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
describe '#inspect' do
|
337
|
+
it 'returns the quantity and unit' do
|
338
|
+
subject.new(3.5).inspect.should eq '3.5 count'
|
339
|
+
subject.new(2, :dozen).inspect.should eq '2 doz'
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|