auom 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +13 -0
  4. data/Gemfile +10 -0
  5. data/Gemfile.devtools +52 -0
  6. data/Guardfile +8 -0
  7. data/LICENSE +20 -0
  8. data/README.md +92 -0
  9. data/Rakefile +5 -0
  10. data/TODO +3 -0
  11. data/auom.gemspec +21 -0
  12. data/config/flay.yml +3 -0
  13. data/config/flog.yml +2 -0
  14. data/config/heckle.yml +9 -0
  15. data/config/roodi.yml +20 -0
  16. data/config/site.reek +95 -0
  17. data/config/yardstick.yml +2 -0
  18. data/lib/auom.rb +287 -0
  19. data/lib/auom/algebra.rb +129 -0
  20. data/lib/auom/equalization.rb +90 -0
  21. data/lib/auom/inspection.rb +103 -0
  22. data/lib/auom/version.rb +3 -0
  23. data/spec/rcov.opts +7 -0
  24. data/spec/shared/command_method_behavior.rb +7 -0
  25. data/spec/shared/each_method_behaviour.rb +15 -0
  26. data/spec/shared/hash_method_behavior.rb +17 -0
  27. data/spec/shared/idempotent_method_behavior.rb +7 -0
  28. data/spec/shared/incompatible_operation_behavior.rb +5 -0
  29. data/spec/shared/invertible_method_behaviour.rb +9 -0
  30. data/spec/shared/operation_behavior.rb +12 -0
  31. data/spec/shared/sunits_shared.rb +6 -0
  32. data/spec/spec.opts +3 -0
  33. data/spec/spec_helper.rb +13 -0
  34. data/spec/unit/auom/add_spec.rb +0 -0
  35. data/spec/unit/auom/algebra/add_spec.rb +59 -0
  36. data/spec/unit/auom/algebra/divide_spec.rb +74 -0
  37. data/spec/unit/auom/algebra/multiply_spec.rb +74 -0
  38. data/spec/unit/auom/algebra/substract_spec.rb +59 -0
  39. data/spec/unit/auom/equalization/eql_spec.rb +40 -0
  40. data/spec/unit/auom/equalization/equal_value_spec.rb +57 -0
  41. data/spec/unit/auom/equalization/hash_spec.rb +13 -0
  42. data/spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb +27 -0
  43. data/spec/unit/auom/inspection/inspect_spec.rb +41 -0
  44. data/spec/unit/auom/unit/class_methods/convert_spec.rb +35 -0
  45. data/spec/unit/auom/unit/class_methods/lookup_spec.rb +21 -0
  46. data/spec/unit/auom/unit/class_methods/new_spec.rb +156 -0
  47. data/spec/unit/auom/unit/class_methods/try_convert_spec.rb +31 -0
  48. data/spec/unit/auom/unit/class_methods/units_spec.rb +11 -0
  49. data/spec/unit/auom/unit/denominators_spec.rb +16 -0
  50. data/spec/unit/auom/unit/numerators_spec.rb +16 -0
  51. data/spec/unit/auom/unit/scalar_spec.rb +16 -0
  52. data/spec/unit/auom/unit/unit_spec.rb +16 -0
  53. data/spec/unit/auom/unit/unitless_spec.rb +18 -0
  54. metadata +164 -0
File without changes
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Algebra,'#add' do
4
+ subject { object.add(operand) }
5
+
6
+ let(:object) { AUOM::Unit.new(*arguments) }
7
+
8
+ context 'when unit is unitless' do
9
+ let(:arguments) { [1] }
10
+
11
+ context 'and operand is a Fixnum' do
12
+ let(:operand) { 1 }
13
+
14
+ it_should_behave_like 'an operation'
15
+
16
+ it { should eql(AUOM::Unit.new(2)) }
17
+ end
18
+
19
+ context 'and operand is a unitless unit' do
20
+ let(:operand) { AUOM::Unit.new(1) }
21
+
22
+ it { should eql(AUOM::Unit.new(2)) }
23
+ end
24
+
25
+ context 'and operand is a unitful unit' do
26
+ let(:operand) { AUOM::Unit.new(1,:meter) }
27
+
28
+ it_should_behave_like 'an incompatible operation'
29
+ end
30
+ end
31
+
32
+ context 'when unit is unitful' do
33
+ let(:arguments) { [1,:meter] }
34
+
35
+ context 'and operand is a Fixnum' do
36
+ let(:operand) { 1 }
37
+
38
+ it_should_behave_like 'an incompatible operation'
39
+ end
40
+
41
+ context 'and operand is a unitless unit' do
42
+ let(:operand) { AUOM::Unit.new(1) }
43
+
44
+ it_should_behave_like 'an incompatible operation'
45
+ end
46
+
47
+ context 'and operand is a incompatible unit' do
48
+ let(:operand) { AUOM::Unit.new(1,:euro) }
49
+
50
+ it_should_behave_like 'an incompatible operation'
51
+ end
52
+
53
+ context 'and operand is a compatible unit' do
54
+ let(:operand) { AUOM::Unit.new(1,:meter) }
55
+
56
+ it { should eql(AUOM::Unit.new(2,:meter)) }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Algebra,'#divide' do
4
+ subject { object.divide(operand) }
5
+
6
+ let(:object) { AUOM::Unit.new(*arguments) }
7
+
8
+ context 'with unitless unit' do
9
+ let(:arguments) { [2] }
10
+
11
+ context 'when operand is a fixnum' do
12
+ let(:operand) { 1 }
13
+
14
+ it_should_behave_like 'an operation'
15
+
16
+ it { should eql(AUOM::Unit.new(2)) }
17
+ end
18
+
19
+ context 'when operand is a unitless unit' do
20
+ let(:operand) { AUOM::Unit.new(1) }
21
+
22
+ it_should_behave_like 'an operation'
23
+
24
+ it { should eql(AUOM::Unit.new(2)) }
25
+ end
26
+
27
+ context 'when operand is a unitful unit' do
28
+ let(:operand) { AUOM::Unit.new(1,:meter) }
29
+
30
+ it_should_behave_like 'an operation'
31
+
32
+ it { should eql(AUOM::Unit.new(2,[],:meter)) }
33
+ end
34
+ end
35
+
36
+ context 'with unitful unit' do
37
+ let(:arguments) { [2,:meter] }
38
+
39
+ context 'when operand is a fixnum' do
40
+ let(:operand) { 1 }
41
+
42
+ it_should_behave_like 'an operation'
43
+
44
+ it { should eql(AUOM::Unit.new(2,:meter)) }
45
+ end
46
+
47
+ context 'when operand is a unitless unit' do
48
+ let(:operand) { AUOM::Unit.new(1) }
49
+
50
+ it_should_behave_like 'an operation'
51
+
52
+ it { should eql(AUOM::Unit.new(2,:meter)) }
53
+ end
54
+
55
+ context 'when operand is a unitful unit' do
56
+
57
+ context 'and units NOT cancle each other' do
58
+ let(:operand) { AUOM::Unit.new(1,:euro) }
59
+
60
+ it_should_behave_like 'an operation'
61
+
62
+ it { should eql(AUOM::Unit.new(2,:meter,:euro)) }
63
+ end
64
+
65
+ context 'and untis cancle each other' do
66
+ let(:operand) { AUOM::Unit.new(1,:meter) }
67
+
68
+ it_should_behave_like 'an operation'
69
+
70
+ it { should eql(AUOM::Unit.new(2)) }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Algebra,'#multiply' do
4
+ subject { object.multiply(operand) }
5
+
6
+ let(:object) { AUOM::Unit.new(*arguments) }
7
+
8
+ context 'with unitless unit' do
9
+ let(:arguments) { [1] }
10
+
11
+ context 'when operand is a fixnum' do
12
+ let(:operand) { 2 }
13
+
14
+ it_should_behave_like 'an operation'
15
+
16
+ it { should eql(AUOM::Unit.new(2)) }
17
+ end
18
+
19
+ context 'when operand is a unitless unit' do
20
+ let(:operand) { AUOM::Unit.new(2) }
21
+
22
+ it_should_behave_like 'an operation'
23
+
24
+ it { should eql(AUOM::Unit.new(2)) }
25
+ end
26
+
27
+ context 'when operand is a unitful unit' do
28
+ let(:operand) { AUOM::Unit.new(2,:meter) }
29
+
30
+ it_should_behave_like 'an operation'
31
+
32
+ it { should eql(AUOM::Unit.new(2,:meter)) }
33
+ end
34
+ end
35
+
36
+ context 'with unitful unit' do
37
+ let(:arguments) { [1,:meter] }
38
+
39
+ context 'when operand is a fixnum' do
40
+ let(:operand) { 2 }
41
+
42
+ it_should_behave_like 'an operation'
43
+
44
+ it { should eql(AUOM::Unit.new(2,:meter)) }
45
+ end
46
+
47
+ context 'when operand is a unitless unit' do
48
+ let(:operand) { AUOM::Unit.new(2) }
49
+
50
+ it_should_behave_like 'an operation'
51
+
52
+ it { should eql(AUOM::Unit.new(2,:meter)) }
53
+ end
54
+
55
+ context 'when operand is a unitful unit' do
56
+
57
+ context 'and units NOT cancle each other' do
58
+ let(:operand) { AUOM::Unit.new(2,:meter) }
59
+
60
+ it_should_behave_like 'an operation'
61
+
62
+ it { should eql(AUOM::Unit.new(2,[:meter,:meter])) }
63
+ end
64
+
65
+ context 'and units cancle each other' do
66
+ let(:operand) { AUOM::Unit.new(2,[],:meter) }
67
+
68
+ it_should_behave_like 'an operation'
69
+
70
+ it { should eql(AUOM::Unit.new(2)) }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Algebra,'#substract' do
4
+ subject { object.substract(operand) }
5
+
6
+ let(:object) { AUOM::Unit.new(*arguments) }
7
+
8
+ context 'when unit is unitless' do
9
+ let(:arguments) { [1] }
10
+
11
+ context 'and operand is a Fixnum' do
12
+ let(:operand) { 1 }
13
+
14
+ it_should_behave_like 'an operation'
15
+
16
+ it { should eql(AUOM::Unit.new(0)) }
17
+ end
18
+
19
+ context 'and operand is a unitless unit' do
20
+ let(:operand) { AUOM::Unit.new(1) }
21
+
22
+ it { should eql(AUOM::Unit.new(0)) }
23
+ end
24
+
25
+ context 'and operand is a unitful unit' do
26
+ let(:operand) { AUOM::Unit.new(1,:meter) }
27
+
28
+ it_should_behave_like 'an incompatible operation'
29
+ end
30
+ end
31
+
32
+ context 'when unit is unitful' do
33
+ let(:arguments) { [1,:meter] }
34
+
35
+ context 'and operand is a Fixnum' do
36
+ let(:operand) { 1 }
37
+
38
+ it_should_behave_like 'an incompatible operation'
39
+ end
40
+
41
+ context 'and operand is a unitless unit' do
42
+ let(:operand) { AUOM::Unit.new(1) }
43
+
44
+ it_should_behave_like 'an incompatible operation'
45
+ end
46
+
47
+ context 'and operand is a incompatible unit' do
48
+ let(:operand) { AUOM::Unit.new(1,:euro) }
49
+
50
+ it_should_behave_like 'an incompatible operation'
51
+ end
52
+
53
+ context 'and operand is a compatible unit' do
54
+ let(:operand) { AUOM::Unit.new(1,:meter) }
55
+
56
+ it { should eql(AUOM::Unit.new(0,:meter)) }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Equalization,'#eql?' do
4
+ subject { object.eql?(other) }
5
+
6
+ let(:object) { AUOM::Unit.new(1,:meter) }
7
+
8
+ context 'when is other kind of object' do
9
+ let(:other) { Object.new }
10
+
11
+ it { should be(false) }
12
+ end
13
+
14
+ context 'when is same kind of object' do
15
+ let(:other) { AUOM::Unit.new(scalar,*unit) }
16
+
17
+ let(:scalar) { 1 }
18
+ let(:unit) { [:meter] }
19
+
20
+ context 'and scalar and unit are the same' do
21
+ it { should be(true) }
22
+ end
23
+
24
+ context 'and scalar is differend' do
25
+ let(:scalar) { 2 }
26
+ it { should be(false) }
27
+ end
28
+
29
+ context 'and unit is differend' do
30
+ let(:unit) { [:euro] }
31
+ it { should be(false) }
32
+ end
33
+
34
+ context 'and scalar and unit is differend' do
35
+ let(:scalar) { 2 }
36
+ let(:unit) { [:euro] }
37
+ it { should be(false) }
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Equalization,'#==' do
4
+ subject { object == other }
5
+
6
+ let(:object) { AUOM::Unit.new(1,:meter) }
7
+
8
+ context 'when is other kind of object' do
9
+
10
+ context 'that cannot be converted' do
11
+ let(:other) { Object.new }
12
+
13
+ it { should be(false) }
14
+ end
15
+
16
+ context 'that can be converted' do
17
+ let(:other) { 1 }
18
+
19
+ context 'and does not have the same values' do
20
+ it { should be(false) }
21
+ end
22
+
23
+ context 'and does have the same values' do
24
+ let(:other) { 1 }
25
+ let(:object) { AUOM::Unit.new(1) }
26
+ it { should be(true) }
27
+ end
28
+ end
29
+ end
30
+
31
+ context 'when is same kind of object' do
32
+ let(:other) { AUOM::Unit.new(scalar,*unit) }
33
+
34
+ let(:scalar) { 1 }
35
+ let(:unit) { [:meter] }
36
+
37
+ context 'and scalar and unit are the same' do
38
+ it { should be(true) }
39
+ end
40
+
41
+ context 'and scalar is differend' do
42
+ let(:scalar) { 2 }
43
+ it { should be(false) }
44
+ end
45
+
46
+ context 'and unit is differend' do
47
+ let(:unit) { [:euro] }
48
+ it { should be(false) }
49
+ end
50
+
51
+ context 'and scalar and unit is differend' do
52
+ let(:scalar) { 2 }
53
+ let(:unit) { [:euro] }
54
+ it { should be(false) }
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Equalization, '#hash' do
4
+ subject { object.hash }
5
+
6
+ let(:object) { AUOM::Unit.new(1) }
7
+
8
+ it { should be_a(Fixnum) }
9
+
10
+ it_should_behave_like 'an idempotent method'
11
+
12
+ it { should be(Rational(1).hash ^ [[],[]].hash) }
13
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Inspection,'.prettify_unit_part' do
4
+ subject { object.prettify_unit_part(part) }
5
+
6
+ let(:object) { AUOM::Inspection }
7
+
8
+ context 'with simple part' do
9
+ let(:part) { [:meter] }
10
+ it { should eql('meter') }
11
+ end
12
+
13
+ context 'with mixed part' do
14
+ let(:part) { [:meter,:euro] }
15
+ it { should eql('euro*meter') }
16
+ end
17
+
18
+ context 'with complex part' do
19
+ let(:part) { [:meter,:meter,:euro] }
20
+ it { should eql('meter^2*euro') }
21
+ end
22
+
23
+ context 'with very complex part' do
24
+ let(:part) { [:meter,:meter,:euro,:euro,:kilogramm] }
25
+ it { should eql('euro^2*meter^2*kilogramm') }
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Inspection,'#inspect' do
4
+ subject { object.inspect }
5
+
6
+ let(:object) { AUOM::Unit.new(scalar,*unit) }
7
+ let(:unit) { [] }
8
+
9
+ context 'when scalar is exact in decimal' do
10
+ let(:scalar) { 1 }
11
+
12
+ it { should eql("<AUOM::Unit @scalar=1>") }
13
+ end
14
+
15
+ context 'when scalar is NOT exact in decimal' do
16
+ let(:scalar) { Rational(1,3) }
17
+
18
+ it { should eql("<AUOM::Unit @scalar=~0.3333>") }
19
+ end
20
+
21
+ context 'when has only numerator' do
22
+ let(:scalar) { Rational(1,3) }
23
+ let(:unit) { [:meter] }
24
+
25
+ it { should eql("<AUOM::Unit @scalar=~0.3333 meter>") }
26
+ end
27
+
28
+ context 'when has only denominator' do
29
+ let(:scalar) { Rational(1,3) }
30
+ let(:unit) { [[],:meter] }
31
+
32
+ it { should eql("<AUOM::Unit @scalar=~0.3333 1/meter>") }
33
+ end
34
+
35
+ context 'when has numerator and denominator' do
36
+ let(:scalar) { Rational(1,3) }
37
+ let(:unit) { [:euro,:meter] }
38
+
39
+ it { should eql("<AUOM::Unit @scalar=~0.3333 euro/meter>") }
40
+ end
41
+ end