papercavalier-money 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,292 @@
1
+ # encoding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe Money do
6
+
7
+ describe "#new" do
8
+ it "rounds the given cents to an integer" do
9
+ Money.new(1.00, "USD").cents.should == 1
10
+ Money.new(1.01, "USD").cents.should == 1
11
+ Money.new(1.50, "USD").cents.should == 2
12
+ end
13
+
14
+ it "is associated to the singleton instance of Bank::VariableExchange by default" do
15
+ Money.new(0).bank.should be_equal(Money::Bank::VariableExchange.instance)
16
+ end
17
+
18
+ end
19
+
20
+ describe "#cents" do
21
+ it "returns the amount of cents passed to the constructor" do
22
+ Money.new(200_00, "USD").cents.should == 200_00
23
+ end
24
+
25
+ it "stores cents as an integer regardless of what is passed into the constructor" do
26
+ [ Money.new(100), 1.to_money, 1.00.to_money, BigDecimal('1.00').to_money ].each do |m|
27
+ m.cents.should == 100
28
+ m.cents.should be_an_instance_of(Fixnum)
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "#dollars" do
34
+ it "gets cents as dollars" do
35
+ Money.new_with_dollars(1).should == Money.new(100)
36
+ Money.new_with_dollars(1, "USD").should == Money.new(100, "USD")
37
+ Money.new_with_dollars(1, "EUR").should == Money.new(100, "EUR")
38
+ end
39
+
40
+ it "should respect :subunit_to_unit currency property" do
41
+ Money.new(1_00, "USD").dollars.should == 1
42
+ Money.new(1_000, "TND").dollars.should == 1
43
+ Money.new(1, "CLP").dollars.should == 1
44
+ end
45
+
46
+ it "should not loose precision" do
47
+ Money.new(100_37).dollars.should == 100.37
48
+ Money.new_with_dollars(100.37).dollars.should == 100.37
49
+ end
50
+ end
51
+
52
+ specify "#currency returns the currency passed to the constructor" do
53
+ Money.new(200_00, "USD").currency.should == Money::Currency.new("USD")
54
+ end
55
+
56
+ specify "#currency_string returns the iso_code of the currency object" do
57
+ Money.new(200_00, "USD").currency_as_string.should == Money::Currency.new("USD").to_s
58
+ Money.new(200_00, "USD").currency_as_string.should == "USD"
59
+ Money.new(200_00, "EUR").currency_as_string.should == "EUR"
60
+ Money.new(200_00, "YEN").currency_as_string.should == "YEN"
61
+ end
62
+
63
+ specify "#currency_string= set the currency object using the provided string" do
64
+ obj = Money.new(200_00, "USD")
65
+ obj.currency_as_string = "EUR"
66
+ obj.currency.should == Money::Currency.new("EUR")
67
+ obj.currency_as_string = "YEN"
68
+ obj.currency.should == Money::Currency.new("YEN")
69
+ obj.currency_as_string = "USD"
70
+ obj.currency.should == Money::Currency.new("USD")
71
+ end
72
+
73
+
74
+ specify "#exchange_to exchanges the amount via its exchange bank" do
75
+ money = Money.new(100_00, "USD")
76
+ money.bank.should_receive(:exchange_with).with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
77
+ money.exchange_to("EUR")
78
+ end
79
+
80
+ specify "#exchange_to exchanges the amount properly" do
81
+ money = Money.new(100_00, "USD")
82
+ money.bank.should_receive(:exchange_with).with(Money.new(100_00, Money::Currency.new("USD")), Money::Currency.new("EUR")).and_return(Money.new(200_00, Money::Currency.new('EUR')))
83
+ money.exchange_to("EUR").should == Money.new(200_00, "EUR")
84
+ end
85
+
86
+ specify "#hash should return the same value for equal objects" do
87
+ Money.new(1_00, :eur).hash.should == Money.new(1_00, :eur).hash
88
+ Money.new(2_00, :usd).hash.should == Money.new(2_00, :usd).hash
89
+ Money.new(1_00, :eur).hash.should_not == Money.new(2_00, :eur).hash
90
+ Money.new(1_00, :eur).hash.should_not == Money.new(1_00, :usd).hash
91
+ Money.new(1_00, :eur).hash.should_not == Money.new(2_00, :usd).hash
92
+ end
93
+
94
+ specify "#hash can be used to return the intersection of Money object arrays" do
95
+ intersection = [Money.new(1_00, :eur), Money.new(1_00, :usd)] & [Money.new(1_00, :eur)]
96
+ intersection.should == [Money.new(1_00, :eur)]
97
+ end
98
+
99
+
100
+ specify "Money.to_s works" do
101
+ Money.new(10_00).to_s.should == "10.00"
102
+ Money.new(400_08).to_s.should == "400.08"
103
+ Money.new(-237_43).to_s.should == "-237.43"
104
+ end
105
+
106
+ specify "Money.to_s should respect :subunit_to_unit currency property" do
107
+ Money.new(10_00, "BHD").to_s.should == "1.000"
108
+ Money.new(10_00, "CNY").to_s.should == "10.00"
109
+ end
110
+
111
+ specify "Money.to_s shouldn't have decimal when :subunit_to_unit is 1" do
112
+ Money.new(10_00, "CLP").to_s.should == "1000"
113
+ end
114
+
115
+ specify "Money.to_s should work with :subunit_to_unit == 5" do
116
+ Money.new(10_00, "MGA").to_s.should == "200.0"
117
+ end
118
+
119
+ specify "Money.to_s should respect :decimal_mark" do
120
+ Money.new(10_00, "BRL").to_s.should == "10,00"
121
+ end
122
+
123
+ specify "Money.to_d works" do
124
+ decimal = Money.new(10_00).to_d
125
+ decimal.should be_instance_of(BigDecimal)
126
+ decimal.should == 10.0
127
+ end
128
+
129
+ specify "Money.to_d should respect :subunit_to_unit currency property" do
130
+ decimal = Money.new(10_00, "BHD").to_d
131
+ decimal.should be_instance_of(BigDecimal)
132
+ decimal.should == 1.0
133
+ end
134
+
135
+ specify "Money.to_d should work with float :subunit_to_unit currency property" do
136
+ money = Money.new(10_00, "BHD")
137
+ money.currency.stub(:subunit_to_unit).and_return(1000.0)
138
+
139
+ decimal = money.to_d
140
+ decimal.should be_instance_of(BigDecimal)
141
+ decimal.should == 1.0
142
+ end
143
+
144
+ specify "Money.to_f works" do
145
+ Money.new(10_00).to_f.should == 10.0
146
+ end
147
+
148
+ specify "Money.to_f should respect :subunit_to_unit currency property" do
149
+ Money.new(10_00, "BHD").to_f.should == 1.0
150
+ end
151
+
152
+ specify "#symbol works as documented" do
153
+ currency = Money::Currency.new("EUR")
154
+ currency.should_receive(:symbol).and_return("€")
155
+ Money.empty(currency).symbol.should == "€"
156
+
157
+ currency = Money::Currency.new("EUR")
158
+ currency.should_receive(:symbol).and_return(nil)
159
+ Money.empty(currency).symbol.should == "¤"
160
+ end
161
+
162
+ describe "Money.empty" do
163
+ it "Money.empty creates a new Money object of 0 cents" do
164
+ Money.empty.should == Money.new(0)
165
+ end
166
+ end
167
+
168
+ describe "Money.ca_dollar" do
169
+ it "creates a new Money object of the given value in CAD" do
170
+ Money.ca_dollar(50).should == Money.new(50, "CAD")
171
+ end
172
+ end
173
+
174
+ describe "Money.us_dollar" do
175
+ it "creates a new Money object of the given value in USD" do
176
+ Money.us_dollar(50).should == Money.new(50, "USD")
177
+ end
178
+ end
179
+
180
+ describe "Money.euro" do
181
+ it "creates a new Money object of the given value in EUR" do
182
+ Money.euro(50).should == Money.new(50, "EUR")
183
+ end
184
+ end
185
+
186
+
187
+ describe "Money.new_with_dollars" do
188
+ it "converts given amount to cents" do
189
+ Money.new_with_dollars(1).should == Money.new(100)
190
+ Money.new_with_dollars(1, "USD").should == Money.new(100, "USD")
191
+ Money.new_with_dollars(1, "EUR").should == Money.new(100, "EUR")
192
+ end
193
+
194
+ it "should respect :subunit_to_unit currency property" do
195
+ Money.new_with_dollars(1, "USD").should == Money.new(1_00, "USD")
196
+ Money.new_with_dollars(1, "TND").should == Money.new(1_000, "TND")
197
+ Money.new_with_dollars(1, "CLP").should == Money.new(1, "CLP")
198
+ end
199
+
200
+ it "should not loose precision" do
201
+ Money.new_with_dollars(1234).cents.should == 1234_00
202
+ Money.new_with_dollars(100.37).cents.should == 100_37
203
+ Money.new_with_dollars(BigDecimal.new('1234')).cents.should == 1234_00
204
+ end
205
+
206
+ it "accepts a currency options" do
207
+ m = Money.new_with_dollars(1)
208
+ m.currency.should == Money.default_currency
209
+
210
+ m = Money.new_with_dollars(1, Money::Currency.wrap("EUR"))
211
+ m.currency.should == Money::Currency.wrap("EUR")
212
+
213
+ m = Money.new_with_dollars(1, "EUR")
214
+ m.currency.should == Money::Currency.wrap("EUR")
215
+ end
216
+
217
+ it "accepts a bank options" do
218
+ m = Money.new_with_dollars(1)
219
+ m.bank.should == Money.default_bank
220
+
221
+ m = Money.new_with_dollars(1, "EUR", bank = Object.new)
222
+ m.bank.should == bank
223
+ end
224
+
225
+ it "is associated to the singleton instance of Bank::VariableExchange by default" do
226
+ Money.new_with_dollars(0).bank.should be_equal(Money::Bank::VariableExchange.instance)
227
+ end
228
+ end
229
+
230
+ describe "split" do
231
+ specify "#split needs at least one party" do
232
+ lambda {Money.us_dollar(1).split(0)}.should raise_error(ArgumentError)
233
+ lambda {Money.us_dollar(1).split(-1)}.should raise_error(ArgumentError)
234
+ end
235
+
236
+
237
+ specify "#gives 1 cent to both people if we start with 2" do
238
+ Money.us_dollar(2).split(2).should == [Money.us_dollar(1), Money.us_dollar(1)]
239
+ end
240
+
241
+ specify "#split may distribute no money to some parties if there isnt enough to go around" do
242
+ Money.us_dollar(2).split(3).should == [Money.us_dollar(1), Money.us_dollar(1), Money.us_dollar(0)]
243
+ end
244
+
245
+ specify "#split does not lose pennies" do
246
+ Money.us_dollar(5).split(2).should == [Money.us_dollar(3), Money.us_dollar(2)]
247
+ end
248
+
249
+ specify "#split a dollar" do
250
+ moneys = Money.us_dollar(100).split(3)
251
+ moneys[0].cents.should == 34
252
+ moneys[1].cents.should == 33
253
+ moneys[2].cents.should == 33
254
+ end
255
+ end
256
+
257
+ describe "allocation" do
258
+ specify "#allocate takes no action when one gets all" do
259
+ Money.us_dollar(005).allocate([1.0]).should == [Money.us_dollar(5)]
260
+ end
261
+
262
+ specify "#allocate keeps currencies intact" do
263
+ Money.ca_dollar(005).allocate([1]).should == [Money.ca_dollar(5)]
264
+ end
265
+
266
+ specify "#allocate does not loose pennies" do
267
+ moneys = Money.us_dollar(5).allocate([0.3,0.7])
268
+ moneys[0].should == Money.us_dollar(2)
269
+ moneys[1].should == Money.us_dollar(3)
270
+ end
271
+
272
+ specify "#allocate does not loose pennies" do
273
+ moneys = Money.us_dollar(100).allocate([0.333,0.333, 0.333])
274
+ moneys[0].cents.should == 34
275
+ moneys[1].cents.should == 33
276
+ moneys[2].cents.should == 33
277
+ end
278
+
279
+ specify "#allocate requires total to be less then 1" do
280
+ lambda { Money.us_dollar(0.05).allocate([0.5,0.6]) }.should raise_error(ArgumentError)
281
+ end
282
+ end
283
+
284
+ describe "Money.add_rate" do
285
+ it "saves rate into current bank" do
286
+ Money.add_rate("EUR", "USD", 10)
287
+ Money.new(10_00, "EUR").exchange_to("USD").should == Money.new(100_00, "USD")
288
+ end
289
+ end
290
+
291
+ end
292
+
@@ -0,0 +1,28 @@
1
+ require "money"
2
+ require "rubygems"
3
+
4
+ #RSpec.configure do |config|
5
+ #end
6
+
7
+ def silence_warnings
8
+ old_verbose, $VERBOSE = $VERBOSE, nil
9
+ yield
10
+ ensure
11
+ $VERBOSE = old_verbose
12
+ end
13
+
14
+ def reset_i18n()
15
+ I18n.backend = I18n::Backend::Simple.new
16
+ end
17
+
18
+ def store_number_currency_formats(locale)
19
+ I18n.backend.store_translations(locale,
20
+ :number => {
21
+ :currency => {
22
+ :format => {
23
+ :delimiter => ",",
24
+ :separator => "."
25
+ }
26
+ }
27
+ })
28
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: papercavalier-money
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 3
7
+ - 7
8
+ - 1
9
+ version: 3.7.1
10
+ platform: ruby
11
+ authors:
12
+ - Tobias Luetke
13
+ - Hongli Lai
14
+ - Jeremy McNevin
15
+ - Shane Emmons
16
+ - Simone Carletti
17
+ - Jean-Louis Giordano
18
+ - Joshua Clayton
19
+ - Bodaniel Jeanes
20
+ - Tobias Schmidt
21
+ - Chris Kampmeier
22
+ - "Romain G\xC3\xA9rard"
23
+ - Eloy
24
+ - Josh Delsman
25
+ - Pelle Braendgaard
26
+ - Tom Lianza
27
+ - James Cotterill
28
+ - "Fran\xC3\xA7ois Beausoleil"
29
+ - Abhay Kumar
30
+ - pconnor
31
+ - Christian Billen
32
+ - Ilia Lobsanov
33
+ - Andrew White
34
+ autorequire:
35
+ bindir: bin
36
+ cert_chain: []
37
+
38
+ date: 2011-06-14 00:00:00 +01:00
39
+ default_executable:
40
+ dependencies:
41
+ - !ruby/object:Gem::Dependency
42
+ name: i18n
43
+ prerelease: false
44
+ requirement: &id001 !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ - 4
52
+ version: "0.4"
53
+ type: :runtime
54
+ version_requirements: *id001
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ prerelease: false
58
+ requirement: &id002 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 2
65
+ - 0
66
+ - 0
67
+ version: 2.0.0
68
+ type: :development
69
+ version_requirements: *id002
70
+ - !ruby/object:Gem::Dependency
71
+ name: yard
72
+ prerelease: false
73
+ requirement: &id003 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id003
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ prerelease: false
86
+ requirement: &id004 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :development
95
+ version_requirements: *id004
96
+ description: This library aids one in handling money and different currencies.
97
+ email:
98
+ - hongli@phusion.nl
99
+ - semmons99+RubyMoney@gmail.com
100
+ executables: []
101
+
102
+ extensions: []
103
+
104
+ extra_rdoc_files: []
105
+
106
+ files:
107
+ - lib/money/bank/base.rb
108
+ - lib/money/bank/variable_exchange.rb
109
+ - lib/money/core_extensions.rb
110
+ - lib/money/currency.rb
111
+ - lib/money/money/arithmetic.rb
112
+ - lib/money/money/formatting.rb
113
+ - lib/money/money/parsing.rb
114
+ - lib/money/money.rb
115
+ - lib/money.rb
116
+ - spec/bank/base_spec.rb
117
+ - spec/bank/variable_exchange_spec.rb
118
+ - spec/core_extensions_spec.rb
119
+ - spec/currency_spec.rb
120
+ - spec/money/arithmetic_spec.rb
121
+ - spec/money/formatting_spec.rb
122
+ - spec/money/parsing_spec.rb
123
+ - spec/money_spec.rb
124
+ - spec/spec_helper.rb
125
+ - CHANGELOG.md
126
+ - LICENSE
127
+ - README.md
128
+ - Rakefile
129
+ - .gemtest
130
+ - money.gemspec
131
+ has_rdoc: true
132
+ homepage: http://money.rubyforge.org
133
+ licenses: []
134
+
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ segments:
154
+ - 1
155
+ - 3
156
+ - 6
157
+ version: 1.3.6
158
+ requirements:
159
+ - json if you plan to import/export rates formatted as json
160
+ rubyforge_project: papercavalier-money
161
+ rubygems_version: 1.3.7
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: Money and currency exchange support library.
165
+ test_files: []
166
+