summaryrefslogtreecommitdiff
path: root/spec/models/quantity_spec.rb
blob: 85bc0bc4fe30645287ca1abdd1b1d8e83ef1a502 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require "spec_helper"

describe Quantity do
  describe "#to" do
    it "converts lbs to kgs" do
      lbs = Quantity.new(135.0, :lbs)
      expect(lbs.to(:kg).to_f.round(1)).to eql(61.2)
    end

    it "converts kgs to kgs" do
      kgs = Quantity.new(135.0, :kgs)
      expect(kgs.to(:kgs).to_f).to eql(135.0)
    end

    it "converts kgs to lbs" do
      kgs = Quantity.new(61.2, :kgs)
      expect(kgs.to(:lbs).to_f.round(0)).to eql(135)
    end

    it "converts lbs to lbs" do
      lbs = Quantity.new(135.0, :lbs)
      expect(lbs.to(:lbs).to_f).to eql(135.0)
    end
  end

  describe "#eql?" do
    it "is equal when both are lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity).to eql(other)
    end

    it "is equal when both are kgs" do
      quantity = Quantity.new(61.2, :kgs)
      other = Quantity.new(61.2, :kgs)
      expect(quantity).to eql(other)
    end

    it "is equal when different units" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(61.2, :kgs)
      expect(quantity).to eql(other)
    end

    it "is equal to a float" do
      quantity = Quantity.new(135.0, :lbs)
      expect(quantity).to eql(135.0)
    end

    it "is not equal" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(135.2, :lbs)
      expect(quantity).to_not eql(other)
    end
  end

  describe "#+" do
    it "adds lbs to lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity + other).to eql(Quantity.new(270.0, :lbs))
    end

    it "adds kgs to lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(61.2, :kg)
      expect(quantity + other).to eql(Quantity.new(270.0, :lbs))
    end

    it "adds lbs to kgs" do
      quantity = Quantity.new(61.2, :kg)
      other = Quantity.new(135.0, :lbs)
      expect(quantity + other).to eql(Quantity.new(122.4, :kg))
    end

    it "adds a float to lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = 135.0
      expect(quantity + other).to eql(Quantity.new(270.0, :lbs))
    end
  end

  describe "#-" do
    it "subtracts lbs from lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity - other).to eql(Quantity.new(0.0, :lbs))
    end

    it "subtracts kgs from lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(61.2, :kg)
      expect(quantity - other).to eql(Quantity.new(0.0, :lbs))
    end

    it "subtracts lbs from kgs" do
      quantity = Quantity.new(61.2, :kg)
      other = Quantity.new(135.0, :lbs)
      expect(quantity - other).to eql(Quantity.new(0.0, :kg))
    end

    it "subtracts a float from lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = 135.0
      expect(quantity - other).to eql(Quantity.new(0.0, :lbs))
    end
  end

  describe "#/" do
    it "divides lbs from lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity / other).to eql(Quantity.new(1.0, :lbs))
    end

    it "divides kgs from lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(61.2, :kg)
      expect(quantity / other).to eql(Quantity.new(1.0, :lbs))
    end

    it "divides lbs from kgs" do
      quantity = Quantity.new(61.2, :kg)
      other = Quantity.new(135.0, :lbs)
      expect(quantity / other).to eql(Quantity.new(1.0, :kg))
    end

    it "divides a float from lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = 135.0
      expect(quantity / other).to eql(Quantity.new(1.0, :lbs))
    end
  end

  describe "#*" do
    it "multiples lbs with lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity * other).to eql(Quantity.new(18_225.0, :lbs))
    end

    it "multiples a float with lbs" do
      quantity = Quantity.new(135.0, :lbs)
      other = 135.0
      expect(quantity * other).to eql(Quantity.new(18_225.0, :lbs))
    end
  end

  describe "#>" do
    it "compares lbs with lbs" do
      quantity = Quantity.new(135.1, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity).to be > other
      expect(other).to_not be > quantity
    end
  end

  describe "#>=" do
    it "compares lbs with lbs" do
      quantity = Quantity.new(135.2, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity).to be >= quantity
      expect(quantity).to be >= other
      expect(other).to_not be >= quantity
    end
  end

  describe "#<" do
    it "compares lbs with lbs" do
      quantity = Quantity.new(135.1, :lbs)
      other = Quantity.new(135.0, :lbs)
      expect(quantity).to_not be < other
      expect(other).to be < quantity
    end
  end

  describe "#to_json" do
    it 'format the amount and unit to json' do
      expect(100.lbs.to_json).to eql({
        amount: 100.0,
        unit: 'lbs',
      }.to_json)
    end
  end
end