Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 5526 Bytes
1 | require 'test_helper' |
2 | |
3 | class CurrencyTest < ActiveSupport::TestCase |
4 | test "must have name" do |
5 | c = Currency.new |
6 | assert !c.save |
7 | end |
8 | |
9 | test "must default to USD" do |
10 | c = Currency.get_best_currency( nil ) |
11 | assert c.code == "USD" |
12 | end |
13 | |
14 | test "simplify currency string" do |
15 | str = Currency.simplify( "12,23'", "431" ) |
16 | assert str == "1223.431" |
17 | end |
18 | |
19 | test "simplify negative currency string" do |
20 | str = Currency.simplify( " - 1!2,23'", "431" ) |
21 | assert str == "-1223.431" |
22 | end |
23 | |
24 | test "add currency string components" do |
25 | ary = Currency.add( "12,23'", "43189621", "4", "56810379" ) |
26 | assert ary == [ "1228", "0" ] |
27 | end |
28 | |
29 | test "add negative currency string components" do |
30 | ary = Currency.add( "12,23'", "43189621", "-4", "56810379" ) |
31 | assert ary == [ "1218", "86379242" ] |
32 | end |
33 | |
34 | test "subtract currency string components" do |
35 | ary = Currency.subtract( "12,28'", "0", "4", "56810379" ) |
36 | assert ary == [ "1223", "43189621" ] |
37 | end |
38 | |
39 | test "multiply currency string components by integer in String" do |
40 | ary = Currency.multiply( "12,23'", "431", "5" ) |
41 | assert ary == [ "6117", "155" ] |
42 | end |
43 | |
44 | test "multiply currency string components by negative integer in String" do |
45 | ary = Currency.multiply( "12,23'", "431", " -5" ) |
46 | assert ary == [ "-6117", "155" ] |
47 | end |
48 | |
49 | test "divide currency string components by integer in String" do |
50 | ary = Currency.divide( "!611,7", "155", "5" ) |
51 | assert ary == [ "1223", "431" ] |
52 | end |
53 | |
54 | test "divide negative currency string components by integer in String" do |
55 | ary = Currency.divide( "-!611,7", "155", "5" ) |
56 | assert ary == [ "-1223", "431" ] |
57 | end |
58 | |
59 | test "multiply currency string components by integer in BigDecimal" do |
60 | ary = Currency.multiply( "12,23'", "431", BigDecimal.new( "5" ) ) |
61 | assert ary == [ "6117", "155" ] |
62 | end |
63 | |
64 | test "multiply negative currency string components by integer in BigDecimal" do |
65 | ary = Currency.multiply( "-12,23'", "431", BigDecimal.new( "5" ) ) |
66 | assert ary == [ "-6117", "155" ] |
67 | end |
68 | |
69 | test "multiply negative currency string components by negative integer in BigDecimal" do |
70 | ary = Currency.multiply( "-12,23'", "431", BigDecimal.new( "-5" ) ) |
71 | assert ary == [ "6117", "155" ] |
72 | end |
73 | |
74 | test "divide currency string components by integer in BigDecimal" do |
75 | ary = Currency.divide( "'6,117", "155", BigDecimal.new( "5" ) ) |
76 | assert ary == [ "1223", "431" ] |
77 | end |
78 | |
79 | test "multiply currency string components by negative fraction in String" do |
80 | ary = Currency.multiply( "12,23'", "431", "-0.25" ) |
81 | assert ary == [ "-305", "85775" ] |
82 | end |
83 | |
84 | test "divide negative currency string components by negative fraction in String" do |
85 | ary = Currency.divide( "-305'", "85775", "-0.25" ) |
86 | assert ary == [ "1223", "431" ] |
87 | end |
88 | |
89 | test "multiply currency string components by negative fraction in BigDecimal" do |
90 | ary = Currency.multiply( "12,23'", "431", BigDecimal.new( "-0.25" ) ) |
91 | assert ary == [ "-305", "85775" ] |
92 | end |
93 | |
94 | test "divide currency string components by negative fraction in BigDecimal" do |
95 | ary = Currency.divide( "-305", "85775", BigDecimal.new( "-0.25" ) ) |
96 | assert ary == [ "1223", "431" ] |
97 | end |
98 | |
99 | test "mathematical rounding algorithm works" do |
100 | c = dummy_currency |
101 | |
102 | n = c.round( "10.995" ) |
103 | assert n = "11.00" |
104 | |
105 | n = c.round( "10.494" ) |
106 | assert n = "10.49" |
107 | |
108 | n = c.round( "10.495" ) |
109 | assert n = "10.50" |
110 | end |
111 | |
112 | test "round_up rounding algorithm works" do |
113 | c = dummy_currency; c.rounding_algorithm = "round_up" |
114 | |
115 | n = c.round( "10.991" ) |
116 | assert n = "11.00" |
117 | |
118 | n = c.round( "10.495" ) |
119 | assert n = "10.50" |
120 | |
121 | n = c.round( "10.499" ) |
122 | assert n = "10.50" |
123 | end |
124 | |
125 | test "round_down rounding algorithm works" do |
126 | c = dummy_currency; c.rounding_algorithm = "round_down" |
127 | |
128 | n = c.round( "10.999" ) |
129 | assert n = "10.99" |
130 | |
131 | n = c.round( "10.495" ) |
132 | assert n = "10.49" |
133 | |
134 | n = c.round( "10.499" ) |
135 | assert n = "10.49" |
136 | end |
137 | |
138 | test "argentinian rounding algorithm works" do |
139 | c = dummy_currency; c.rounding_algorithm = "argentinian"; c.decimal_precision = 4 |
140 | |
141 | # This test is only as good as my understanding of Argentinian rounding! |
142 | # http://stackoverflow.com/questions/5134237/swiss-and-argentinian-currency-fourth-decimal-digit-rounding |
143 | |
144 | n = c.round( "10.9929" ) |
145 | assert n = "10.990" |
146 | |
147 | n = c.round( "10.9939" ) |
148 | assert n = "10.995" |
149 | |
150 | n = c.round( "10.9979" ) |
151 | assert n = "10.995" |
152 | |
153 | n = c.round( "10.9989" ) |
154 | assert n = "11.000" |
155 | end |
156 | |
157 | test "swiss rounding algorithm works" do |
158 | c = dummy_currency; c.rounding_algorithm = "swiss"; c.decimal_precision = 4 |
159 | |
160 | # This test is only as good as my understanding of Swiss rounding! |
161 | # http://stackoverflow.com/questions/5134237/swiss-and-argentinian-currency-fourth-decimal-digit-rounding |
162 | |
163 | n = c.round( "10.9259" ) |
164 | assert n = "10.90" |
165 | |
166 | n = c.round( "10.9269" ) |
167 | assert n = "10.95" |
168 | |
169 | n = c.round( "10.9759" ) |
170 | assert n = "11.00" |
171 | |
172 | n = c.round( "10.92" ) |
173 | assert n = "10.90" |
174 | |
175 | n = c.round( "10.93" ) |
176 | assert n = "10.95" |
177 | |
178 | n = c.round( "10.98" ) |
179 | assert n = "11.00" |
180 | end |
181 | |
182 | def dummy_currency |
183 | Currency.new( |
184 | :name => "Dummy", |
185 | :code => "DUMMY", |
186 | :rounding_algorithm => "mathematical", |
187 | :integer_template => "0,000,000,000,000", |
188 | :delimiter => ".", |
189 | :fraction_template => "00", |
190 | :decimal_precision => 2, |
191 | :symbol => "D", |
192 | :show_after_number => false |
193 | ) |
194 | end |
195 | end |