ruby-measurement 1.2.2 → 1.2.3

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