This repository has been archived by the owner on Mar 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
error_safe_spec.rb
59 lines (57 loc) · 2.12 KB
/
error_safe_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
# -*- encoding : utf-8 -*-
require 'spec_helper'
describe "Exchange::ErrorSafe" do
let(:is_mri_21_or_greater) { ((RUBY_VERSION =~ /\A2.1/ || RUBY_VERSION =~ /\A2.2/) && RUBY_ENGINE == 'ruby') }
let(:time) { Time.gm(2012,8,27) }
before(:all) do
Exchange.configuration = Exchange::Configuration.new { |c| c.cache = { :subclass => :no_cache } }
end
before(:each) do
allow(Time).to receive(:now).and_return time
end
after(:all) do
Exchange.configuration.reset
end
describe "money safe calculation" do
describe "*" do
it "should calculate correctly with exchange money" do
expect((0.29 * 50.in(:usd)).round).to eq(15)
end
it "should not touch other operations" do
expect((0.29 * 50).round).to eq(14)
end
end
describe "/" do
it "should calculate correctly with exchange money" do
expect((((1829.82 / 12.in(:usd)) * 100).round.to_f / 100).to_f).to eq(152.49)
end
it "should not touch other operations" do
expect(((1829.82 / 12) * 100).round.to_f / 100).to eq(152.48)
end
end
describe "+" do
it "should calculate correctly with exchange money" do
expect((1.0e+25 + BigDecimal.new("9999999999999999900000000").in(:usd)).round.to_f).to eq(2.0e+25)
end
it "should not touch other operations" do
if is_mri_21_or_greater
expect((1.0e+25 + BigDecimal.new("9999999999999999900000000")).round).to eq(BigDecimal.new("0.199999999999999999E26"))
else
expect((1.0e+25 + BigDecimal.new("9999999999999999900000000")).round).to eq(20000000000000001811939328)
end
end
end
describe "-" do
it "should calculate correctly with exchange money" do
expect((1.0e+25 - BigDecimal.new("9999999999999999900000000").in(:usd)).round).to eq(100000000)
end
it "should not touch other operations" do
if is_mri_21_or_greater
expect(1.0e+25 - BigDecimal.new("9999999999999999900000000")).to eq(BigDecimal.new("0.1E9"))
else
expect(1.0e+25 - BigDecimal.new("9999999999999999900000000")).to eq(0)
end
end
end
end
end