extensions 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/class'
4
+
5
+ class TC_Class < Test::Unit::TestCase
6
+
7
+ class ClassExample1
8
+ attr_reader :a, :b, :c
9
+ autoinit(:a, :b) do
10
+ @c = @a + @b
11
+ end
12
+ end
13
+
14
+ # Test autoinit with a block.
15
+ def test_autoinit_1
16
+ eg = ClassExample1.new(15, 3)
17
+ assert_equal(15, eg.a)
18
+ assert_equal(3, eg.b)
19
+ assert_equal(18, eg.c)
20
+ end
21
+
22
+ class ClassExample2
23
+ attr_reader :x
24
+ autoinit(:x)
25
+ end
26
+
27
+ # Test autoinit without a block.
28
+ def test_autoinit_2
29
+ eg = ClassExample2.new("Hello")
30
+ assert_equal("Hello", eg.x)
31
+ end
32
+
33
+ end # class TC_Class
34
+
@@ -0,0 +1,87 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/enumerable'
4
+ require 'set'
5
+
6
+ class TC_Enumerable < Test::Unit::TestCase
7
+ def setup
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ def test_build_hash
14
+ input = [1, 2, 3]
15
+ expected_output = { 1 => 1, 4 => 8, 9 => 27 }
16
+ output = input.build_hash { |n| [n**2, n**3] }
17
+ assert_equal(expected_output, output)
18
+
19
+ input = { "x" => 1, "y" => 2 }
20
+ expected_output = { :x => 1, :y => 2 }
21
+ output = input.build_hash { |a,b| [a.intern, b] }
22
+ assert_equal(expected_output, output)
23
+
24
+ input = [[1,1], [2,2]]
25
+ expected_output = { 1 => 1, 2 => 2 }
26
+ output = input.build_hash { |a,b| [a, b] }
27
+ assert_equal(expected_output, output)
28
+ end
29
+
30
+ def test_mapf
31
+ assert_equal([1,2,3], %w[1 2 3].mapf(:to_i))
32
+ assert_equal(%w[John Terry Fiona], %w[john terry fiona].mapf(:capitalize))
33
+ end
34
+
35
+ def test_collectf
36
+ assert_equal([1,2,3], %w[1 2 3].collectf(:to_i))
37
+ assert_equal(%w[John Terry Fiona], %w[john terry fiona].collectf(:capitalize))
38
+ end
39
+
40
+ def test_includes
41
+ assert([1, 2, 3].includes?(2))
42
+ assert("one\ntwo\nthree\n".includes?(5) == false)
43
+ assert({:t => 5, :u => 10, :e => 51}.includes?([:u, 10]))
44
+ end
45
+
46
+ def test_contains
47
+ assert([1, 2, 3].contains?(2))
48
+ assert("one\ntwo\nthree\n".contains?(5) == false)
49
+ assert({:t => 5, :u => 10, :e => 51}.contains?([:u, 10]))
50
+ end
51
+
52
+ def test_has
53
+ assert([1, 2, 3].has?(2))
54
+ assert("one\ntwo\nthree\n".has?(5) == false)
55
+ assert("one\ntwo\nthree\n".has?("two\n"))
56
+ assert({:t => 5, :u => 10, :e => 51}.has?([:u, 10]))
57
+ end
58
+
59
+ def test_map_with_index
60
+ columns = [ ["EMP_ID","INTEGER"], ["DIVISION", "CHAR(10)"] ]
61
+ data = ["11", "Retail"]
62
+ string_data = columns.map_with_index { |c, i|
63
+ col, type = c
64
+ datum = data[i]
65
+ datum = "'#{datum}'" if type =~ /char/i
66
+ datum
67
+ }
68
+ expected_output = ["11", "'Retail'"]
69
+ assert_equal(expected_output, string_data)
70
+ end
71
+
72
+ def test_partition_by
73
+ assert_equal({}, [].partition_by { |x| x })
74
+
75
+ result = (1..5).partition_by { |n| n % 3 }
76
+ expected_result = { 0 => [3], 1 => [1, 4], 2 => [2,5] }
77
+ assert_equal(expected_result, result)
78
+
79
+ result = ["I had", 1, "dollar and", 50, "cents"].partition_by { |e| e.class }
80
+ expected_result = { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
81
+ assert_equal(expected_result, result)
82
+ end
83
+
84
+ # Object#in? is tested in tc_object.rb.
85
+
86
+ end # class TC_Enumerable
87
+
@@ -0,0 +1,34 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/hash'
4
+
5
+ class TC_Hash < Test::Unit::TestCase
6
+ def test_select!
7
+ # Empty hash.
8
+ a = {}
9
+ assert_equal(nil, a.select! {false}, "return nil if hash not changed")
10
+ assert_equal({}, a, "hash is not changed")
11
+
12
+ a = {}
13
+ assert_equal(nil, a.select! {true}, "return nil if hash not changed")
14
+ assert_equal({}, a, "hash is not changed")
15
+
16
+ # Non-empty hash.
17
+ a = {0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'}
18
+ assert_equal({0 => 'a', 2 => 'c'}, a.select! {|x,y| x % 2 == 0}, "select even numbers")
19
+ assert_equal({0 => 'a', 2 => 'c'}, a, "select even numbers")
20
+
21
+ a = {0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'}
22
+ assert_equal({1 => 'b'}, a.select! {|x,y| y == 'b'}, "select one member")
23
+ assert_equal({1 => 'b'}, a, "select one member")
24
+
25
+ a = {0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'}
26
+ assert_equal(nil, a.select! {true}, "return nil if hash not changed")
27
+ assert_equal({0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'}, a, "select all")
28
+
29
+ a = {0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'}
30
+ assert_equal({}, a.select! {false}, "select none")
31
+ assert_equal({}, a, "select none")
32
+ end
33
+ end # class TC_Hash
34
+
@@ -0,0 +1,32 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/io'
4
+ require 'tempfile'
5
+
6
+ class TC_IO < Test::Unit::TestCase
7
+
8
+ def setup
9
+ tmp_dir = ENV["TMP"] || ENV["TEMP"] || "/tmp"
10
+ raise "Can't find TMP directory" unless File.directory?(tmp_dir)
11
+ @path = File.join(tmp_dir, "ruby_io_test")
12
+ end
13
+
14
+ # Test IO.write
15
+ def test_io_write
16
+ data_in = "Test data\n"
17
+ nbytes = File.write(@path, data_in)
18
+ data_out = File.read(@path) # This is standard class method.
19
+ assert_equal(data_in, data_out)
20
+ assert_equal(data_out.size, nbytes)
21
+ end
22
+
23
+ # Test IO.writelines
24
+ def test_io_writelines
25
+ data_in = %w[one two three four five]
26
+ File.writelines(@path, data_in)
27
+ data_out = File.readlines(@path) # This is standard class method.
28
+ assert_equal(data_in, data_out.map { |l| l.chomp })
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,435 @@
1
+
2
+ require 'test/unit'
3
+ require 'extensions/numeric'
4
+
5
+ class TC_Numeric < Test::Unit::TestCase
6
+ def test_even
7
+ (-100..100).step(2) do |n|
8
+ assert(n.even? == true)
9
+ end
10
+ (-101..101).step(2) do |n|
11
+ assert(n.even? == false)
12
+ end
13
+ end
14
+
15
+ def test_odd
16
+ (-101..101).step(2) do |n|
17
+ assert(n.odd? == true)
18
+ end
19
+ (-100..100).step(2) do |n|
20
+ assert(n.odd? == false)
21
+ end
22
+ end
23
+ end # class TC_Numeric
24
+
25
+ # Part of Austin Ziegler's code; see copyright notice above.
26
+ class TC_FormatNumber < Test::Unit::TestCase
27
+ def test_integer
28
+ assert_equal("1", 1.format_s(:us))
29
+ assert_equal("12", 12.format_s(:us))
30
+ assert_equal("123", 123.format_s(:us))
31
+ assert_equal("1,234", 1234.format_s(:us))
32
+ assert_equal("12,345", 12345.format_s(:us))
33
+ assert_equal("123,456", 123456.format_s(:us))
34
+ assert_equal("1,234,567", 1234567.format_s(:us))
35
+ assert_equal("12,345,678", 12345678.format_s(:us))
36
+ assert_equal("123,456,789", 123456789.format_s(:us))
37
+ assert_equal("1,234,567,890", 1234567890.format_s(:us))
38
+ assert_equal("1", 1.format_s)
39
+ assert_equal("12", 12.format_s)
40
+ assert_equal("123", 123.format_s)
41
+ assert_equal("1,234", 1234.format_s)
42
+ assert_equal("12,345", 12345.format_s)
43
+ assert_equal("123,456", 123456.format_s)
44
+ assert_equal("1,234,567", 1234567.format_s)
45
+ assert_equal("12,345,678", 12345678.format_s)
46
+ assert_equal("123,456,789", 123456789.format_s)
47
+ assert_equal("1,234,567,890", 1234567890.format_s)
48
+ end
49
+
50
+ def test_usd_integer
51
+ assert_equal("$1", 1.format_s(:usd))
52
+ assert_equal("$12", 12.format_s(:usd))
53
+ assert_equal("$123", 123.format_s(:usd))
54
+ assert_equal("$1,234", 1234.format_s(:usd))
55
+ assert_equal("$12,345", 12345.format_s(:usd))
56
+ assert_equal("$123,456", 123456.format_s(:usd))
57
+ assert_equal("$1,234,567", 1234567.format_s(:usd))
58
+ assert_equal("$12,345,678", 12345678.format_s(:usd))
59
+ assert_equal("$123,456,789", 123456789.format_s(:usd))
60
+ assert_equal("$1,234,567,890", 1234567890.format_s(:usd))
61
+ end
62
+
63
+ def test_percent_integer
64
+ assert_equal("1%", 1.format_s(:percent))
65
+ assert_equal("12%", 12.format_s(:percent))
66
+ assert_equal("123%", 123.format_s(:percent))
67
+ assert_equal("1,234%", 1234.format_s(:percent))
68
+ assert_equal("12,345%", 12345.format_s(:percent))
69
+ assert_equal("123,456%", 123456.format_s(:percent))
70
+ assert_equal("1,234,567%", 1234567.format_s(:percent))
71
+ assert_equal("12,345,678%", 12345678.format_s(:percent))
72
+ assert_equal("123,456,789%", 123456789.format_s(:percent))
73
+ assert_equal("1,234,567,890%", 1234567890.format_s(:percent))
74
+ end
75
+
76
+ def test_euro_integer
77
+ assert_equal("�1", 1.format_s(:euro))
78
+ assert_equal("�12", 12.format_s(:euro))
79
+ assert_equal("�123", 123.format_s(:euro))
80
+ assert_equal("�1 234", 1234.format_s(:euro))
81
+ assert_equal("�12 345", 12345.format_s(:euro))
82
+ assert_equal("�123 456", 123456.format_s(:euro))
83
+ assert_equal("�1 234 567", 1234567.format_s(:euro))
84
+ assert_equal("�12 345 678", 12345678.format_s(:euro))
85
+ assert_equal("�123 456 789", 123456789.format_s(:euro))
86
+ assert_equal("�1 234 567 890", 1234567890.format_s(:euro))
87
+ end
88
+
89
+ def test_negative_integer
90
+ assert_equal("-1", -1.format_s(:us))
91
+ assert_equal("-12", -12.format_s(:us))
92
+ assert_equal("-123", -123.format_s(:us))
93
+ assert_equal("-1,234", -1234.format_s(:us))
94
+ assert_equal("-12,345", -12345.format_s(:us))
95
+ assert_equal("-123,456", -123456.format_s(:us))
96
+ assert_equal("-1,234,567", -1234567.format_s(:us))
97
+ assert_equal("-12,345,678", -12345678.format_s(:us))
98
+ assert_equal("-123,456,789", -123456789.format_s(:us))
99
+ assert_equal("-1,234,567,890", -1234567890.format_s(:us))
100
+ assert_equal("-1", -1.format_s)
101
+ assert_equal("-12", -12.format_s)
102
+ assert_equal("-123", -123.format_s)
103
+ assert_equal("-1,234", -1234.format_s)
104
+ assert_equal("-12,345", -12345.format_s)
105
+ assert_equal("-123,456", -123456.format_s)
106
+ assert_equal("-1,234,567", -1234567.format_s)
107
+ assert_equal("-12,345,678", -12345678.format_s)
108
+ assert_equal("-123,456,789", -123456789.format_s)
109
+ assert_equal("-1,234,567,890", -1234567890.format_s)
110
+ end
111
+
112
+ def test_negative_usd_integer
113
+ assert_equal("$-1", -1.format_s(:usd))
114
+ assert_equal("$-12", -12.format_s(:usd))
115
+ assert_equal("$-123", -123.format_s(:usd))
116
+ assert_equal("$-1,234", -1234.format_s(:usd))
117
+ assert_equal("$-12,345", -12345.format_s(:usd))
118
+ assert_equal("$-123,456", -123456.format_s(:usd))
119
+ assert_equal("$-1,234,567", -1234567.format_s(:usd))
120
+ assert_equal("$-12,345,678", -12345678.format_s(:usd))
121
+ assert_equal("$-123,456,789", -123456789.format_s(:usd))
122
+ assert_equal("$-1,234,567,890", -1234567890.format_s(:usd))
123
+ end
124
+
125
+ def test_negative_percent_integer
126
+ assert_equal("-1%", -1.format_s(:percent))
127
+ assert_equal("-12%", -12.format_s(:percent))
128
+ assert_equal("-123%", -123.format_s(:percent))
129
+ assert_equal("-1,234%", -1234.format_s(:percent))
130
+ assert_equal("-12,345%", -12345.format_s(:percent))
131
+ assert_equal("-123,456%", -123456.format_s(:percent))
132
+ assert_equal("-1,234,567%", -1234567.format_s(:percent))
133
+ assert_equal("-12,345,678%", -12345678.format_s(:percent))
134
+ assert_equal("-123,456,789%", -123456789.format_s(:percent))
135
+ assert_equal("-1,234,567,890%", -1234567890.format_s(:percent))
136
+ end
137
+
138
+ def test_negative_euro_integer
139
+ assert_equal("�-1", -1.format_s(:euro))
140
+ assert_equal("�-12", -12.format_s(:euro))
141
+ assert_equal("�-123", -123.format_s(:euro))
142
+ assert_equal("�-1 234", -1234.format_s(:euro))
143
+ assert_equal("�-12 345", -12345.format_s(:euro))
144
+ assert_equal("�-123 456", -123456.format_s(:euro))
145
+ assert_equal("�-1 234 567", -1234567.format_s(:euro))
146
+ assert_equal("�-12 345 678", -12345678.format_s(:euro))
147
+ assert_equal("�-123 456 789", -123456789.format_s(:euro))
148
+ assert_equal("�-1 234 567 890", -1234567890.format_s(:euro))
149
+ end
150
+
151
+ def test_spaces
152
+ assert_equal("1", 1.format_s(:us, :sep => " "))
153
+ assert_equal("12", 12.format_s(:us, :sep => " "))
154
+ assert_equal("123", 123.format_s(:us, :sep => " "))
155
+ assert_equal("1 234", 1234.format_s(:us, :sep => " "))
156
+ assert_equal("12 345", 12345.format_s(:us, :sep => " "))
157
+ assert_equal("123 456", 123456.format_s(:us, :sep => " "))
158
+ assert_equal("1 234 567", 1234567.format_s(:us, :sep => " "))
159
+ assert_equal("12 345 678", 12345678.format_s(:us, :sep => " "))
160
+ assert_equal("123 456 789", 123456789.format_s(:us, :sep => " "))
161
+ assert_equal("1 234 567 890", 1234567890.format_s(:us, :sep => " "))
162
+ end
163
+
164
+ def test_negative_spaces
165
+ assert_equal("-1", -1.format_s(:us, :sep => " "))
166
+ assert_equal("-12", -12.format_s(:us, :sep => " "))
167
+ assert_equal("-123", -123.format_s(:us, :sep => " "))
168
+ assert_equal("-1 234", -1234.format_s(:us, :sep => " "))
169
+ assert_equal("-12 345", -12345.format_s(:us, :sep => " "))
170
+ assert_equal("-123 456", -123456.format_s(:us, :sep => " "))
171
+ assert_equal("-1 234 567", -1234567.format_s(:us, :sep => " "))
172
+ assert_equal("-12 345 678", -12345678.format_s(:us, :sep => " "))
173
+ assert_equal("-123 456 789", -123456789.format_s(:us, :sep => " "))
174
+ assert_equal("-1 234 567 890", -1234567890.format_s(:us, :sep => " "))
175
+ end
176
+
177
+ def test_size
178
+ assert_equal("1", 1.format_s(:us, :size => 2))
179
+ assert_equal("12", 12.format_s(:us, :size => 2))
180
+ assert_equal("1,23", 123.format_s(:us, :size => 2))
181
+ assert_equal("12,34", 1234.format_s(:us, :size => 2))
182
+ assert_equal("1,23,45", 12345.format_s(:us, :size => 2))
183
+ assert_equal("12,34,56", 123456.format_s(:us, :size => 2))
184
+ assert_equal("1,23,45,67", 1234567.format_s(:us, :size => 2))
185
+ assert_equal("12,34,56,78", 12345678.format_s(:us, :size => 2))
186
+ assert_equal("1,23,45,67,89", 123456789.format_s(:us, :size => 2))
187
+ assert_equal("12,34,56,78,90", 1234567890.format_s(:us, :size => 2))
188
+ end
189
+
190
+ def test_negative_size
191
+ assert_equal("-1", -1.format_s(:us, :size => 2))
192
+ assert_equal("-12", -12.format_s(:us, :size => 2))
193
+ assert_equal("-1,23", -123.format_s(:us, :size => 2))
194
+ assert_equal("-12,34", -1234.format_s(:us, :size => 2))
195
+ assert_equal("-1,23,45", -12345.format_s(:us, :size => 2))
196
+ assert_equal("-12,34,56", -123456.format_s(:us, :size => 2))
197
+ assert_equal("-1,23,45,67", -1234567.format_s(:us, :size => 2))
198
+ assert_equal("-12,34,56,78", -12345678.format_s(:us, :size => 2))
199
+ assert_equal("-1,23,45,67,89", -123456789.format_s(:us, :size => 2))
200
+ assert_equal("-12,34,56,78,90", -1234567890.format_s(:us, :size => 2))
201
+ end
202
+
203
+ def test_usd_decimal
204
+ assert_equal("$1.1", 1.1.format_s(:usd))
205
+ assert_equal("$12.12", 12.12.format_s(:usd))
206
+ assert_equal("$123.123", 123.123.format_s(:usd))
207
+ assert_equal("$1,234.1234", 1234.1234.format_s(:usd))
208
+ assert_equal("$12,345.12345", 12345.12345.format_s(:usd))
209
+ assert_equal("$123,456.123456", 123456.123456.format_s(:usd))
210
+ assert_equal("$1,234,567.1234567", 1234567.1234567.format_s(:usd))
211
+ assert_equal("$12,345,678.1234568", 12345678.12345678.format_s(:usd))
212
+ assert_equal("$123,456,789.123457", 123456789.123456789.format_s(:usd))
213
+ assert_equal("$1,234,567,890.01235", 1234567890.0123456789.format_s(:usd))
214
+ end
215
+
216
+ def test_percent_decimal
217
+ assert_equal("1.1%", 1.1.format_s(:percent))
218
+ assert_equal("12.12%", 12.12.format_s(:percent))
219
+ assert_equal("123.123%", 123.123.format_s(:percent))
220
+ assert_equal("1,234.1234%", 1234.1234.format_s(:percent))
221
+ assert_equal("12,345.12345%", 12345.12345.format_s(:percent))
222
+ assert_equal("123,456.123456%", 123456.123456.format_s(:percent))
223
+ assert_equal("1,234,567.1234567%", 1234567.1234567.format_s(:percent))
224
+ assert_equal("12,345,678.1234568%", 12345678.12345678.format_s(:percent))
225
+ assert_equal("123,456,789.123457%", 123456789.123456789.format_s(:percent))
226
+ assert_equal("1,234,567,890.01235%", 1234567890.0123456789.format_s(:percent))
227
+ end
228
+
229
+ def test_euro_decimal
230
+ assert_equal("�1,1", 1.1.format_s(:euro))
231
+ assert_equal("�12,12", 12.12.format_s(:euro))
232
+ assert_equal("�123,123", 123.123.format_s(:euro))
233
+ assert_equal("�1 234,1234", 1234.1234.format_s(:euro))
234
+ assert_equal("�12 345,12345", 12345.12345.format_s(:euro))
235
+ assert_equal("�123 456,123456", 123456.123456.format_s(:euro))
236
+ assert_equal("�1 234 567,1234567", 1234567.1234567.format_s(:euro))
237
+ assert_equal("�12 345 678,1234568", 12345678.12345678.format_s(:euro))
238
+ assert_equal("�123 456 789,123457", 123456789.123456789.format_s(:euro))
239
+ assert_equal("�1 234 567 890,01235", 1234567890.0123456789.format_s(:euro))
240
+ end
241
+
242
+ def test_decimal
243
+ assert_equal("1.1", 1.1.format_s(:us))
244
+ assert_equal("12.12", 12.12.format_s(:us))
245
+ assert_equal("123.123", 123.123.format_s(:us))
246
+ assert_equal("1,234.1234", 1234.1234.format_s(:us))
247
+ assert_equal("12,345.12345", 12345.12345.format_s(:us))
248
+ assert_equal("123,456.123456", 123456.123456.format_s(:us))
249
+ assert_equal("1,234,567.1234567", 1234567.1234567.format_s(:us))
250
+ assert_equal("12,345,678.1234568", 12345678.12345678.format_s(:us))
251
+ assert_equal("123,456,789.123457", 123456789.123456789.format_s(:us))
252
+ assert_equal("1,234,567,890.01235", 1234567890.0123456789.format_s(:us))
253
+ assert_equal("1.1", 1.1.format_s)
254
+ assert_equal("12.12", 12.12.format_s)
255
+ assert_equal("123.123", 123.123.format_s)
256
+ assert_equal("1,234.1234", 1234.1234.format_s)
257
+ assert_equal("12,345.12345", 12345.12345.format_s)
258
+ assert_equal("123,456.123456", 123456.123456.format_s)
259
+ assert_equal("1,234,567.1234567", 1234567.1234567.format_s)
260
+ assert_equal("12,345,678.1234568", 12345678.12345678.format_s)
261
+ assert_equal("123,456,789.123457", 123456789.123456789.format_s)
262
+ assert_equal("1,234,567,890.01235", 1234567890.0123456789.format_s)
263
+ end
264
+
265
+ def test_negative_decimal
266
+ assert_equal("-1.1", -1.1.format_s(:us))
267
+ assert_equal("-12.12", -12.12.format_s(:us))
268
+ assert_equal("-123.123", -123.123.format_s(:us))
269
+ assert_equal("-1,234.1234", -1234.1234.format_s(:us))
270
+ assert_equal("-12,345.12345", -12345.12345.format_s(:us))
271
+ assert_equal("-123,456.123456", -123456.123456.format_s(:us))
272
+ assert_equal("-1,234,567.1234567", -1234567.1234567.format_s(:us))
273
+ assert_equal("-12,345,678.1234568", -12345678.12345678.format_s(:us))
274
+ assert_equal("-123,456,789.123457", -123456789.123456789.format_s(:us))
275
+ assert_equal("-1,234,567,890.01235", -1234567890.0123456789.format_s(:us))
276
+ end
277
+
278
+ def test_negative_usd_decimal
279
+ assert_equal("$-1.1", -1.1.format_s(:usd))
280
+ assert_equal("$-12.12", -12.12.format_s(:usd))
281
+ assert_equal("$-123.123", -123.123.format_s(:usd))
282
+ assert_equal("$-1,234.1234", -1234.1234.format_s(:usd))
283
+ assert_equal("$-12,345.12345", -12345.12345.format_s(:usd))
284
+ assert_equal("$-123,456.123456", -123456.123456.format_s(:usd))
285
+ assert_equal("$-1,234,567.1234567", -1234567.1234567.format_s(:usd))
286
+ assert_equal("$-12,345,678.1234568", -12345678.12345678.format_s(:usd))
287
+ assert_equal("$-123,456,789.123457", -123456789.123456789.format_s(:usd))
288
+ assert_equal("$-1,234,567,890.01235", -1234567890.0123456789.format_s(:usd))
289
+ end
290
+
291
+ def test_negative_percent_decimal
292
+ assert_equal("-1.1%", -1.1.format_s(:percent))
293
+ assert_equal("-12.12%", -12.12.format_s(:percent))
294
+ assert_equal("-123.123%", -123.123.format_s(:percent))
295
+ assert_equal("-1,234.1234%", -1234.1234.format_s(:percent))
296
+ assert_equal("-12,345.12345%", -12345.12345.format_s(:percent))
297
+ assert_equal("-123,456.123456%", -123456.123456.format_s(:percent))
298
+ assert_equal("-1,234,567.1234567%", -1234567.1234567.format_s(:percent))
299
+ assert_equal("-12,345,678.1234568%", -12345678.12345678.format_s(:percent))
300
+ assert_equal("-123,456,789.123457%", -123456789.123456789.format_s(:percent))
301
+ assert_equal("-1,234,567,890.01235%", -1234567890.0123456789.format_s(:percent))
302
+ end
303
+
304
+ def test_negative_euro_decimal
305
+ assert_equal("�-1,1", -1.1.format_s(:euro))
306
+ assert_equal("�-12,12", -12.12.format_s(:euro))
307
+ assert_equal("�-123,123", -123.123.format_s(:euro))
308
+ assert_equal("�-1 234,1234", -1234.1234.format_s(:euro))
309
+ assert_equal("�-12 345,12345", -12345.12345.format_s(:euro))
310
+ assert_equal("�-123 456,123456", -123456.123456.format_s(:euro))
311
+ assert_equal("�-1 234 567,1234567", -1234567.1234567.format_s(:euro))
312
+ assert_equal("�-12 345 678,1234568", -12345678.12345678.format_s(:euro))
313
+ assert_equal("�-123 456 789,123457", -123456789.123456789.format_s(:euro))
314
+ assert_equal("�-1 234 567 890,01235", -1234567890.0123456789.format_s(:euro))
315
+ end
316
+
317
+ def test_european
318
+ assert_equal("1,1", 1.1.format_s(:eu))
319
+ assert_equal("12,12", 12.12.format_s(:eu))
320
+ assert_equal("123,123", 123.123.format_s(:eu))
321
+ assert_equal("1 234,1234", 1234.1234.format_s(:eu))
322
+ assert_equal("12 345,12345", 12345.12345.format_s(:eu))
323
+ assert_equal("123 456,123456", 123456.123456.format_s(:eu))
324
+ assert_equal("1 234 567,1234567", 1234567.1234567.format_s(:eu))
325
+ assert_equal("12 345 678,1234568", 12345678.12345678.format_s(:eu))
326
+ assert_equal("123 456 789,123457", 123456789.123456789.format_s(:eu))
327
+ assert_equal("1 234 567 890,01235", 1234567890.0123456789.format_s(:eu))
328
+ end
329
+
330
+ def test_negative_european
331
+ assert_equal("-1,1", -1.1.format_s(:eu))
332
+ assert_equal("-12,12", -12.12.format_s(:eu))
333
+ assert_equal("-123,123", -123.123.format_s(:eu))
334
+ assert_equal("-1 234,1234", -1234.1234.format_s(:eu))
335
+ assert_equal("-12 345,12345", -12345.12345.format_s(:eu))
336
+ assert_equal("-123 456,123456", -123456.123456.format_s(:eu))
337
+ assert_equal("-1 234 567,1234567", -1234567.1234567.format_s(:eu))
338
+ assert_equal("-12 345 678,1234568", -12345678.12345678.format_s(:eu))
339
+ assert_equal("-123 456 789,123457", -123456789.123456789.format_s(:eu))
340
+ assert_equal("-1 234 567 890,01235", -1234567890.0123456789.format_s(:eu))
341
+ end
342
+
343
+ def test_negative_accountant
344
+ assert_equal("(1.1)", -1.1.format_s(:us, :acct => true))
345
+ assert_equal("(12.12)", -12.12.format_s(:us, :acct => true))
346
+ assert_equal("(123.123)", -123.123.format_s(:us, :acct => true))
347
+ assert_equal("(1,234.1234)", -1234.1234.format_s(:us, :acct => true))
348
+ assert_equal("(12,345.12345)", -12345.12345.format_s(:us, :acct => true))
349
+ assert_equal("(123,456.123456)", -123456.123456.format_s(:us, :acct => true))
350
+ assert_equal("(1,234,567.1234567)", -1234567.1234567.format_s(:us, :acct => true))
351
+ assert_equal("(12,345,678.1234568)", -12345678.12345678.format_s(:us, :acct => true))
352
+ assert_equal("(123,456,789.123457)", -123456789.123456789.format_s(:us, :acct => true))
353
+ assert_equal("(1,234,567,890.01235)", -1234567890.0123456789.format_s(:us, :acct => true))
354
+ end
355
+
356
+ def test_negative_accountant_usd
357
+ assert_equal("$(1.1)", -1.1.format_s(:usd, :acct => true))
358
+ assert_equal("$(12.12)", -12.12.format_s(:usd, :acct => true))
359
+ assert_equal("$(123.123)", -123.123.format_s(:usd, :acct => true))
360
+ assert_equal("$(1,234.1234)", -1234.1234.format_s(:usd, :acct => true))
361
+ assert_equal("$(12,345.12345)", -12345.12345.format_s(:usd, :acct => true))
362
+ assert_equal("$(123,456.123456)", -123456.123456.format_s(:usd, :acct => true))
363
+ assert_equal("$(1,234,567.1234567)", -1234567.1234567.format_s(:usd, :acct => true))
364
+ assert_equal("$(12,345,678.1234568)", -12345678.12345678.format_s(:usd, :acct => true))
365
+ assert_equal("$(123,456,789.123457)", -123456789.123456789.format_s(:usd, :acct => true))
366
+ assert_equal("$(1,234,567,890.01235)", -1234567890.0123456789.format_s(:usd, :acct => true))
367
+ end
368
+
369
+ def test_negative_accountant_percent
370
+ assert_equal("(1.1)%", -1.1.format_s(:percent, :acct => true))
371
+ assert_equal("(12.12)%", -12.12.format_s(:percent, :acct => true))
372
+ assert_equal("(123.123)%", -123.123.format_s(:percent, :acct => true))
373
+ assert_equal("(1,234.1234)%", -1234.1234.format_s(:percent, :acct => true))
374
+ assert_equal("(12,345.12345)%", -12345.12345.format_s(:percent, :acct => true))
375
+ assert_equal("(123,456.123456)%", -123456.123456.format_s(:percent, :acct => true))
376
+ assert_equal("(1,234,567.1234567)%", -1234567.1234567.format_s(:percent, :acct => true))
377
+ assert_equal("(12,345,678.1234568)%", -12345678.12345678.format_s(:percent, :acct => true))
378
+ assert_equal("(123,456,789.123457)%", -123456789.123456789.format_s(:percent, :acct => true))
379
+ assert_equal("(1,234,567,890.01235)%", -1234567890.0123456789.format_s(:percent, :acct => true))
380
+ end
381
+
382
+ def test_negative_european_accountant_euro
383
+ assert_equal("�(1,1)", -1.1.format_s(:euro, :acct => true))
384
+ assert_equal("�(12,12)", -12.12.format_s(:euro, :acct => true))
385
+ assert_equal("�(123,123)", -123.123.format_s(:euro, :acct => true))
386
+ assert_equal("�(1 234,1234)", -1234.1234.format_s(:euro, :acct => true))
387
+ assert_equal("�(12 345,12345)", -12345.12345.format_s(:euro, :acct => true))
388
+ assert_equal("�(123 456,123456)", -123456.123456.format_s(:euro, :acct => true))
389
+ assert_equal("�(1 234 567,1234567)", -1234567.1234567.format_s(:euro, :acct => true))
390
+ assert_equal("�(12 345 678,1234568)", -12345678.12345678.format_s(:euro, :acct => true))
391
+ assert_equal("�(123 456 789,123457)", -123456789.123456789.format_s(:euro, :acct => true))
392
+ assert_equal("�(1 234 567 890,01235)", -1234567890.0123456789.format_s(:euro, :acct => true))
393
+ end
394
+
395
+ def test_negative_european_accountant
396
+ assert_equal("(1,1)", -1.1.format_s(:eu, :acct => true))
397
+ assert_equal("(12,12)", -12.12.format_s(:eu, :acct => true))
398
+ assert_equal("(123,123)", -123.123.format_s(:eu, :acct => true))
399
+ assert_equal("(1 234,1234)", -1234.1234.format_s(:eu, :acct => true))
400
+ assert_equal("(12 345,12345)", -12345.12345.format_s(:eu, :acct => true))
401
+ assert_equal("(123 456,123456)", -123456.123456.format_s(:eu, :acct => true))
402
+ assert_equal("(1 234 567,1234567)", -1234567.1234567.format_s(:eu, :acct => true))
403
+ assert_equal("(12 345 678,1234568)", -12345678.12345678.format_s(:eu, :acct => true))
404
+ assert_equal("(123 456 789,123457)", -123456789.123456789.format_s(:eu, :acct => true))
405
+ assert_equal("(1 234 567 890,01235)", -1234567890.0123456789.format_s(:eu, :acct => true))
406
+ end
407
+
408
+ def test_decimal_formatted
409
+ assert_equal("1.1", 1.1.format_s(:us, :fd => true))
410
+ assert_equal("12.12", 12.12.format_s(:us, :fd => true))
411
+ assert_equal("123.123", 123.123.format_s(:us, :fd => true))
412
+ assert_equal("1,234.123,4", 1234.1234.format_s(:us, :fd => true))
413
+ assert_equal("12,345.123,45", 12345.12345.format_s(:us, :fd => true))
414
+ assert_equal("123,456.123,456", 123456.123456.format_s(:us, :fd => true))
415
+ assert_equal("1,234,567.123,456,7", 1234567.1234567.format_s(:us, :fd => true))
416
+ assert_equal("12,345,678.123,456,8", 12345678.12345678.format_s(:us, :fd => true))
417
+ assert_equal("123,456,789.123,457", 123456789.123456789.format_s(:us, :fd => true))
418
+ assert_equal("1,234,567,890.012,35", 1234567890.0123456789.format_s(:us, :fd => true))
419
+ end
420
+
421
+ def test_negative_decimal_formatted
422
+ assert_equal("-1.1", -1.1.format_s(:us, :fd => true))
423
+ assert_equal("-12.12", -12.12.format_s(:us, :fd => true))
424
+ assert_equal("-123.123", -123.123.format_s(:us, :fd => true))
425
+ assert_equal("-1,234.123,4", -1234.1234.format_s(:us, :fd => true))
426
+ assert_equal("-12,345.123,45", -12345.12345.format_s(:us, :fd => true))
427
+ assert_equal("-123,456.123,456", -123456.123456.format_s(:us, :fd => true))
428
+ assert_equal("-1,234,567.123,456,7", -1234567.1234567.format_s(:us, :fd => true))
429
+ assert_equal("-12,345,678.123,456,8", -12345678.12345678.format_s(:us, :fd => true))
430
+ assert_equal("-123,456,789.123,457", -123456789.123456789.format_s(:us, :fd => true))
431
+ assert_equal("-1,234,567,890.012,35", -1234567890.0123456789.format_s(:us, :fd => true))
432
+ end
433
+
434
+ end # class TC_FormatNumber
435
+