money 3.7.1 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +384 -351
- data/LICENSE +21 -21
- data/README.md +243 -214
- data/Rakefile +49 -49
- data/lib/money.rb +28 -27
- data/lib/money/bank/base.rb +131 -131
- data/lib/money/bank/variable_exchange.rb +252 -252
- data/lib/money/core_extensions.rb +82 -82
- data/lib/money/currency.rb +263 -422
- data/lib/money/currency_loader.rb +19 -0
- data/lib/money/money.rb +405 -405
- data/lib/money/money/arithmetic.rb +246 -246
- data/lib/money/money/formatting.rb +260 -244
- data/lib/money/money/parsing.rb +350 -350
- data/money.gemspec +29 -35
- data/spec/bank/base_spec.rb +72 -72
- data/spec/bank/variable_exchange_spec.rb +238 -238
- data/spec/core_extensions_spec.rb +158 -158
- data/spec/currency_spec.rb +120 -133
- data/spec/money/arithmetic_spec.rb +479 -479
- data/spec/money/formatting_spec.rb +383 -375
- data/spec/money/parsing_spec.rb +197 -197
- data/spec/money_spec.rb +292 -292
- data/spec/spec_helper.rb +28 -28
- metadata +54 -126
- data/lib/money.rbc +0 -184
- data/lib/money/bank/base.rbc +0 -818
- data/lib/money/bank/variable_exchange.rbc +0 -2550
- data/lib/money/core_extensions.rbc +0 -664
- data/lib/money/currency.rbc +0 -22708
- data/lib/money/money.rbc +0 -3861
- data/lib/money/money/arithmetic.rbc +0 -2778
- data/lib/money/money/formatting.rbc +0 -2265
- data/lib/money/money/parsing.rbc +0 -2737
- data/spec/bank/base_spec.rbc +0 -2461
- data/spec/bank/variable_exchange_spec.rbc +0 -7541
- data/spec/core_extensions_spec.rbc +0 -5921
- data/spec/currency_spec.rbc +0 -4535
- data/spec/money/arithmetic_spec.rbc +0 -25140
- data/spec/money/formatting_spec.rbc +0 -12545
- data/spec/money/parsing_spec.rbc +0 -6511
- data/spec/money_spec.rbc +0 -9824
- data/spec/spec_helper.rbc +0 -575
@@ -1,158 +1,158 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "Money core extensions" do
|
4
|
-
|
5
|
-
describe Numeric do
|
6
|
-
describe "#to_money" do
|
7
|
-
it "should work" do
|
8
|
-
money = 1234.to_money
|
9
|
-
money.cents.should == 1234_00
|
10
|
-
money.currency.should == Money.default_currency
|
11
|
-
|
12
|
-
money = 100.37.to_money
|
13
|
-
money.cents.should == 100_37
|
14
|
-
money.currency.should == Money.default_currency
|
15
|
-
|
16
|
-
money = BigDecimal.new('1234').to_money
|
17
|
-
money.cents.should == 1234_00
|
18
|
-
money.currency.should == Money.default_currency
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should accept optional currency" do
|
22
|
-
1234.to_money('USD').should == Money.new(123400, 'USD')
|
23
|
-
1234.to_money('EUR').should == Money.new(123400, 'EUR')
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should respect :subunit_to_unit currency property" do
|
27
|
-
10.to_money('USD').should == Money.new(10_00, 'USD')
|
28
|
-
10.to_money('TND').should == Money.new(10_000, 'TND')
|
29
|
-
10.to_money('CLP').should == Money.new(10, 'CLP')
|
30
|
-
end
|
31
|
-
|
32
|
-
specify "#issue/15" do
|
33
|
-
amount = 555.55.to_money
|
34
|
-
amount.should == Money.new(55555)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
describe String do
|
40
|
-
describe "#to_money" do
|
41
|
-
|
42
|
-
StringToMoney = {
|
43
|
-
"20.15" => Money.new(20_15) ,
|
44
|
-
"100" => Money.new(100_00) ,
|
45
|
-
"100.37" => Money.new(100_37) ,
|
46
|
-
"100,37" => Money.new(100_37) ,
|
47
|
-
"100 000" => Money.new(100_000_00) ,
|
48
|
-
"100,000.00" => Money.new(100_000_00) ,
|
49
|
-
"1,000" => Money.new(1_000_00) ,
|
50
|
-
"-1,000" => Money.new(-1_000_00) ,
|
51
|
-
"1,000.5" => Money.new(1_000_50) ,
|
52
|
-
"1,000.51" => Money.new(1_000_51) ,
|
53
|
-
"1,000.505" => Money.new(1_000_51) ,
|
54
|
-
"1,000.504" => Money.new(1_000_50) ,
|
55
|
-
"1,000.0000" => Money.new(1_000_00) ,
|
56
|
-
"1,000.5000" => Money.new(1_000_50) ,
|
57
|
-
"1,000.5099" => Money.new(1_000_51) ,
|
58
|
-
"1.550" => Money.new(1_55) ,
|
59
|
-
"25." => Money.new(25_00) ,
|
60
|
-
".75" => Money.new(75) ,
|
61
|
-
|
62
|
-
"100 USD" => Money.new(100_00, "USD") ,
|
63
|
-
"-100 USD" => Money.new(-100_00, "USD") ,
|
64
|
-
"100 EUR" => Money.new(100_00, "EUR") ,
|
65
|
-
"100.37 EUR" => Money.new(100_37, "EUR") ,
|
66
|
-
"100,37 EUR" => Money.new(100_37, "EUR") ,
|
67
|
-
"100,000.00 USD" => Money.new(100_000_00, "USD") ,
|
68
|
-
"100.000,00 EUR" => Money.new(100_000_00, "EUR") ,
|
69
|
-
"1,000 USD" => Money.new(1_000_00, "USD") ,
|
70
|
-
"-1,000 USD" => Money.new(-1_000_00, "USD") ,
|
71
|
-
"1,000.5500 USD" => Money.new(1_000_55, "USD") ,
|
72
|
-
"-1,000.6500 USD" => Money.new(-1_000_65, "USD") ,
|
73
|
-
"1.550 USD" => Money.new(1_55, "USD") ,
|
74
|
-
|
75
|
-
"USD 100" => Money.new(100_00, "USD") ,
|
76
|
-
"EUR 100" => Money.new(100_00, "EUR") ,
|
77
|
-
"EUR 100.37" => Money.new(100_37, "EUR") ,
|
78
|
-
"CAD -100.37" => Money.new(-100_37, "CAD") ,
|
79
|
-
"EUR 100,37" => Money.new(100_37, "EUR") ,
|
80
|
-
"EUR -100,37" => Money.new(-100_37, "EUR") ,
|
81
|
-
"USD 100,000.00" => Money.new(100_000_00, "USD") ,
|
82
|
-
"EUR 100.000,00" => Money.new(100_000_00, "EUR") ,
|
83
|
-
"USD 1,000" => Money.new(1_000_00, "USD") ,
|
84
|
-
"USD -1,000" => Money.new(-1_000_00, "USD") ,
|
85
|
-
"USD 1,000.9000" => Money.new(1_000_90, "USD") ,
|
86
|
-
"USD -1,000.090" => Money.new(-1_000_09, "USD") ,
|
87
|
-
"USD 1.5500" => Money.new(1_55, "USD") ,
|
88
|
-
|
89
|
-
"$100 USD" => Money.new(100_00, "USD") ,
|
90
|
-
"$1,194.59 USD" => Money.new(1_194_59, "USD") ,
|
91
|
-
"$-1,955 USD" => Money.new(-1_955_00, "USD") ,
|
92
|
-
"$1,194.5900 USD" => Money.new(1_194_59, "USD") ,
|
93
|
-
"$-1,955.000 USD" => Money.new(-1_955_00, "USD") ,
|
94
|
-
"$1.99000 USD" => Money.new(1_99, "USD") ,
|
95
|
-
}
|
96
|
-
|
97
|
-
specify "it should work" do
|
98
|
-
StringToMoney.each do |string, money|
|
99
|
-
string.to_money.should == money
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
it "should coerce input to string" do
|
104
|
-
Money.parse(20, "USD").should == Money.new(20_00, "USD")
|
105
|
-
end
|
106
|
-
|
107
|
-
it "should accept optional currency" do
|
108
|
-
"10.10".to_money('USD').should == Money.new(1010, 'USD')
|
109
|
-
"10.10".to_money('EUR').should == Money.new(1010, 'EUR')
|
110
|
-
"10.10 USD".to_money('USD').should == Money.new(1010, 'USD')
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should raise error if optional currency doesn't match string currency" do
|
114
|
-
lambda{ "10.10 USD".to_money('EUR') }.should raise_error(/Mismatching Currencies/)
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should ignore unrecognized data" do
|
118
|
-
"hello 2000 world".to_money.should == Money.new(2000_00)
|
119
|
-
end
|
120
|
-
|
121
|
-
it "should respect :subunit_to_unit currency property" do
|
122
|
-
"1".to_money("USD").should == Money.new(1_00, "USD")
|
123
|
-
"1".to_money("TND").should == Money.new(1_000, "TND")
|
124
|
-
"1".to_money("CLP").should == Money.new(1, "CLP")
|
125
|
-
"1.5".to_money("KWD").cents.should == 1500
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
describe "#to_currency" do
|
130
|
-
it "should convert string to Currency" do
|
131
|
-
"USD".to_currency.should == Money::Currency.new(:usd)
|
132
|
-
"EUR".to_currency.should == Money::Currency.new(:eur)
|
133
|
-
end
|
134
|
-
|
135
|
-
it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
|
136
|
-
lambda { "XXX".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
137
|
-
lambda { " ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
describe Symbol do
|
143
|
-
describe "#to_currency" do
|
144
|
-
it "should convert symbol to Currency" do
|
145
|
-
:usd.to_currency.should == Money::Currency.new(:usd)
|
146
|
-
:ars.to_currency.should == Money::Currency.new(:ars)
|
147
|
-
end
|
148
|
-
it "should work case-insensitive" do
|
149
|
-
:EUR.to_currency.should == Money::Currency.new(:eur)
|
150
|
-
end
|
151
|
-
it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
|
152
|
-
lambda { :XXX.to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
153
|
-
lambda { :" ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Money core extensions" do
|
4
|
+
|
5
|
+
describe Numeric do
|
6
|
+
describe "#to_money" do
|
7
|
+
it "should work" do
|
8
|
+
money = 1234.to_money
|
9
|
+
money.cents.should == 1234_00
|
10
|
+
money.currency.should == Money.default_currency
|
11
|
+
|
12
|
+
money = 100.37.to_money
|
13
|
+
money.cents.should == 100_37
|
14
|
+
money.currency.should == Money.default_currency
|
15
|
+
|
16
|
+
money = BigDecimal.new('1234').to_money
|
17
|
+
money.cents.should == 1234_00
|
18
|
+
money.currency.should == Money.default_currency
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should accept optional currency" do
|
22
|
+
1234.to_money('USD').should == Money.new(123400, 'USD')
|
23
|
+
1234.to_money('EUR').should == Money.new(123400, 'EUR')
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should respect :subunit_to_unit currency property" do
|
27
|
+
10.to_money('USD').should == Money.new(10_00, 'USD')
|
28
|
+
10.to_money('TND').should == Money.new(10_000, 'TND')
|
29
|
+
10.to_money('CLP').should == Money.new(10, 'CLP')
|
30
|
+
end
|
31
|
+
|
32
|
+
specify "#issue/15" do
|
33
|
+
amount = 555.55.to_money
|
34
|
+
amount.should == Money.new(55555)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe String do
|
40
|
+
describe "#to_money" do
|
41
|
+
|
42
|
+
StringToMoney = {
|
43
|
+
"20.15" => Money.new(20_15) ,
|
44
|
+
"100" => Money.new(100_00) ,
|
45
|
+
"100.37" => Money.new(100_37) ,
|
46
|
+
"100,37" => Money.new(100_37) ,
|
47
|
+
"100 000" => Money.new(100_000_00) ,
|
48
|
+
"100,000.00" => Money.new(100_000_00) ,
|
49
|
+
"1,000" => Money.new(1_000_00) ,
|
50
|
+
"-1,000" => Money.new(-1_000_00) ,
|
51
|
+
"1,000.5" => Money.new(1_000_50) ,
|
52
|
+
"1,000.51" => Money.new(1_000_51) ,
|
53
|
+
"1,000.505" => Money.new(1_000_51) ,
|
54
|
+
"1,000.504" => Money.new(1_000_50) ,
|
55
|
+
"1,000.0000" => Money.new(1_000_00) ,
|
56
|
+
"1,000.5000" => Money.new(1_000_50) ,
|
57
|
+
"1,000.5099" => Money.new(1_000_51) ,
|
58
|
+
"1.550" => Money.new(1_55) ,
|
59
|
+
"25." => Money.new(25_00) ,
|
60
|
+
".75" => Money.new(75) ,
|
61
|
+
|
62
|
+
"100 USD" => Money.new(100_00, "USD") ,
|
63
|
+
"-100 USD" => Money.new(-100_00, "USD") ,
|
64
|
+
"100 EUR" => Money.new(100_00, "EUR") ,
|
65
|
+
"100.37 EUR" => Money.new(100_37, "EUR") ,
|
66
|
+
"100,37 EUR" => Money.new(100_37, "EUR") ,
|
67
|
+
"100,000.00 USD" => Money.new(100_000_00, "USD") ,
|
68
|
+
"100.000,00 EUR" => Money.new(100_000_00, "EUR") ,
|
69
|
+
"1,000 USD" => Money.new(1_000_00, "USD") ,
|
70
|
+
"-1,000 USD" => Money.new(-1_000_00, "USD") ,
|
71
|
+
"1,000.5500 USD" => Money.new(1_000_55, "USD") ,
|
72
|
+
"-1,000.6500 USD" => Money.new(-1_000_65, "USD") ,
|
73
|
+
"1.550 USD" => Money.new(1_55, "USD") ,
|
74
|
+
|
75
|
+
"USD 100" => Money.new(100_00, "USD") ,
|
76
|
+
"EUR 100" => Money.new(100_00, "EUR") ,
|
77
|
+
"EUR 100.37" => Money.new(100_37, "EUR") ,
|
78
|
+
"CAD -100.37" => Money.new(-100_37, "CAD") ,
|
79
|
+
"EUR 100,37" => Money.new(100_37, "EUR") ,
|
80
|
+
"EUR -100,37" => Money.new(-100_37, "EUR") ,
|
81
|
+
"USD 100,000.00" => Money.new(100_000_00, "USD") ,
|
82
|
+
"EUR 100.000,00" => Money.new(100_000_00, "EUR") ,
|
83
|
+
"USD 1,000" => Money.new(1_000_00, "USD") ,
|
84
|
+
"USD -1,000" => Money.new(-1_000_00, "USD") ,
|
85
|
+
"USD 1,000.9000" => Money.new(1_000_90, "USD") ,
|
86
|
+
"USD -1,000.090" => Money.new(-1_000_09, "USD") ,
|
87
|
+
"USD 1.5500" => Money.new(1_55, "USD") ,
|
88
|
+
|
89
|
+
"$100 USD" => Money.new(100_00, "USD") ,
|
90
|
+
"$1,194.59 USD" => Money.new(1_194_59, "USD") ,
|
91
|
+
"$-1,955 USD" => Money.new(-1_955_00, "USD") ,
|
92
|
+
"$1,194.5900 USD" => Money.new(1_194_59, "USD") ,
|
93
|
+
"$-1,955.000 USD" => Money.new(-1_955_00, "USD") ,
|
94
|
+
"$1.99000 USD" => Money.new(1_99, "USD") ,
|
95
|
+
}
|
96
|
+
|
97
|
+
specify "it should work" do
|
98
|
+
StringToMoney.each do |string, money|
|
99
|
+
string.to_money.should == money
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should coerce input to string" do
|
104
|
+
Money.parse(20, "USD").should == Money.new(20_00, "USD")
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should accept optional currency" do
|
108
|
+
"10.10".to_money('USD').should == Money.new(1010, 'USD')
|
109
|
+
"10.10".to_money('EUR').should == Money.new(1010, 'EUR')
|
110
|
+
"10.10 USD".to_money('USD').should == Money.new(1010, 'USD')
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should raise error if optional currency doesn't match string currency" do
|
114
|
+
lambda{ "10.10 USD".to_money('EUR') }.should raise_error(/Mismatching Currencies/)
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should ignore unrecognized data" do
|
118
|
+
"hello 2000 world".to_money.should == Money.new(2000_00)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should respect :subunit_to_unit currency property" do
|
122
|
+
"1".to_money("USD").should == Money.new(1_00, "USD")
|
123
|
+
"1".to_money("TND").should == Money.new(1_000, "TND")
|
124
|
+
"1".to_money("CLP").should == Money.new(1, "CLP")
|
125
|
+
"1.5".to_money("KWD").cents.should == 1500
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "#to_currency" do
|
130
|
+
it "should convert string to Currency" do
|
131
|
+
"USD".to_currency.should == Money::Currency.new(:usd)
|
132
|
+
"EUR".to_currency.should == Money::Currency.new(:eur)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
|
136
|
+
lambda { "XXX".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
137
|
+
lambda { " ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe Symbol do
|
143
|
+
describe "#to_currency" do
|
144
|
+
it "should convert symbol to Currency" do
|
145
|
+
:usd.to_currency.should == Money::Currency.new(:usd)
|
146
|
+
:ars.to_currency.should == Money::Currency.new(:ars)
|
147
|
+
end
|
148
|
+
it "should work case-insensitive" do
|
149
|
+
:EUR.to_currency.should == Money::Currency.new(:eur)
|
150
|
+
end
|
151
|
+
it "should raise Money::Currency::UnknownCurrency with unknown Currency" do
|
152
|
+
lambda { :XXX.to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
153
|
+
lambda { :" ".to_currency }.should raise_error(Money::Currency::UnknownCurrency)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
data/spec/currency_spec.rb
CHANGED
@@ -1,133 +1,120 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe Money::Currency do
|
6
|
-
|
7
|
-
specify "#initialize should lookup data from
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
currency
|
13
|
-
currency.
|
14
|
-
currency.
|
15
|
-
currency.
|
16
|
-
currency.
|
17
|
-
currency.
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
specify "
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
Money::Currency.new(:eur).
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
Money::Currency.new(:eur).
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
Money::Currency.new(:eur).
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
Money::Currency.new(:usd).should
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
Money::Currency.new(:
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
usd.
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
Money::Currency
|
77
|
-
Money::Currency
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
specify "#self.
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
Money::Currency.
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
end
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
# silence_warnings do
|
122
|
-
# value = noisy_call # no warning voiced
|
123
|
-
# end
|
124
|
-
#
|
125
|
-
# noisy_call # warning voiced
|
126
|
-
def silence_warnings
|
127
|
-
old_verbose, $VERBOSE = $VERBOSE, nil
|
128
|
-
yield
|
129
|
-
ensure
|
130
|
-
$VERBOSE = old_verbose
|
131
|
-
end
|
132
|
-
|
133
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Money::Currency do
|
6
|
+
|
7
|
+
specify "#initialize should lookup data from loaded config" do
|
8
|
+
currency = Money::Currency.new("USD")
|
9
|
+
currency.id.should == :usd
|
10
|
+
currency.priority.should == 1
|
11
|
+
currency.iso_code.should == "USD"
|
12
|
+
currency.iso_numeric.should == "840"
|
13
|
+
currency.name.should == "United States Dollar"
|
14
|
+
currency.decimal_mark.should == "."
|
15
|
+
currency.separator.should == "."
|
16
|
+
currency.thousands_separator.should == ","
|
17
|
+
currency.delimiter.should == ","
|
18
|
+
end
|
19
|
+
|
20
|
+
specify "#initialize should raise UnknownMoney::Currency with unknown currency" do
|
21
|
+
lambda { Money::Currency.new("xxx") }.should raise_error(Money::Currency::UnknownCurrency, /xxx/)
|
22
|
+
end
|
23
|
+
|
24
|
+
specify "#== should return true if self === other" do
|
25
|
+
currency = Money::Currency.new(:eur)
|
26
|
+
currency.should == currency
|
27
|
+
end
|
28
|
+
|
29
|
+
specify "#== should return true if the id is equal" do
|
30
|
+
Money::Currency.new(:eur).should == Money::Currency.new(:eur)
|
31
|
+
Money::Currency.new(:eur).should_not == Money::Currency.new(:usd)
|
32
|
+
end
|
33
|
+
|
34
|
+
specify "#eql? should return true if #== returns true" do
|
35
|
+
Money::Currency.new(:eur).eql?(Money::Currency.new(:eur)).should be true
|
36
|
+
Money::Currency.new(:eur).eql?(Money::Currency.new(:usd)).should be false
|
37
|
+
end
|
38
|
+
|
39
|
+
specify "#hash should return the same value for equal objects" do
|
40
|
+
Money::Currency.new(:eur).hash.should == Money::Currency.new(:eur).hash
|
41
|
+
Money::Currency.new(:eur).hash.should_not == Money::Currency.new(:usd).hash
|
42
|
+
end
|
43
|
+
|
44
|
+
specify "#hash can be used to return the intersection of Currency object arrays" do
|
45
|
+
intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
|
46
|
+
intersection.should == [Money::Currency.new(:eur)]
|
47
|
+
end
|
48
|
+
|
49
|
+
specify "#<=> should compare objects by priority" do
|
50
|
+
Money::Currency.new(:cad).should > Money::Currency.new(:usd)
|
51
|
+
Money::Currency.new(:usd).should < Money::Currency.new(:eur)
|
52
|
+
end
|
53
|
+
|
54
|
+
specify "#to_s" do
|
55
|
+
Money::Currency.new(:usd).to_s.should == "USD"
|
56
|
+
Money::Currency.new(:eur).to_s.should == "EUR"
|
57
|
+
end
|
58
|
+
|
59
|
+
specify "#to_currency" do
|
60
|
+
usd = Money::Currency.new(:usd)
|
61
|
+
usd.to_currency.should == usd
|
62
|
+
end
|
63
|
+
|
64
|
+
specify "#inspect" do
|
65
|
+
Money::Currency.new(:usd).inspect.should ==
|
66
|
+
%Q{#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, iso_code: USD, iso_numeric: 840, subunit: Cent>}
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
specify "#self.find should return currency matching given id" do
|
71
|
+
with_custom_definitions do
|
72
|
+
Money::Currency::TABLE[:usd] = JSON.parse(%Q({ "priority": 1, "iso_code": "USD", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 100, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": "," }))
|
73
|
+
Money::Currency::TABLE[:eur] = JSON.parse(%Q({ "priority": 2, "iso_code": "EUR", "iso_numeric": "978", "name": "Euro", "symbol": "€", "subunit": "Cent", "subunit_to_unit": 100, "symbol_first": false, "html_entity": "€", "decimal_mark": ",", "thousands_separator": "." }))
|
74
|
+
|
75
|
+
expected = Money::Currency.new(:eur)
|
76
|
+
Money::Currency.find(:eur).should == expected
|
77
|
+
Money::Currency.find(:EUR).should == expected
|
78
|
+
Money::Currency.find("eur").should == expected
|
79
|
+
Money::Currency.find("EUR").should == expected
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
specify "#self.find should return nil unless currency matching given id" do
|
84
|
+
Money::Currency.find("ZZZ").should be_nil
|
85
|
+
end
|
86
|
+
|
87
|
+
specify "#self.wrap should return nil if object is nil" do
|
88
|
+
Money::Currency.wrap(nil).should == nil
|
89
|
+
Money::Currency.wrap(Money::Currency.new(:usd)).should == Money::Currency.new(:usd)
|
90
|
+
Money::Currency.wrap(:usd).should == Money::Currency.new(:usd)
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def with_custom_definitions(&block)
|
95
|
+
begin
|
96
|
+
old = Money::Currency::TABLE.dup
|
97
|
+
Money::Currency::TABLE.clear
|
98
|
+
yield
|
99
|
+
ensure
|
100
|
+
silence_warnings do
|
101
|
+
Money::Currency.const_set("TABLE", old)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
|
107
|
+
#
|
108
|
+
# silence_warnings do
|
109
|
+
# value = noisy_call # no warning voiced
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# noisy_call # warning voiced
|
113
|
+
def silence_warnings
|
114
|
+
old_verbose, $VERBOSE = $VERBOSE, nil
|
115
|
+
yield
|
116
|
+
ensure
|
117
|
+
$VERBOSE = old_verbose
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|