This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
units_spec.rb
123 lines (106 loc) · 3.37 KB
/
units_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require "#{File.dirname(__FILE__)}/spec_helper"
class Float
def near?(rhs, prec = 0.01)
self.between?(rhs - prec, rhs + prec)
end
end
describe Beerxml::Unit do
it "should allow constructing with base units" do
w = U('153.27 kg')
w.unit.should == 'kg'
w.is?('kg').should be_true
w.is?('kilograms').should be_true
w.is?('g').should be_false
w.to_f.should == 153.27
w.should == 153.27
w.should == U(153.27, 'kg')
w.should == U('153.27', 'kg')
end
it "should reject unknown units" do
proc { U('153') }.should raise_error(ArgumentError)
proc { U('153 glorbs') }.should raise_error(ArgumentError)
proc { U(153, 'glorbs') }.should raise_error(ArgumentError)
end
it "should require an amount" do
proc { U('kg') }.should raise_error(ArgumentError)
proc { U('hai kg') }.should raise_error(ArgumentError)
proc { U('hai', 'kg') }.should raise_error(ArgumentError)
end
it "should allow symbols for units" do
U(2, :kg).should == U(2, 'kilograms')
end
it "should allow constructing with different units" do
w = U(53.97, 'oz')
w.in('kg').should == U(1.53, 'kg')
w.should == U('53.97 oz')
end
it "should allow constructing from another compatible Unit object" do
w = U(53.97, 'oz')
w2 = U(w) # defaults to oz, same units as copying object
w3 = U(w, 'kg')
w.should == w2
w.should == w3
w2.should == w3
w2.should == 53.97
w3.should == 1.53
end
describe "equality" do
it "should compare between units" do
w = U(3.5, 'kg')
lbs = w.in('pounds')
lbs.to_f.should be_near(7.7162)
w.should == lbs
w.should == 3.5
w.in('lbs').should == 7.716
end
it "should not compare with strings" do
w = U(1, 'kg')
proc { w == "5" }.should raise_error(ArgumentError)
end
it "should not allow equality between incompatible units" do
w = U(53.97, 'oz')
v = U(2, 'gallons')
proc { w == v }.should raise_error(ArgumentError)
end
end
it "should infer in_* methods for defined units" do
w = U(1, 'kg')
w.in_grams.should == 1000
w.in_kilograms.should == 1
end
describe "changing units in place" do
it "should allow changing units with attr writer" do
w = U(1, 'kg')
w.to_f.should == 1
w.unit = 'g'
w.to_f.should == 1000
end
it "should not allow changing to an unknown unit" do
w = U(1, 'kg')
proc { w.in!('blorg') }.should raise_error(ArgumentError)
end
it "should not allow changing to an incompatible unit" do
w = U(1, 'kg')
proc { w.in!('minutes') }.should raise_error(ArgumentError)
end
end
describe "changing units with in()" do
it "should handle basic conversions" do
w = U(1.53, 'kg')
w.in('grams').should == 1530
w.in('lbs').to_f.should be_near(3.37)
w.in('lbs').in('ounces').to_f.should be_near(53.97)
w.in('pounds').in('grams').in('kg').to_f.should be_near(1.53)
end
it "should not allow changing to an unknown unit" do
w = U(1, 'kg')
proc { w.in('blorg') }.should raise_error(ArgumentError)
proc { w.in_blorg }.should raise_error(ArgumentError)
end
it "should not allow changing to an incompatible unit" do
w = U(1, 'kg')
proc { w.in('minutes') }.should raise_error(ArgumentError)
proc { w.in_hours }.should raise_error(ArgumentError)
end
end
end