latinum 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/latinum/bank.rb +51 -20
- data/lib/latinum/collection.rb +31 -10
- data/lib/latinum/currencies/global.rb +14 -0
- data/{spec/latinum/comparison_spec.rb → lib/latinum/error.rb} +6 -18
- data/lib/latinum/formatters.rb +38 -21
- data/lib/latinum/resource.rb +66 -35
- data/lib/latinum/version.rb +1 -1
- metadata +24 -44
- data/.rspec +0 -3
- data/.travis.yml +0 -20
- data/Gemfile +0 -5
- data/README.md +0 -276
- data/Rakefile +0 -8
- data/latinum.gemspec +0 -22
- data/spec/latinum/bank_spec.rb +0 -92
- data/spec/latinum/collection_spec.rb +0 -142
- data/spec/latinum/formatters_spec.rb +0 -95
- data/spec/latinum/integrals_spec.rb +0 -47
- data/spec/latinum/resource_spec.rb +0 -132
- data/spec/spec_helper.rb +0 -12
data/Rakefile
DELETED
data/latinum.gemspec
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
|
2
|
-
require_relative "lib/latinum/version"
|
3
|
-
|
4
|
-
Gem::Specification.new do |spec|
|
5
|
-
spec.name = "latinum"
|
6
|
-
spec.version = Latinum::VERSION
|
7
|
-
spec.authors = ["Samuel Williams"]
|
8
|
-
spec.email = ["samuel.williams@oriontransfer.co.nz"]
|
9
|
-
spec.summary = %q{Latinum is a simple gem for managing resource computations, including money and minerals.}
|
10
|
-
spec.homepage = "https://github.com/ioquatix/latinum"
|
11
|
-
spec.license = "MIT"
|
12
|
-
|
13
|
-
spec.files = `git ls-files`.split($/)
|
14
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
15
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
-
spec.require_paths = ["lib"]
|
17
|
-
|
18
|
-
spec.add_development_dependency "covered"
|
19
|
-
spec.add_development_dependency "bundler"
|
20
|
-
spec.add_development_dependency "rspec", "~> 3.4"
|
21
|
-
spec.add_development_dependency "rake"
|
22
|
-
end
|
data/spec/latinum/bank_spec.rb
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require 'latinum'
|
24
|
-
require 'latinum/currencies/global'
|
25
|
-
|
26
|
-
RSpec.describe Latinum::Bank do
|
27
|
-
before(:all) do
|
28
|
-
@bank = Latinum::Bank.new(Latinum::Currencies::Global)
|
29
|
-
|
30
|
-
@bank << Latinum::ExchangeRate.new("NZD", "AUD", "0.5")
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should format the amounts correctly" do
|
34
|
-
resource = Latinum::Resource.new("10", "NZD")
|
35
|
-
|
36
|
-
expect(@bank.format(resource)).to be == "$10.00 NZD"
|
37
|
-
expect(@bank.format(resource, name: nil)).to be == "$10.00"
|
38
|
-
|
39
|
-
resource = Latinum::Resource.new("391", "AUD")
|
40
|
-
expect(@bank.format(resource)).to be == "$391.00 AUD"
|
41
|
-
|
42
|
-
resource = Latinum::Resource.new("-100", "NZD")
|
43
|
-
expect(@bank.format(resource)).to be == "-$100.00 NZD"
|
44
|
-
|
45
|
-
resource = Latinum::Resource.new("1.12345678", "BTC")
|
46
|
-
expect(@bank.format(resource)).to be == "B⃦1.12345678 BTC"
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should round up using correct precision" do
|
50
|
-
resource = Latinum::Resource.new("19.9989", "NZD")
|
51
|
-
|
52
|
-
expect(@bank.round(resource)).to be == Latinum::Resource.new(20, "NZD")
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should round down using correct precision" do
|
56
|
-
resource = Latinum::Resource.new("19.991", "NZD")
|
57
|
-
|
58
|
-
expect(@bank.round(resource)).to be == Latinum::Resource.new("19.99", "NZD")
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should round values when formatting" do
|
62
|
-
resource = Latinum::Resource.new("19.9989", "NZD")
|
63
|
-
|
64
|
-
expect(@bank.format(resource)).to be == "$20.00 NZD"
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should exchange currencies from NZD to AUD" do
|
68
|
-
nzd = Latinum::Resource.new("10", "NZD")
|
69
|
-
|
70
|
-
aud = @bank.exchange nzd, "AUD"
|
71
|
-
expect(aud).to be == Latinum::Resource.new("5", "AUD")
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should parse strings into resources" do
|
75
|
-
expect(@bank.parse("$5")).to be == Latinum::Resource.new("5", "USD")
|
76
|
-
expect(@bank.parse("$5 NZD")).to be == Latinum::Resource.new("5", "NZD")
|
77
|
-
expect(@bank.parse("€5")).to be == Latinum::Resource.new("5", "EUR")
|
78
|
-
|
79
|
-
expect(@bank.parse("5 NZD")).to be == Latinum::Resource.new("5", "NZD")
|
80
|
-
|
81
|
-
expect(@bank.parse("-$5")).to be == Latinum::Resource.new("-5", "USD")
|
82
|
-
expect(@bank.parse("-$5 NZD")).to be == Latinum::Resource.new("-5", "NZD")
|
83
|
-
expect(@bank.parse("-€5")).to be == Latinum::Resource.new("-5", "EUR")
|
84
|
-
|
85
|
-
expect(@bank.parse("5", default_name: "EUR")).to be == Latinum::Resource.new("5", "EUR")
|
86
|
-
expect(@bank.parse("-5", default_name: "EUR")).to be == Latinum::Resource.new("-5", "EUR")
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should fail to parse unknown resource" do
|
90
|
-
expect{@bank.parse("B5")}.to raise_error(ArgumentError)
|
91
|
-
end
|
92
|
-
end
|
@@ -1,142 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require 'latinum'
|
24
|
-
require 'latinum/currencies/global'
|
25
|
-
|
26
|
-
require 'set'
|
27
|
-
|
28
|
-
RSpec.describe Latinum::Collection do
|
29
|
-
it "can set an initial value" do
|
30
|
-
subject["NZD"] = BigDecimal("20")
|
31
|
-
|
32
|
-
expect(subject["NZD"]).to be == Latinum::Resource.load("20 NZD")
|
33
|
-
end
|
34
|
-
|
35
|
-
it "can be negated" do
|
36
|
-
subject["NZD"] = BigDecimal("20")
|
37
|
-
|
38
|
-
negated = -subject
|
39
|
-
|
40
|
-
expect(negated["NZD"]).to be == BigDecimal("-20")
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should sum up currencies correctly" do
|
44
|
-
resource = Latinum::Resource.new("10", "NZD")
|
45
|
-
|
46
|
-
subject << resource
|
47
|
-
expect(subject["NZD"]).to be == resource
|
48
|
-
|
49
|
-
subject << resource
|
50
|
-
expect(subject["NZD"]).to be == (resource * 2)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should sum up multiple currencies correctly" do
|
54
|
-
resources = [
|
55
|
-
Latinum::Resource.new("10", "NZD"),
|
56
|
-
Latinum::Resource.new("10", "AUD"),
|
57
|
-
Latinum::Resource.new("10", "USD"),
|
58
|
-
Latinum::Resource.new("10", "NZD"),
|
59
|
-
Latinum::Resource.new("10", "AUD"),
|
60
|
-
Latinum::Resource.new("10", "USD"),
|
61
|
-
]
|
62
|
-
|
63
|
-
subject = Latinum::Collection.new
|
64
|
-
subject << resources
|
65
|
-
|
66
|
-
expect(subject["NZD"]).to be == (resources[0] * 2)
|
67
|
-
expect(subject.names).to be == Set.new(["NZD", "AUD", "USD"])
|
68
|
-
end
|
69
|
-
|
70
|
-
it "can add two collections together" do
|
71
|
-
other_resources = [
|
72
|
-
Latinum::Resource.new("10", "NZD"),
|
73
|
-
Latinum::Resource.new("10", "AUD"),
|
74
|
-
Latinum::Resource.new("10", "USD"),
|
75
|
-
]
|
76
|
-
|
77
|
-
other_collection = Latinum::Collection.new
|
78
|
-
other_collection << other_resources
|
79
|
-
|
80
|
-
resources = [
|
81
|
-
Latinum::Resource.new("10", "NZD"),
|
82
|
-
Latinum::Resource.new("10", "AUD"),
|
83
|
-
Latinum::Resource.new("10", "USD"),
|
84
|
-
]
|
85
|
-
|
86
|
-
subject << resources
|
87
|
-
subject << other_collection
|
88
|
-
|
89
|
-
expect(subject["NZD"]).to be == Latinum::Resource.load("20 NZD")
|
90
|
-
expect(subject["AUD"]).to be == Latinum::Resource.load("20 AUD")
|
91
|
-
expect(subject["USD"]).to be == Latinum::Resource.load("20 USD")
|
92
|
-
end
|
93
|
-
|
94
|
-
it "can enumerate resources" do
|
95
|
-
resources = [
|
96
|
-
Latinum::Resource.new("10", "NZD"),
|
97
|
-
Latinum::Resource.new("10", "AUD"),
|
98
|
-
Latinum::Resource.new("10", "USD"),
|
99
|
-
]
|
100
|
-
|
101
|
-
collection = Latinum::Collection.new
|
102
|
-
collection << resources
|
103
|
-
|
104
|
-
expect(collection.each.to_a).to be == resources
|
105
|
-
end
|
106
|
-
|
107
|
-
describe '#-@' do
|
108
|
-
it "can subtract itself" do
|
109
|
-
subject << Latinum::Resource.new("10.0", "NZD")
|
110
|
-
|
111
|
-
result = (subject - subject).compact
|
112
|
-
|
113
|
-
expect(result).to be_empty
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
describe '#compact' do
|
118
|
-
it "can remove zero value resources" do
|
119
|
-
subject << Latinum::Resource.new("0.0", "NZD")
|
120
|
-
|
121
|
-
expect(subject).to include "NZD"
|
122
|
-
expect(subject.compact).to_not include "NZD"
|
123
|
-
end
|
124
|
-
|
125
|
-
it "doesn't remove non-zero value resources" do
|
126
|
-
subject << Latinum::Resource.new("1.0", "NZD")
|
127
|
-
|
128
|
-
expect(subject).to include "NZD"
|
129
|
-
expect(subject.compact).to include "NZD"
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe '#to_s' do
|
134
|
-
it "can geneate formatted output" do
|
135
|
-
subject << Latinum::Resource.new("5.0", "NZD")
|
136
|
-
subject << Latinum::Resource.new("10.0", "AUD")
|
137
|
-
subject << Latinum::Resource.new("20", "JPY")
|
138
|
-
|
139
|
-
expect(subject.to_s).to be == "5.0 NZD; 10.0 AUD; 20.0 JPY"
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require 'latinum/bank'
|
24
|
-
require 'latinum/currencies/global'
|
25
|
-
require 'latinum/formatters'
|
26
|
-
|
27
|
-
RSpec.describe Latinum::Formatters::PlainFormatter.new(name: "NZD") do
|
28
|
-
let(:amount) {BigDecimal(10)}
|
29
|
-
|
30
|
-
it "can convert to integral" do
|
31
|
-
expect(subject.to_integral(amount)).to be == 10
|
32
|
-
end
|
33
|
-
|
34
|
-
it "can convert from integral" do
|
35
|
-
expect(subject.from_integral(10)).to be == amount
|
36
|
-
end
|
37
|
-
|
38
|
-
it "can do basic formatting" do
|
39
|
-
expect(subject.format(amount)).to be == "10.0 NZD"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
RSpec.describe Latinum::Formatters::DecimalCurrencyFormatter do
|
44
|
-
before(:all) do
|
45
|
-
@bank = Latinum::Bank.new
|
46
|
-
@bank.import(Latinum::Currencies::Global)
|
47
|
-
end
|
48
|
-
|
49
|
-
let(:resource) {Latinum::Resource.load("10 NZD")}
|
50
|
-
|
51
|
-
it "should format output" do
|
52
|
-
expect(@bank.format(resource)).to be == "$10.00 NZD"
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should format output without name" do
|
56
|
-
expect(@bank.format(resource, name: nil)).to be == "$10.00"
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should format output with alternative name" do
|
60
|
-
expect(@bank.format(resource, name: "Foo")).to be == "$10.00 Foo"
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should format output" do
|
64
|
-
expect(@bank.format(resource)).to be == "$10.00 NZD"
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should format output without symbol" do
|
68
|
-
expect(@bank.format(resource, symbol: nil)).to be == "10.00 NZD"
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should format output with alternative symbol" do
|
72
|
-
expect(@bank.format(resource, symbol: "!!")).to be == "!!10.00 NZD"
|
73
|
-
end
|
74
|
-
|
75
|
-
context "negative zero" do
|
76
|
-
let(:resource) {Latinum::Resource.new(BigDecimal("-0"), "NZD")}
|
77
|
-
|
78
|
-
it "should format as (positve) zero" do
|
79
|
-
expect(@bank.format(resource)).to be == "$0.00 NZD"
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
RSpec.describe Latinum::Formatters::DecimalCurrencyFormatter do
|
85
|
-
before(:all) do
|
86
|
-
@bank = Latinum::Bank.new
|
87
|
-
@bank.import(Latinum::Currencies::Global)
|
88
|
-
end
|
89
|
-
|
90
|
-
let(:resource) {Latinum::Resource.load("10 JPY")}
|
91
|
-
|
92
|
-
it "should format without separator or fractional part" do
|
93
|
-
expect(@bank.format(resource)).to be == "¥10 JPY"
|
94
|
-
end
|
95
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require 'latinum'
|
24
|
-
require 'latinum/currencies/global'
|
25
|
-
|
26
|
-
RSpec.describe Latinum::Bank do
|
27
|
-
before(:all) do
|
28
|
-
@bank = Latinum::Bank.new
|
29
|
-
@bank.import(Latinum::Currencies::Global)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should convert to NZD integral value" do
|
33
|
-
resource = Latinum::Resource.new("10", "NZD")
|
34
|
-
|
35
|
-
expect(@bank.to_integral(resource)).to be == 1000
|
36
|
-
|
37
|
-
expect(@bank.from_integral(1000, "NZD")).to be == resource
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should convert to BTC integral value" do
|
41
|
-
resource = Latinum::Resource.new("1.12345678", "BTC")
|
42
|
-
|
43
|
-
expect(@bank.to_integral(resource)).to be == 112345678
|
44
|
-
|
45
|
-
expect(@bank.from_integral(112345678, "BTC")).to be == resource
|
46
|
-
end
|
47
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
-
#
|
5
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
# of this software and associated documentation files (the "Software"), to deal
|
7
|
-
# in the Software without restriction, including without limitation the rights
|
8
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
# copies of the Software, and to permit persons to whom the Software is
|
10
|
-
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
12
|
-
# The above copyright notice and this permission notice shall be included in
|
13
|
-
# all copies or substantial portions of the Software.
|
14
|
-
#
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
# THE SOFTWARE.
|
22
|
-
|
23
|
-
require 'latinum/resource'
|
24
|
-
|
25
|
-
RSpec.describe Latinum::Resource do
|
26
|
-
it "should load and dump resources" do
|
27
|
-
resource = Latinum::Resource.load("10 NZD")
|
28
|
-
string_representation = Latinum::Resource.dump(resource)
|
29
|
-
|
30
|
-
loaded_resource = Latinum::Resource.load(string_representation)
|
31
|
-
|
32
|
-
expect(loaded_resource).to be == loaded_resource
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should load and dump nil correctly" do
|
36
|
-
expect(Latinum::Resource.load(nil)).to be nil
|
37
|
-
expect(Latinum::Resource.dump(nil)).to be nil
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should handle empty strings correctly" do
|
41
|
-
expect(Latinum::Resource.load("")).to be nil
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should handle whitespace strings correctly" do
|
45
|
-
expect(Latinum::Resource.load(" ")).to be nil
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should load and dump resources correctly" do
|
49
|
-
resource = Latinum::Resource.new(10, 'NZD')
|
50
|
-
|
51
|
-
expect(Latinum::Resource.load("10.0 NZD")).to be == resource
|
52
|
-
expect(Latinum::Resource.dump(resource)).to be == "10.0 NZD"
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should inspect nicely" do
|
56
|
-
resource = Latinum::Resource.load("10 NZD")
|
57
|
-
|
58
|
-
expect(resource.inspect).to be == '#<Latinum::Resource "10.0 NZD">'
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should compute percentage difference" do
|
62
|
-
original_price = Latinum::Resource.load("10 NZD")
|
63
|
-
discount_price = Latinum::Resource.load("5 NZD")
|
64
|
-
|
65
|
-
discount = (original_price - discount_price) / original_price
|
66
|
-
|
67
|
-
expect(discount).to be == 0.5
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should not divide" do
|
71
|
-
original_price = Latinum::Resource.load("10 NZD")
|
72
|
-
discount_price = Latinum::Resource.load("5 USD")
|
73
|
-
|
74
|
-
expect{original_price / discount_price}.to raise_exception(Latinum::DifferentResourceNameError)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should compute quotient" do
|
78
|
-
original_price = Latinum::Resource.load("10 NZD")
|
79
|
-
|
80
|
-
expect(original_price / 2.0).to be == Latinum::Resource.load("5 NZD")
|
81
|
-
end
|
82
|
-
|
83
|
-
it "should add two resources of the same symbol" do
|
84
|
-
a = Latinum::Resource.load("10 NZD")
|
85
|
-
b = Latinum::Resource.load("5 NZD")
|
86
|
-
c = Latinum::Resource.load("15 NZD")
|
87
|
-
|
88
|
-
expect(a+b).to be == c
|
89
|
-
end
|
90
|
-
|
91
|
-
it "should fail two resources of different symbol" do
|
92
|
-
a = Latinum::Resource.load("10 NZD")
|
93
|
-
b = Latinum::Resource.load("5 USD")
|
94
|
-
|
95
|
-
expect{a+b}.to raise_error(Latinum::DifferentResourceNameError)
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should be able to negate a value" do
|
99
|
-
a = Latinum::Resource.load("10 NZD")
|
100
|
-
b = Latinum::Resource.load("-10 NZD")
|
101
|
-
|
102
|
-
expect(-a).to be == b
|
103
|
-
end
|
104
|
-
|
105
|
-
it "can be used as a hash key" do
|
106
|
-
a = Latinum::Resource.load("10 NZD")
|
107
|
-
b = Latinum::Resource.load("0 NZD")
|
108
|
-
|
109
|
-
hash = {a => true}
|
110
|
-
|
111
|
-
expect(hash).to be_include a
|
112
|
-
expect(hash).to_not be_include b
|
113
|
-
end
|
114
|
-
|
115
|
-
it "can be zero" do
|
116
|
-
a = Latinum::Resource.load("10 NZD")
|
117
|
-
b = Latinum::Resource.load("0 NZD")
|
118
|
-
|
119
|
-
expect(a).to_not be_zero
|
120
|
-
expect(b).to be_zero
|
121
|
-
end
|
122
|
-
|
123
|
-
it "can be eql?" do
|
124
|
-
a = Latinum::Resource.load("10 NZD")
|
125
|
-
b = Latinum::Resource.load("0 NZD")
|
126
|
-
c = Latinum::Resource.load("0 NZD")
|
127
|
-
|
128
|
-
expect(a).to be_eql a
|
129
|
-
expect(a).to_not be_eql b
|
130
|
-
expect(b).to be_eql c
|
131
|
-
end
|
132
|
-
end
|