auom 0.2.0 → 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.
Files changed (52) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +18 -0
  3. data/.rubocop.yml +1 -1
  4. data/Changelog.md +5 -0
  5. data/Gemfile +2 -0
  6. data/Rakefile +2 -0
  7. data/auom.gemspec +6 -4
  8. data/config/devtools.yml +2 -0
  9. data/config/flay.yml +1 -1
  10. data/config/reek.yml +96 -97
  11. data/config/rubocop.yml +3 -0
  12. data/lib/auom.rb +2 -1
  13. data/lib/auom/algebra.rb +2 -0
  14. data/lib/auom/equalization.rb +2 -2
  15. data/lib/auom/inspection.rb +15 -10
  16. data/lib/auom/relational.rb +6 -76
  17. data/lib/auom/unit.rb +6 -8
  18. data/spec/shared/incompatible_operation_behavior.rb +2 -0
  19. data/spec/shared/operation_behavior.rb +3 -0
  20. data/spec/shared/sunits_shared.rb +2 -0
  21. data/spec/spec_helper.rb +2 -0
  22. data/spec/unit/auom/algebra_spec.rb +279 -0
  23. data/spec/unit/auom/{equalization/equality_operator_spec.rb → equalization_spec.rb} +2 -1
  24. data/spec/unit/auom/inspection_spec.rb +73 -0
  25. data/spec/unit/auom/relational_spec.rb +39 -0
  26. data/spec/unit/auom/unit_spec.rb +377 -0
  27. metadata +18 -57
  28. data/.circle.yml +0 -4
  29. data/TODO +0 -1
  30. data/spec/unit/auom/algebra/add_spec.rb +0 -57
  31. data/spec/unit/auom/algebra/divide_spec.rb +0 -83
  32. data/spec/unit/auom/algebra/multiply_spec.rb +0 -80
  33. data/spec/unit/auom/algebra/substract_spec.rb +0 -57
  34. data/spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb +0 -29
  35. data/spec/unit/auom/inspection/inspect_spec.rb +0 -39
  36. data/spec/unit/auom/relational/greater_than_or_equal_to_predicate_spec.rb +0 -38
  37. data/spec/unit/auom/relational/greater_than_predicate_spec.rb +0 -38
  38. data/spec/unit/auom/relational/less_than_or_equal_to_predicate_spec.rb +0 -38
  39. data/spec/unit/auom/relational/less_than_predicate_spec.rb +0 -38
  40. data/spec/unit/auom/unit/assert_same_unit_spec.rb +0 -20
  41. data/spec/unit/auom/unit/class_methods/convert_spec.rb +0 -33
  42. data/spec/unit/auom/unit/class_methods/lookup_spec.rb +0 -19
  43. data/spec/unit/auom/unit/class_methods/new_spec.rb +0 -152
  44. data/spec/unit/auom/unit/class_methods/try_convert_spec.rb +0 -35
  45. data/spec/unit/auom/unit/class_methods/units_spec.rb +0 -9
  46. data/spec/unit/auom/unit/denominators_spec.rb +0 -14
  47. data/spec/unit/auom/unit/numerators_spec.rb +0 -14
  48. data/spec/unit/auom/unit/same_unit_predicate_spec.rb +0 -20
  49. data/spec/unit/auom/unit/scalar_spec.rb +0 -14
  50. data/spec/unit/auom/unit/unit +0 -0
  51. data/spec/unit/auom/unit/unit_spec.rb +0 -14
  52. data/spec/unit/auom/unit/unitless_predicate_spec.rb +0 -22
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe AUOM::Relational do
4
+ describe '#<=>' do
5
+ subject { object <=> other }
6
+
7
+ let(:object) { AUOM::Unit.new(1, :meter) }
8
+ let(:other) { AUOM::Unit.new(scalar, unit) }
9
+
10
+ context 'when operand unit is the same' do
11
+ let(:unit) { :meter }
12
+
13
+ context 'and operand scalar is less than receiver scalar' do
14
+ let(:scalar) { 0 }
15
+
16
+ it { should be(1) }
17
+ end
18
+
19
+ context 'and operand scalar is equal to receiver scalar' do
20
+ let(:scalar) { 1 }
21
+
22
+ it { should be(0) }
23
+ end
24
+
25
+ context 'and operand scalar is greater than receiver scalar' do
26
+ let(:scalar) { 2 }
27
+
28
+ it { should be(-1) }
29
+ end
30
+ end
31
+
32
+ context 'when operand unit is not the same' do
33
+ let(:scalar) { 1 }
34
+ let(:unit) { :euro }
35
+
36
+ it_should_behave_like 'an incompatible operation'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,377 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe AUOM::Unit do
4
+ describe '#assert_same_unit' do
5
+ subject { object.assert_same_unit(other) }
6
+
7
+ let(:object) { described_class.new(1, *unit) }
8
+ let(:unit) { [%i[meter], %i[euro]] }
9
+
10
+ context 'when units are the same' do
11
+ let(:other) { AUOM::Unit.new(2, :meter, :euro) }
12
+
13
+ it { should be(object) }
14
+ end
15
+
16
+ context 'when units are not the same' do
17
+ let(:other) { AUOM::Unit.new(2, :meter) }
18
+
19
+ it_should_behave_like 'an incompatible operation'
20
+ end
21
+ end
22
+
23
+ describe '#denominators' do
24
+ subject { object.denominators }
25
+
26
+ let(:object) { described_class.new(1, *unit) }
27
+ let(:unit) { [[:meter], [:euro]] }
28
+
29
+ it 'should return denominators' do
30
+ should eql([:euro])
31
+ end
32
+
33
+ it { should be_frozen }
34
+
35
+ it_should_behave_like 'an idempotent method'
36
+ end
37
+
38
+ describe '#numerators' do
39
+ subject { object.numerators }
40
+
41
+ let(:object) { described_class.new(1, *unit) }
42
+ let(:unit) { [[:meter], [:euro]] }
43
+
44
+ it 'should return numerators' do
45
+ should eql([:meter])
46
+ end
47
+
48
+ it { should be_frozen }
49
+
50
+ it_should_behave_like 'an idempotent method'
51
+ end
52
+
53
+ describe '#same_unit?' do
54
+ subject { object.same_unit?(other) }
55
+
56
+ let(:object) { described_class.new(1, *unit) }
57
+ let(:unit) { [[:meter], [:euro]] }
58
+
59
+ context 'when units are the same' do
60
+ let(:other) { AUOM::Unit.new(2, :meter, :euro) }
61
+
62
+ it { should be(true) }
63
+ end
64
+
65
+ context 'when units are not the same' do
66
+ let(:other) { AUOM::Unit.new(2, :meter) }
67
+
68
+ it { should be(false) }
69
+ end
70
+ end
71
+
72
+ describe '#denominators' do
73
+ subject { object.scalar }
74
+
75
+ let(:object) { described_class.new(scalar) }
76
+ let(:scalar) { Rational(1, 2) }
77
+
78
+ it 'should return scalar' do
79
+ should eql(scalar)
80
+ end
81
+
82
+ it { should be_frozen }
83
+
84
+ it_should_behave_like 'an idempotent method'
85
+ end
86
+
87
+ describe '#unitless?' do
88
+ subject { object.unitless? }
89
+
90
+ let(:object) { described_class.new(1, *unit) }
91
+
92
+ context 'when unit is unitless' do
93
+ let(:unit) { [] }
94
+ it { should be(true) }
95
+ end
96
+
97
+ context 'when unit has no denominator' do
98
+ let(:unit) { [:meter] }
99
+
100
+ it { should be(false) }
101
+ end
102
+
103
+ context 'when unit has no numerator' do
104
+ let(:unit) { [[], :meter] }
105
+
106
+ it { should be(false) }
107
+ end
108
+ end
109
+
110
+ describe '#unit' do
111
+ subject { object.unit }
112
+
113
+ let(:object) { described_class.new(1, *unit) }
114
+ let(:unit) { [[:meter], [:euro]] }
115
+
116
+ it 'should return unit of unit instance' do
117
+ should eql(unit)
118
+ end
119
+
120
+ it { should be_frozen }
121
+
122
+ it_should_behave_like 'an idempotent method'
123
+ end
124
+
125
+ describe '.convert' do
126
+ subject { object.convert(value) }
127
+
128
+ let(:object) { AUOM::Unit }
129
+
130
+ context 'with nil' do
131
+ let(:value) { nil }
132
+
133
+ it 'should raise error' do
134
+ expect { subject }.to raise_error(ArgumentError, 'Cannot convert nil to AUOM::Unit')
135
+ end
136
+ end
137
+
138
+ context 'with fixnum' do
139
+ let(:value) { 1 }
140
+
141
+ it { should eql(AUOM::Unit.new(1)) }
142
+ end
143
+
144
+ context 'with rational' do
145
+ let(:value) { Rational(2, 1) }
146
+
147
+ it { should eql(AUOM::Unit.new(2)) }
148
+ end
149
+
150
+ context 'with Object' do
151
+ let(:value) { Object.new }
152
+
153
+ it 'should raise error' do
154
+ expect { subject }.to raise_error(ArgumentError, "Cannot convert #{value.inspect} to AUOM::Unit")
155
+ end
156
+ end
157
+ end
158
+
159
+ describe '.lookup' do
160
+ subject { object.__send__(:lookup, value) }
161
+
162
+ let(:object) { described_class }
163
+
164
+ context 'with existing symbol' do
165
+ let(:value) { :meter }
166
+
167
+ it { should eql([1, :meter]) }
168
+ end
169
+
170
+ context 'with inexistent symbol' do
171
+ let(:value) { :foo }
172
+
173
+ it 'should raise error' do
174
+ expect { subject }.to raise_error(ArgumentError, 'Unknown unit :foo')
175
+ end
176
+ end
177
+ end
178
+
179
+ describe '.new' do
180
+ let(:object) { described_class }
181
+
182
+ subject do
183
+ described_class.new(*arguments)
184
+ end
185
+
186
+ shared_examples_for 'invalid unit' do
187
+ it 'should raise ArgumentError' do
188
+ expect { subject }.to raise_error(ArgumentError)
189
+ end
190
+ end
191
+
192
+ let(:expected_scalar) { 1 }
193
+ let(:expected_numerators) { [] }
194
+ let(:expected_denominators) { [] }
195
+
196
+ shared_examples_for 'valid unit' do
197
+ it { should be_frozen }
198
+
199
+ its(:scalar) { should == expected_scalar }
200
+ its(:scalar) { should be_frozen }
201
+ its(:numerators) { should == expected_numerators }
202
+ its(:numerators) { should be_frozen }
203
+ its(:denominators) { should == expected_denominators }
204
+ its(:denominators) { should be_frozen }
205
+ end
206
+
207
+ describe 'without arguments' do
208
+ let(:arguments) { [] }
209
+ it_should_behave_like 'invalid unit'
210
+ end
211
+
212
+ describe 'with one scalar argument' do
213
+ let(:arguments) { [argument] }
214
+
215
+ context 'when scalar is a string' do
216
+ let(:argument) { '10.31' }
217
+ it 'should raise error' do
218
+ expect { subject }.to raise_error(ArgumentError, '"10.31" cannot be converted to rational')
219
+ end
220
+ end
221
+
222
+ context 'when argument is an Integer' do
223
+ let(:argument) { 1 }
224
+
225
+ it_should_behave_like 'unitless unit'
226
+ it_should_behave_like 'valid unit'
227
+ end
228
+
229
+ context 'when argument is a Rational' do
230
+ let(:argument) { Rational(1, 1) }
231
+
232
+ it_should_behave_like 'unitless unit'
233
+ it_should_behave_like 'valid unit'
234
+ end
235
+
236
+ context 'when argument is something else' do
237
+ let(:argument) { 1.0 }
238
+
239
+ it_should_behave_like 'invalid unit'
240
+ end
241
+ end
242
+
243
+ describe 'with scalar and numerator argument' do
244
+ let(:arguments) { [1, argument] }
245
+
246
+ context 'when argument is a valid unit' do
247
+ let(:argument) { :kilogramm }
248
+ let(:expected_numerators) { [:kilogramm] }
249
+
250
+ it_should_behave_like 'valid unit'
251
+ end
252
+
253
+ context 'when argument is a valid unit alias' do
254
+ let(:argument) { :kilometer }
255
+ let(:expected_numerators) { [:meter] }
256
+ let(:expected_scalar) { 1000 }
257
+
258
+ it_should_behave_like 'valid unit'
259
+ end
260
+
261
+ context 'when argument is an array of valid units' do
262
+ let(:argument) { %i[kilogramm meter] }
263
+ let(:expected_numerators) { %i[kilogramm meter] }
264
+
265
+ it_should_behave_like 'valid unit'
266
+ end
267
+
268
+ context 'when argument is an array with invalid unit' do
269
+ let(:argument) { %i[kilogramm nonsense] }
270
+
271
+ it_should_behave_like 'invalid unit'
272
+ end
273
+
274
+ context 'when argument is an invalid unit' do
275
+ let(:argument) { :nonsense }
276
+
277
+ it_should_behave_like 'invalid unit'
278
+ end
279
+ end
280
+
281
+ describe 'with scalar, numerator and denominator argument' do
282
+ let(:arguments) { [1, :kilogramm, argument] }
283
+ let(:expected_numerators) { %i[kilogramm] }
284
+
285
+ context 'when argument is a valid unit' do
286
+ let(:argument) { :meter }
287
+ let(:expected_denominators) { [:meter] }
288
+
289
+ it_should_behave_like 'valid unit'
290
+ end
291
+
292
+ context 'when argument is a valid unit alias' do
293
+ let(:argument) { :kilometer }
294
+
295
+ let(:expected_denominators) { [:meter] }
296
+ let(:expected_scalar) { Rational(1, 1000) }
297
+
298
+ it_should_behave_like 'valid unit'
299
+ end
300
+
301
+ context 'when argument is an array of valid units' do
302
+ let(:argument) { %i[euro meter] }
303
+ let(:expected_denominators) { %i[euro meter] }
304
+
305
+ it_should_behave_like 'valid unit'
306
+ end
307
+
308
+ context 'when argument is an array with invalid unit' do
309
+ let(:argument) { %i[euro nonsense] }
310
+
311
+ it_should_behave_like 'invalid unit'
312
+ end
313
+
314
+ context 'when argument is an invalid unit' do
315
+ let(:argument) { :nonsense }
316
+
317
+ it_should_behave_like 'invalid unit'
318
+ end
319
+ end
320
+
321
+ context 'when numerators and denominators overlap' do
322
+ let(:arguments) { [1, numerators, denominators] }
323
+ let(:numerators) { %i[kilogramm meter euro] }
324
+ let(:denominators) { %i[meter meter] }
325
+ let(:expected_numerators) { %i[euro kilogramm] }
326
+ let(:expected_denominators) { [:meter] }
327
+
328
+ it_should_behave_like 'valid unit'
329
+ end
330
+ end
331
+
332
+ describe '.try_convert' do
333
+ subject { object.try_convert(value) }
334
+
335
+ let(:object) { AUOM::Unit }
336
+
337
+ context 'with unit' do
338
+ let(:value) { AUOM::Unit.new(1) }
339
+
340
+ it { should be(value) }
341
+ end
342
+
343
+ context 'with nil' do
344
+ let(:value) { nil }
345
+
346
+ it { should be(nil) }
347
+ end
348
+
349
+ context 'with fixnum' do
350
+ let(:value) { 1 }
351
+
352
+ it { should eql(AUOM::Unit.new(1)) }
353
+ end
354
+
355
+ context 'with rational' do
356
+ let(:value) { Rational(2, 1) }
357
+
358
+ it { should eql(AUOM::Unit.new(2)) }
359
+ end
360
+
361
+ context 'with Object' do
362
+ let(:value) { Object.new }
363
+
364
+ it { should be(nil) }
365
+ end
366
+ end
367
+
368
+ describe '.units' do
369
+ subject { object.units }
370
+
371
+ let(:object) { described_class }
372
+
373
+ it { should be_a(Hash) }
374
+
375
+ it_should_behave_like 'an idempotent method'
376
+ end
377
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Markus Schirp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-29 00:00:00.000000000 Z
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: equalizer
@@ -30,23 +30,22 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.1.12
33
+ version: 0.1.21
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.1.12
40
+ version: 0.1.21
41
41
  description:
42
42
  email: mbj@schirp-dso.com
43
43
  executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files:
46
- - TODO
47
46
  - LICENSE
48
47
  files:
49
- - ".circle.yml"
48
+ - ".circleci/config.yml"
50
49
  - ".gitignore"
51
50
  - ".rspec"
52
51
  - ".rubocop.yml"
@@ -56,9 +55,9 @@ files:
56
55
  - LICENSE
57
56
  - README.md
58
57
  - Rakefile
59
- - TODO
60
58
  - auom.gemspec
61
59
  - circle.yml
60
+ - config/devtools.yml
62
61
  - config/flay.yml
63
62
  - config/flog.yml
64
63
  - config/mutant.yml
@@ -77,30 +76,11 @@ files:
77
76
  - spec/shared/operation_behavior.rb
78
77
  - spec/shared/sunits_shared.rb
79
78
  - spec/spec_helper.rb
80
- - spec/unit/auom/algebra/add_spec.rb
81
- - spec/unit/auom/algebra/divide_spec.rb
82
- - spec/unit/auom/algebra/multiply_spec.rb
83
- - spec/unit/auom/algebra/substract_spec.rb
84
- - spec/unit/auom/equalization/equality_operator_spec.rb
85
- - spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb
86
- - spec/unit/auom/inspection/inspect_spec.rb
87
- - spec/unit/auom/relational/greater_than_or_equal_to_predicate_spec.rb
88
- - spec/unit/auom/relational/greater_than_predicate_spec.rb
89
- - spec/unit/auom/relational/less_than_or_equal_to_predicate_spec.rb
90
- - spec/unit/auom/relational/less_than_predicate_spec.rb
91
- - spec/unit/auom/unit/assert_same_unit_spec.rb
92
- - spec/unit/auom/unit/class_methods/convert_spec.rb
93
- - spec/unit/auom/unit/class_methods/lookup_spec.rb
94
- - spec/unit/auom/unit/class_methods/new_spec.rb
95
- - spec/unit/auom/unit/class_methods/try_convert_spec.rb
96
- - spec/unit/auom/unit/class_methods/units_spec.rb
97
- - spec/unit/auom/unit/denominators_spec.rb
98
- - spec/unit/auom/unit/numerators_spec.rb
99
- - spec/unit/auom/unit/same_unit_predicate_spec.rb
100
- - spec/unit/auom/unit/scalar_spec.rb
101
- - spec/unit/auom/unit/unit
102
- - spec/unit/auom/unit/unit_spec.rb
103
- - spec/unit/auom/unit/unitless_predicate_spec.rb
79
+ - spec/unit/auom/algebra_spec.rb
80
+ - spec/unit/auom/equalization_spec.rb
81
+ - spec/unit/auom/inspection_spec.rb
82
+ - spec/unit/auom/relational_spec.rb
83
+ - spec/unit/auom/unit_spec.rb
104
84
  homepage: http://github.com/mbj/auom
105
85
  licenses: []
106
86
  metadata: {}
@@ -112,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
112
92
  requirements:
113
93
  - - ">="
114
94
  - !ruby/object:Gem::Version
115
- version: '2.4'
95
+ version: '2.5'
116
96
  required_rubygems_version: !ruby/object:Gem::Requirement
117
97
  requirements:
118
98
  - - ">="
@@ -120,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
100
  version: '0'
121
101
  requirements: []
122
102
  rubyforge_project:
123
- rubygems_version: 2.6.11
103
+ rubygems_version: 2.7.6
124
104
  signing_key:
125
105
  specification_version: 4
126
106
  summary: Algebra (with) Units Of Measurement
@@ -130,27 +110,8 @@ test_files:
130
110
  - spec/shared/operation_behavior.rb
131
111
  - spec/shared/sunits_shared.rb
132
112
  - spec/spec_helper.rb
133
- - spec/unit/auom/algebra/add_spec.rb
134
- - spec/unit/auom/algebra/divide_spec.rb
135
- - spec/unit/auom/algebra/multiply_spec.rb
136
- - spec/unit/auom/algebra/substract_spec.rb
137
- - spec/unit/auom/equalization/equality_operator_spec.rb
138
- - spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb
139
- - spec/unit/auom/inspection/inspect_spec.rb
140
- - spec/unit/auom/relational/greater_than_or_equal_to_predicate_spec.rb
141
- - spec/unit/auom/relational/greater_than_predicate_spec.rb
142
- - spec/unit/auom/relational/less_than_or_equal_to_predicate_spec.rb
143
- - spec/unit/auom/relational/less_than_predicate_spec.rb
144
- - spec/unit/auom/unit/assert_same_unit_spec.rb
145
- - spec/unit/auom/unit/class_methods/convert_spec.rb
146
- - spec/unit/auom/unit/class_methods/lookup_spec.rb
147
- - spec/unit/auom/unit/class_methods/new_spec.rb
148
- - spec/unit/auom/unit/class_methods/try_convert_spec.rb
149
- - spec/unit/auom/unit/class_methods/units_spec.rb
150
- - spec/unit/auom/unit/denominators_spec.rb
151
- - spec/unit/auom/unit/numerators_spec.rb
152
- - spec/unit/auom/unit/same_unit_predicate_spec.rb
153
- - spec/unit/auom/unit/scalar_spec.rb
154
- - spec/unit/auom/unit/unit
155
- - spec/unit/auom/unit/unit_spec.rb
156
- - spec/unit/auom/unit/unitless_predicate_spec.rb
113
+ - spec/unit/auom/algebra_spec.rb
114
+ - spec/unit/auom/equalization_spec.rb
115
+ - spec/unit/auom/inspection_spec.rb
116
+ - spec/unit/auom/relational_spec.rb
117
+ - spec/unit/auom/unit_spec.rb