| Module | ActsAsPriceHelper |
| In: |
lib/acts_as_price_helper.rb
|
# File lib/acts_as_price_helper.rb, line 73
73: def columns
74: columns_in_doubles + columns_in_cents
75: end
# File lib/acts_as_price_helper.rb, line 65
65: def columns_in_cents
66: [:price_in_cents, "#{@column_name}_in_cents"]
67: end
# File lib/acts_as_price_helper.rb, line 69
69: def columns_in_doubles
70: [:price, @column_name]
71: end
Test the acts_as_price plugin for the specified column_name and model
Note the you can also specify the model in a before block in your tests
# File lib/acts_as_price_helper.rb, line 5
5: def test_acts_as_price_methods column_name, acts_as_price_model = nil
6: @acts_as_price_model = acts_as_price_model if acts_as_price_model
7:
8: @column_name = column_name
9: columns_in_cents.each do |column|
10: @acts_as_price_model.send(column).should == @acts_as_price_model.price_in_cents
11: end
12: columns_in_doubles.each do |column|
13: @acts_as_price_model.send(column).should == @acts_as_price_model.price
14: end
15:
16: #normal test for setter
17: test_setter_in_cents "144"
18: test_setter_in_doubles "1.41"
19:
20: #test for special cases
21: #test if 1.5 is returned as 1.50
22: test_setter_in_doubles "1.5"
23:
24: #test if float returns 2.05 correctly
25: #float can return 2.05 as 2.04 in some cases
26: test_setter_in_doubles "2.05"
27:
28: #test an empty_setter
29: test_setter_in_cents ""
30: test_setter_in_doubles ""
31: end
# File lib/acts_as_price_helper.rb, line 33
33: def test_setter_in_cents cents
34: columns_in_cents.each do |setter|
35: @acts_as_price_model.send("#{setter}=", cents)
36: columns_in_cents.each do |getter|
37: @acts_as_price_model.send(getter).should == cents.to_i
38: end
39: columns_in_doubles.each do |getter|
40: if cents == ""
41: @acts_as_price_model.send(getter).should == ""
42: else
43: @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s)
44: end
45: end
46: end
47: end
# File lib/acts_as_price_helper.rb, line 49
49: def test_setter_in_doubles double
50: columns_in_doubles.each do |setter|
51: @acts_as_price_model.send("#{setter}=", double)
52: columns_in_cents.each do |getter|
53: @acts_as_price_model.send(getter).should == (double.to_f * 100).to_s.to_i
54: end
55: columns_in_doubles.each do |getter|
56: if double == ""
57: @acts_as_price_model.send(getter).should == ""
58: else
59: @acts_as_price_model.send(getter).should == sprintf("%.2f", double)
60: end
61: end
62: end
63: end