money 3.6.1 → 3.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +335 -319
- data/LICENSE +21 -21
- data/README.md +214 -209
- data/Rakefile +49 -49
- data/lib/money.rb +27 -27
- data/lib/money/bank/base.rb +131 -131
- data/lib/money/bank/variable_exchange.rb +252 -251
- data/lib/money/core_extensions.rb +82 -63
- data/lib/money/currency.rb +422 -415
- data/lib/money/money.rb +387 -1210
- data/lib/money/money/arithmetic.rb +246 -0
- data/lib/money/money/formatting.rb +234 -0
- data/lib/money/money/parsing.rb +350 -0
- data/money.gemspec +34 -27
- data/spec/bank/base_spec.rb +72 -72
- data/spec/bank/variable_exchange_spec.rb +238 -238
- data/spec/core_extensions_spec.rb +158 -142
- data/spec/currency_spec.rb +133 -128
- data/spec/money/arithmetic_spec.rb +479 -0
- data/spec/money/formatting_spec.rb +352 -0
- data/spec/money/parsing_spec.rb +197 -0
- data/spec/money_spec.rb +271 -1268
- data/spec/spec_helper.rb +28 -17
- metadata +33 -23
- data/lib/money.rbc +0 -170
- data/lib/money/bank/base.rbc +0 -800
- data/lib/money/bank/variable_exchange.rbc +0 -2496
- data/lib/money/core_extensions.rbc +0 -474
- data/lib/money/currency.rbc +0 -22600
- data/lib/money/money.rbc +0 -10070
- data/spec/bank/base_spec.rbc +0 -2409
- data/spec/bank/variable_exchange_spec.rbc +0 -7389
- data/spec/core_extensions_spec.rbc +0 -5215
- data/spec/currency_spec.rbc +0 -4341
- data/spec/money_spec.rbc +0 -50121
- data/spec/spec_helper.rbc +0 -346
@@ -1,142 +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
|
-
|
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,128 +1,133 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require "spec_helper"
|
4
|
-
|
5
|
-
describe Money::Currency do
|
6
|
-
|
7
|
-
specify "#initialize should lookup data from TABLE" do
|
8
|
-
with_custom_definitions do
|
9
|
-
Money::Currency::TABLE[:usd] = {:priority => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => ","}
|
10
|
-
Money::Currency::TABLE[:eur] = {:priority => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => ","}
|
11
|
-
|
12
|
-
currency = Money::Currency.new("USD")
|
13
|
-
currency.id.should == :usd
|
14
|
-
currency.priority.should == 1
|
15
|
-
currency.iso_code.should == "USD"
|
16
|
-
currency.name.should == "United States Dollar"
|
17
|
-
currency.decimal_mark.should == "."
|
18
|
-
currency.separator.should == "."
|
19
|
-
currency.thousands_separator.should == ","
|
20
|
-
currency.delimiter.should == ","
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
specify "#initialize should raise UnknownMoney::Currency with unknown currency" do
|
25
|
-
lambda { Money::Currency.new("xxx") }.should raise_error(Money::Currency::UnknownCurrency, /xxx/)
|
26
|
-
end
|
27
|
-
|
28
|
-
specify "#== should return true if self === other" do
|
29
|
-
currency = Money::Currency.new(:eur)
|
30
|
-
currency.should == currency
|
31
|
-
end
|
32
|
-
|
33
|
-
specify "#== should return true if the id is equal" do
|
34
|
-
Money::Currency.new(:eur).should == Money::Currency.new(:eur)
|
35
|
-
Money::Currency.new(:eur).should_not == Money::Currency.new(:usd)
|
36
|
-
end
|
37
|
-
|
38
|
-
specify "#eql? should return true if #== returns true" do
|
39
|
-
Money::Currency.new(:eur).eql?(Money::Currency.new(:eur)).should be true
|
40
|
-
Money::Currency.new(:eur).eql?(Money::Currency.new(:usd)).should be false
|
41
|
-
end
|
42
|
-
|
43
|
-
specify "#hash should return the same value for equal objects" do
|
44
|
-
Money::Currency.new(:eur).hash.should == Money::Currency.new(:eur).hash
|
45
|
-
Money::Currency.new(:eur).hash.should_not == Money::Currency.new(:usd).hash
|
46
|
-
end
|
47
|
-
|
48
|
-
specify "#hash can be used to return the intersection of Currency object arrays" do
|
49
|
-
intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
|
50
|
-
intersection.should == [Money::Currency.new(:eur)]
|
51
|
-
end
|
52
|
-
|
53
|
-
specify "#<=> should compare objects by priority" do
|
54
|
-
Money::Currency.new(:cad).should > Money::Currency.new(:usd)
|
55
|
-
Money::Currency.new(:usd).should < Money::Currency.new(:eur)
|
56
|
-
end
|
57
|
-
|
58
|
-
specify "#to_s" do
|
59
|
-
Money::Currency.new(:usd).to_s.should == "USD"
|
60
|
-
Money::Currency.new(:eur).to_s.should == "EUR"
|
61
|
-
end
|
62
|
-
|
63
|
-
specify "#
|
64
|
-
Money::Currency.new(:usd)
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
Money::Currency
|
77
|
-
Money::Currency
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
Money::Currency
|
90
|
-
Money::Currency
|
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
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Money::Currency do
|
6
|
+
|
7
|
+
specify "#initialize should lookup data from TABLE" do
|
8
|
+
with_custom_definitions do
|
9
|
+
Money::Currency::TABLE[:usd] = {:priority => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => ","}
|
10
|
+
Money::Currency::TABLE[:eur] = {:priority => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => ","}
|
11
|
+
|
12
|
+
currency = Money::Currency.new("USD")
|
13
|
+
currency.id.should == :usd
|
14
|
+
currency.priority.should == 1
|
15
|
+
currency.iso_code.should == "USD"
|
16
|
+
currency.name.should == "United States Dollar"
|
17
|
+
currency.decimal_mark.should == "."
|
18
|
+
currency.separator.should == "."
|
19
|
+
currency.thousands_separator.should == ","
|
20
|
+
currency.delimiter.should == ","
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
specify "#initialize should raise UnknownMoney::Currency with unknown currency" do
|
25
|
+
lambda { Money::Currency.new("xxx") }.should raise_error(Money::Currency::UnknownCurrency, /xxx/)
|
26
|
+
end
|
27
|
+
|
28
|
+
specify "#== should return true if self === other" do
|
29
|
+
currency = Money::Currency.new(:eur)
|
30
|
+
currency.should == currency
|
31
|
+
end
|
32
|
+
|
33
|
+
specify "#== should return true if the id is equal" do
|
34
|
+
Money::Currency.new(:eur).should == Money::Currency.new(:eur)
|
35
|
+
Money::Currency.new(:eur).should_not == Money::Currency.new(:usd)
|
36
|
+
end
|
37
|
+
|
38
|
+
specify "#eql? should return true if #== returns true" do
|
39
|
+
Money::Currency.new(:eur).eql?(Money::Currency.new(:eur)).should be true
|
40
|
+
Money::Currency.new(:eur).eql?(Money::Currency.new(:usd)).should be false
|
41
|
+
end
|
42
|
+
|
43
|
+
specify "#hash should return the same value for equal objects" do
|
44
|
+
Money::Currency.new(:eur).hash.should == Money::Currency.new(:eur).hash
|
45
|
+
Money::Currency.new(:eur).hash.should_not == Money::Currency.new(:usd).hash
|
46
|
+
end
|
47
|
+
|
48
|
+
specify "#hash can be used to return the intersection of Currency object arrays" do
|
49
|
+
intersection = [Money::Currency.new(:eur), Money::Currency.new(:usd)] & [Money::Currency.new(:eur)]
|
50
|
+
intersection.should == [Money::Currency.new(:eur)]
|
51
|
+
end
|
52
|
+
|
53
|
+
specify "#<=> should compare objects by priority" do
|
54
|
+
Money::Currency.new(:cad).should > Money::Currency.new(:usd)
|
55
|
+
Money::Currency.new(:usd).should < Money::Currency.new(:eur)
|
56
|
+
end
|
57
|
+
|
58
|
+
specify "#to_s" do
|
59
|
+
Money::Currency.new(:usd).to_s.should == "USD"
|
60
|
+
Money::Currency.new(:eur).to_s.should == "EUR"
|
61
|
+
end
|
62
|
+
|
63
|
+
specify "#to_currency" do
|
64
|
+
usd = Money::Currency.new(:usd)
|
65
|
+
usd.to_currency.should == usd
|
66
|
+
end
|
67
|
+
|
68
|
+
specify "#inspect" do
|
69
|
+
Money::Currency.new(:usd).inspect.should ==
|
70
|
+
%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, subunit: Cent>}
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
specify "#self.find should return currency matching given id" do
|
75
|
+
with_custom_definitions do
|
76
|
+
Money::Currency::TABLE[:usd] = { :priority => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
|
77
|
+
Money::Currency::TABLE[:eur] = { :priority => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
|
78
|
+
|
79
|
+
expected = Money::Currency.new(:eur)
|
80
|
+
Money::Currency.find(:eur).should == expected
|
81
|
+
Money::Currency.find(:EUR).should == expected
|
82
|
+
Money::Currency.find("eur").should == expected
|
83
|
+
Money::Currency.find("EUR").should == expected
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
specify "#self.find should return nil unless currency matching given id" do
|
88
|
+
with_custom_definitions do
|
89
|
+
Money::Currency::TABLE[:usd] = { :position => 1, :iso_code => "USD", :name => "United States Dollar", :symbol => "$", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
|
90
|
+
Money::Currency::TABLE[:eur] = { :position => 2, :iso_code => "EUR", :name => "Euro", :symbol => "€", :subunit => "Cent", :subunit_to_unit => 100, :decimal_mark => ".", :thousands_separator => "," }
|
91
|
+
|
92
|
+
expected = Money::Currency.new(:eur)
|
93
|
+
Money::Currency.find(:eur).should == expected
|
94
|
+
Money::Currency.find(:EUR).should == expected
|
95
|
+
Money::Currency.find("eur").should == expected
|
96
|
+
Money::Currency.find("EUR").should == expected
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
specify "#self.wrap should return nil if object is nil" do
|
101
|
+
Money::Currency.wrap(nil).should == nil
|
102
|
+
Money::Currency.wrap(Money::Currency.new(:usd)).should == Money::Currency.new(:usd)
|
103
|
+
Money::Currency.wrap(:usd).should == Money::Currency.new(:usd)
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
def with_custom_definitions(&block)
|
108
|
+
begin
|
109
|
+
old = Money::Currency::TABLE.dup
|
110
|
+
Money::Currency::TABLE.clear
|
111
|
+
yield
|
112
|
+
ensure
|
113
|
+
silence_warnings do
|
114
|
+
Money::Currency.const_set("TABLE", old)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
|
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
|