text-reform 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,451 +1,448 @@
1
- $LOAD_PATH.unshift "../lib" if __FILE__ == $0
2
-
3
- require 'test/unit'
4
- require 'text/reform'
5
-
6
- class Test__Text_Reform < Test::Unit::TestCase
7
- TEST_PAGINATION_1 = <<-EOS
8
- Page 7
9
-
10
- Vestibulum at felis. Praesent turpis
11
- velit, elementum eget, porttitor id,
12
- bibendum auctor, metus. Ut facilisis
13
- commodo quam. Quisque nec urna ultr-
14
- icies justo condimentum tristique.
15
- Proin eget sem eu neque sollicitudin
16
- --------------------------------------------------
17
- ...8
18
-
19
-
20
- Page 8
21
-
22
- luctus. Etiam mi dolor, aliquet ege-
23
- t, pellentesque ultrices, nonummy e-
24
- t, diam. Sed odio enim, ultrices no-
25
- n, aliquam vitae, sodales sed, odio.
26
- Maecenas id justo. Quisque viverra
27
- malesuada neque. Cras auctor ipsum
28
- --------------------------------------------------
29
- ...9
30
-
31
-
32
- Page 9
33
-
34
- ac ante. Vestibulum consectetuer. N-
35
- ullam mi est, pretium at, elementum
36
- at, dictum scelerisque, purus. Inte-
37
- ger leo velit, adipiscing in, biben-
38
- dum nec, euismod sit amet, quam. Sed
39
- porttitor lorem vitae neque. Maecen-
40
- --------------------------------------------------
41
- ...10
42
-
43
-
44
- Page 10
45
-
46
- as sem lorem, aliquet non, vulputate
47
- vitae, consequat ornare, elit. Pell-
48
- entesque nec lectus quis felis posu-
49
- ere pharetra.
50
-
51
-
52
-
53
-
54
- EOS
55
- TEST_PAGINATION_2 = <<-EOS
56
- Running head
57
- Vestibulum at felis. Praesent turpis
58
- velit, elementum eget, porttitor id,
59
- bibendum auctor, metus. Ut facilisis
60
- commodo quam. Quisque nec urna ultr-
61
- icies justo condimentum tristique.
62
- Proin eget sem eu neque sollicitudin
63
- luctus. Etiam mi dolor, aliquet ege-
64
- t, pellentesque ultrices, nonummy e-
65
- page 1
66
-
67
-
68
- Running head
69
- t, diam. Sed odio enim, ultrices no-
70
- n, aliquam vitae, sodales sed, odio.
71
- Maecenas id justo. Quisque viverra
72
- malesuada neque. Cras auctor ipsum
73
- ac ante. Vestibulum consectetuer. N-
74
- ullam mi est, pretium at, elementum
75
- at, dictum scelerisque, purus. Inte-
76
- ger leo velit, adipiscing in, biben-
77
- page 2
78
-
79
-
80
- Running head
81
- dum nec, euismod sit amet, quam. Sed
82
- porttitor lorem vitae neque. Maecen-
83
- as sem lorem, aliquet non, vulputate
84
- vitae, consequat ornare, elit. Pell-
85
- entesque nec lectus quis felis posu-
86
- ere pharetra.
87
-
88
-
89
- page 3
90
- EOS
91
-
92
- attr_reader :r
93
- def setup
94
- @r = Text::Reform.new
95
- end
96
-
97
- def test_abiglongtext
98
- count = 1
99
- text = "A big long piece of text to be formatted exquisitely"
100
- output = ""
101
- output << r.format(" |||| <<<<<<<<<< ", count, text)
102
- output << r.format(" ---------------- ",
103
- " ^^^^ ]]]]]]]]]]| ", count + 11, text)
104
-
105
- reference = <<-EOS
106
- 1 A big long
107
- ----------------
108
- 12 piece of|
109
- text to be|
110
- formatted|
111
- exquisite-|
112
- ly|
113
- EOS
114
-
115
- assert_equal reference, output
116
- end
117
-
118
- def test_formular
119
- name = 'John Doe'
120
- rank = 'high'
121
- serial_number = '314159'
122
-
123
- report = ''
124
- report << r.format('Name Rank Serial Number',
125
- '==== ==== =============',
126
- '<<<<<<<<<<<<< ^^^^ <<<<<<<<<<<<<',
127
- name, rank, serial_number)
128
-
129
- result = ''
130
- result << "Name Rank Serial Number\n"
131
- result << "==== ==== =============\n"
132
- result << "John Doe high 314159 \n"
133
- assert_equal result, report
134
- end
135
-
136
- def test_trim
137
- r.trim = false
138
- assert_equal 11, r.format("[[[[[[[[[[", "short").length
139
-
140
- r.trim = true
141
- assert_equal 6, r.format("[[[[[[[[[[[", "short").length
142
- end
143
-
144
- def test_filler
145
- r.filler = '*'
146
- assert_equal "Pay bearer: *******$123.4*******\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$123.4')
147
-
148
- r.filler= '-->'
149
- assert_equal "Pay bearer: -->-->-$123.4-->-->-\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$123.4')
150
- assert_equal "Pay bearer: -->-->-$12.4-->-->--\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$12.4')
151
- assert_equal "Pay bearer: -->-->--$1.4-->-->--\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$1.4')
152
-
153
- r.filler = {
154
- :left => 'l',
155
- :right => 'r'
156
- }
157
- assert_equal "Pay bearer: lllllll$123.4rrrrrrr\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$123.4')
158
- end
159
-
160
- def test_data_format
161
- # String form
162
- s1 = r.format "]]]].[[", "1234\n123"
163
-
164
- # Array form
165
- s2 = r.format "]]]].[[", [ 1234, 123 ]
166
-
167
- # Array with another type
168
- s3 = r.format "]]]].[[", [ 1234.0, 123.0 ]
169
-
170
- assert_equal "1234.0 \n 123.0 \n", s1
171
- assert_equal s1, s2
172
- assert_equal s2, s3
173
- end
174
-
175
- def test_multiline_numbers
176
- assert_equal "1 \n2 \n3 \n", r.format("[[[[[[", (1..3).to_a.map { |el| el.to_s })
177
- end
178
-
179
- def test_wrong_number_of_data_args
180
- assert_nothing_raised do
181
- r.format("]]]]]]]] ]]]]]]]]", 'a single data item')
182
- end
183
- end
184
-
185
- def test_whitespace
186
- assert_equal("EG> h e\t l lo \nEG> world \n",
187
- r.format("EG> [[[[[[[[[[[[[[[[[[[[[",
188
- "h e\t l lo\nworld\t\t\t\t\t"))
189
-
190
- r.squeeze = true
191
- assert_equal("EG> h e l lo \nEG> world \n",
192
- r.format("EG> [[[[[[[[[[[[[[[[[[[[[",
193
- "h e\t l lo\nworld\t\t\t\t\t"))
194
-
195
- r.fill = true
196
- assert_equal("EG> h e l lo world \n",
197
- r.format("EG> [[[[[[[[[[[[[[[[[[[[[",
198
- "h \n e\t l\n lo\nworld\t\n\t\t\t\t"))
199
- r.squeeze = false
200
- r.fill = false
201
- end
202
-
203
- def test_format_numbers
204
- numbers = [ 1, 1.2, 1.23, 1.234, 1.2345, "1.2345" ]
205
-
206
- assert_equal(" 1.0 \n 1.2 \n 1.23 \n 1.234 \n 1.234 \n 1.234 \n",
207
- r.format(" ]]].[[[ ", numbers))
208
- assert_equal(" 1.0 \n", r.format(" >>>.<<< ", numbers))
209
- end
210
-
211
- def test_basic_formatting_tests
212
- assert_equal " test \n", r.format(" <<<<<<<<<<<<<<<<<< ", 'test')
213
- assert_equal " test \n", r.format(" >>>>>>>>>>>>>>>>>> ", 'test')
214
- assert_equal " this is a test \n", r.format(" <<<<<<<<<>>>>>>>>> ", 'this is a test')
215
- assert_equal " test \n", r.format(" ^^^^^^^^^^^^^^^^^^ ", 'test')
216
- end
217
-
218
- def test_multi_formatting_tests
219
- text = "A big long piece of text to be formatted exquisitely"
220
- assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
221
- r.format(" [[[[[[[[[[[[[[[[[[ ", text.dup)
222
- assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
223
- r.format(" ]]]]]]]]]]]]]]]]]] ", text.dup)
224
- assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
225
- r.format(" [[[[[[[[[[[]]]]]]] ", text.dup)
226
- assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
227
- r.format(" |||||||||||||||||| ", text.dup)
228
- end
229
-
230
- def test_array_argument
231
- assert_equal " a lotta words come \n", r.format(" <<<<<<<<<<<<<<<<<< ", %w{ a lotta words come here in an array }.join(' '))
232
- end
233
-
234
- def test_break_at
235
- text = "supercalifragilousexpalidoucious"
236
-
237
- @r.break = Text::Reform.break_at('-')
238
-
239
- assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" [[[[[[[[[[[[[[[[[ ", text.dup))
240
- assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" ]]]]]]]]]]]]]]]]] ", text.dup))
241
- assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" [[[[[[[[[[[]]]]]] ", text.dup))
242
- assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" ||||||||||||||||| ", text.dup))
243
- end
244
-
245
- def test_break_tex
246
- begin
247
- require 'tex/hyphen'
248
- hy = TeX::Hyphen.new
249
- rescue LoadError
250
- begin
251
- require 'rubygems'
252
- require 'tex/hyphen'
253
- hy = TeX::Hyphen.new
254
- rescue LoadError
255
- print 'S'
256
- return true
257
- end
258
- end
259
-
260
- text = "supercalifragilousexpalidoucious"
261
-
262
- @r.break = Text::Reform.break_hyphenator(hy)
263
-
264
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[[[[[[[ ", text.dup))
265
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ]]]]]]]]]]]]]]]]] ", text.dup))
266
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[]]]]]] ", text.dup))
267
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ||||||||||||||||| ", text.dup))
268
- end
269
-
270
- def test_break_texthyphen
271
- begin
272
- require 'text/hyphen'
273
- hy = Text::Hyphen.new
274
- rescue LoadError
275
- begin
276
- require 'rubygems'
277
- require 'text/hyphen'
278
- hy = Text::Hyphen.new
279
- rescue LoadError
280
- print 'S'
281
- return true
282
- end
283
- end
284
- text = "supercalifragilousexpalidoucious"
285
-
286
- hy = Text::Hyphen.new
287
- @r.break = Text::Reform.break_hyphenator(hy)
288
-
289
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[[[[[[[ ", text.dup))
290
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ]]]]]]]]]]]]]]]]] ", text.dup))
291
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[]]]]]] ", text.dup))
292
- assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ||||||||||||||||| ", text.dup))
293
- end
294
-
295
- def test_interleave
296
- passes = %w{ pass_1 pass_2 pass_3 }
297
- fails = %w{ fail_1 fail_2 fail_3 }
298
-
299
- r.interleave = true
300
-
301
- fmt = "Passed:\n"
302
- fmt << " [[[[[[[[[[[[[[[\n"
303
- fmt << "Failed:\n"
304
- fmt << " [[[[[[[[[[[[[[[\n"
305
-
306
- str = r.format(fmt, passes, fails)
307
-
308
- assert_equal "Passed:\n pass_1 \n pass_2 \n pass_3 \nFailed:\n fail_1 \n fail_2 \n fail_3 \n", str
309
- end
310
-
311
- def test_hyphenate_small
312
- data = 'split'
313
- s = r.format('~', data)
314
-
315
- assert_equal "s\np\nl\ni\nt\n", s
316
- assert_equal 0, data.length
317
-
318
- data = 'split'
319
- r.min_break = 1
320
- s = r.format('~', data)
321
-
322
- assert_equal "s-\np-\nl-\ni-\nt\n", s
323
- assert_equal 0, data.length
324
- end
325
-
326
- class WrapAndSlop
327
- def break str, initial, total
328
- if initial==total
329
- str =~ /\A(\s*\S*)(.*)/
330
- return $1, $2
331
- else
332
- return '', str
333
- end
334
- end
335
- end
336
-
337
- def test_wrap_and_slop
338
- r.break = Text::Reform.break_wrap
339
- data = 'a looooooong word that should be wrapped and slopped'
340
- assert_equal("a \nlooooooong\nword that\nshould be\nwrapped \nand \nslopped \n",
341
- r.format('[[[[[[[[[', data))
342
-
343
- r.break = WrapAndSlop.new
344
-
345
- data = 'a looooooong word that should be wrapped and slopped'
346
- assert_equal("a \nlooooooong\nword that\nshould be\nwrapped \nand \nslopped \n",
347
- r.format('[[[[[[[[[', data))
348
- end
349
-
350
- def test_break_at_doku
351
- r.break= Text::Reform.break_at('-')
352
- s = r.format("[[[[[[[[[[[[[[", "The Newton-Raphson methodology")
353
-
354
- assert_equal("The Newton- \nRaphson \nmethodology \n", s)
355
-
356
- r.break= Text::Reform.break_with('-')
357
- s = r.format("[[[[[[[[[[[[[[", "The Newton-Raphson methodology")
358
-
359
- assert_equal("The Newton-Ra-\nphson methodo-\nlogy \n", s)
360
- end
361
-
362
- def test_number_formatting
363
- s= r.format('(]]]]].[[)', %w{1 1.0 1.001 1.009 123.456 1234567 one two})
364
-
365
- assert_equal("( 1.0 )\n( 1.0 )\n( 1.00)\n( 1.01)\n( 123.46)\n(#####.##)\n(?????.??)\n(?????.??)\n", s)
366
- end
367
-
368
- def test_num_all_places
369
- r.numeric = Text::Reform::NUMBERS_ALL_PLACES
370
- s = r.format('(]]]]].[[)', %w{1 1.0})
371
- assert_equal("( 1.00)\n( 1.00)\n", s)
372
- end
373
-
374
- def test_skip_nan
375
- r.numeric = Text::Reform::NUMBERS_SKIP_NAN
376
- s= r.format('(]]]]].[[)', %w{1 two three 4})
377
- assert_equal("( 1.0 )\n( 4.0 )\n", s)
378
- end
379
-
380
- def test_array
381
- nvalues = [1, 10, 100, 1000]
382
- svalues = %w{ 1 10 100 1000 }
383
-
384
- assert_equal("( 1.0 )\n( 10.0 )\n( 100.0 )\n(1000.0 )\n", r.format("(]]]].[[)", nvalues))
385
- assert_equal("( 1.0 )\n( 10.0 )\n( 100.0 )\n(1000.0 )\n", r.format("(]]]].[[)", svalues))
386
- end
387
-
388
- def test_pagination
389
- small = Text::Reform.new
390
- template = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['
391
- data = <<DOLOR
392
- Vestibulum at felis. Praesent turpis velit, elementum eget, porttitor id, bibendum auctor, metus. Ut facilisis commodo quam. Quisque nec urna ultricies justo condimentum tristique. Proin eget sem eu neque sollicitudin luctus. Etiam mi dolor, aliquet eget, pellentesque ultrices, nonummy et, diam. Sed odio enim, ultrices non, aliquam vitae, sodales sed, odio. Maecenas id justo. Quisque viverra malesuada neque. Cras auctor ipsum ac ante. Vestibulum consectetuer. Nullam mi est, pretium at, elementum at, dictum scelerisque, purus. Integer leo velit, adipiscing in, bibendum nec, euismod sit amet, quam. Sed porttitor lorem vitae neque. Maecenas sem lorem, aliquet non, vulputate vitae, consequat ornare, elit. Pellentesque nec lectus quis felis posuere pharetra.
393
- DOLOR
394
- r.header = lambda do |page| "Page #{page}\n\n" end
395
- r.footer = lambda do |page, last|
396
- if last
397
- ''
398
- else
399
- ('-'*50 + "\n" + small.format('>'*50, "...#{page+1}"))
400
- end
401
- end
402
- r.page_feed = "\n\n"
403
- r.page_len = 10
404
- r.page_num = 7
405
-
406
- s = r.format(template, data.dup)
407
- assert_equal(TEST_PAGINATION_1, s)
408
-
409
- r.page_num = 1
410
- r.header = { :right => 'Running head' }
411
- r.footer = { :centre => lambda { |page| "page #{page}" } }
412
- r.page_len = 10
413
-
414
- assert_equal(TEST_PAGINATION_2, r.format(template, data.dup))
415
-
416
- r.page_num = 1
417
- r.header = { :right => 'Running head' }
418
- r.footer = lambda do |page| { :center => "page #{page}" } end
419
-
420
- assert_equal(TEST_PAGINATION_2, r.format(template, data.dup))
421
- end
422
-
423
- def test_page_width
424
-
425
- r.page_width = 30
426
- r.header = { :right => 'test' }
427
- text = 'text'
428
-
429
- assert_equal " test\ntext \n",
430
- r.format( '[[[[[[[[[[[[[[[[[[[[[[[[[[[', text )
431
- end
432
-
433
- def test_cols
434
- name = %w{Tom Dick Harry}
435
- score = %w{ 88 54 99}
436
- time = %w{ 15 13 18}
437
-
438
- result = "-------------------------------\n"
439
- result << "Name Score Time\n"
440
- result << "-------------------------------\n"
441
- result << "Tom 88 15 \n"
442
- result << "Dick 54 13 \n"
443
- result << "Harry 99 18 \n"
444
-
445
- assert_equal(result, r.format('-------------------------------',
446
- 'Name Score Time',
447
- '-------------------------------',
448
- '[[[[[[[[[[[[[[ ||||| ||||',
449
- name, score, time))
450
- end
451
- end
1
+ require "test/unit"
2
+ require "text/reform"
3
+
4
+ class TestTextReform < Test::Unit::TestCase
5
+ TEST_PAGINATION_1 = <<-EOS
6
+ Page 7
7
+
8
+ Vestibulum at felis. Praesent turpis
9
+ velit, elementum eget, porttitor id,
10
+ bibendum auctor, metus. Ut facilisis
11
+ commodo quam. Quisque nec urna ultr-
12
+ icies justo condimentum tristique.
13
+ Proin eget sem eu neque sollicitudin
14
+ --------------------------------------------------
15
+ ...8
16
+
17
+
18
+ Page 8
19
+
20
+ luctus. Etiam mi dolor, aliquet ege-
21
+ t, pellentesque ultrices, nonummy e-
22
+ t, diam. Sed odio enim, ultrices no-
23
+ n, aliquam vitae, sodales sed, odio.
24
+ Maecenas id justo. Quisque viverra
25
+ malesuada neque. Cras auctor ipsum
26
+ --------------------------------------------------
27
+ ...9
28
+
29
+
30
+ Page 9
31
+
32
+ ac ante. Vestibulum consectetuer. N-
33
+ ullam mi est, pretium at, elementum
34
+ at, dictum scelerisque, purus. Inte-
35
+ ger leo velit, adipiscing in, biben-
36
+ dum nec, euismod sit amet, quam. Sed
37
+ porttitor lorem vitae neque. Maecen-
38
+ --------------------------------------------------
39
+ ...10
40
+
41
+
42
+ Page 10
43
+
44
+ as sem lorem, aliquet non, vulputate
45
+ vitae, consequat ornare, elit. Pell-
46
+ entesque nec lectus quis felis posu-
47
+ ere pharetra.
48
+
49
+
50
+
51
+
52
+ EOS
53
+ TEST_PAGINATION_2 = <<-EOS
54
+ Running head
55
+ Vestibulum at felis. Praesent turpis
56
+ velit, elementum eget, porttitor id,
57
+ bibendum auctor, metus. Ut facilisis
58
+ commodo quam. Quisque nec urna ultr-
59
+ icies justo condimentum tristique.
60
+ Proin eget sem eu neque sollicitudin
61
+ luctus. Etiam mi dolor, aliquet ege-
62
+ t, pellentesque ultrices, nonummy e-
63
+ page 1
64
+
65
+
66
+ Running head
67
+ t, diam. Sed odio enim, ultrices no-
68
+ n, aliquam vitae, sodales sed, odio.
69
+ Maecenas id justo. Quisque viverra
70
+ malesuada neque. Cras auctor ipsum
71
+ ac ante. Vestibulum consectetuer. N-
72
+ ullam mi est, pretium at, elementum
73
+ at, dictum scelerisque, purus. Inte-
74
+ ger leo velit, adipiscing in, biben-
75
+ page 2
76
+
77
+
78
+ Running head
79
+ dum nec, euismod sit amet, quam. Sed
80
+ porttitor lorem vitae neque. Maecen-
81
+ as sem lorem, aliquet non, vulputate
82
+ vitae, consequat ornare, elit. Pell-
83
+ entesque nec lectus quis felis posu-
84
+ ere pharetra.
85
+
86
+
87
+ page 3
88
+ EOS
89
+
90
+ attr_reader :r
91
+ def setup
92
+ @r = Text::Reform.new
93
+ end
94
+
95
+ def test_abiglongtext
96
+ count = 1
97
+ text = "A big long piece of text to be formatted exquisitely"
98
+ output = ""
99
+ output << r.format(" |||| <<<<<<<<<< ", count, text)
100
+ output << r.format(" ---------------- ",
101
+ " ^^^^ ]]]]]]]]]]| ", count + 11, text)
102
+
103
+ reference = <<-EOS
104
+ 1 A big long
105
+ ----------------
106
+ 12 piece of|
107
+ text to be|
108
+ formatted|
109
+ exquisite-|
110
+ ly|
111
+ EOS
112
+
113
+ assert_equal reference, output
114
+ end
115
+
116
+ def test_formular
117
+ name = 'John Doe'
118
+ rank = 'high'
119
+ serial_number = '314159'
120
+
121
+ report = ''
122
+ report << r.format('Name Rank Serial Number',
123
+ '==== ==== =============',
124
+ '<<<<<<<<<<<<< ^^^^ <<<<<<<<<<<<<',
125
+ name, rank, serial_number)
126
+
127
+ result = ''
128
+ result << "Name Rank Serial Number\n"
129
+ result << "==== ==== =============\n"
130
+ result << "John Doe high 314159 \n"
131
+ assert_equal result, report
132
+ end
133
+
134
+ def test_trim
135
+ r.trim = false
136
+ assert_equal 11, r.format("[[[[[[[[[[", "short").length
137
+
138
+ r.trim = true
139
+ assert_equal 6, r.format("[[[[[[[[[[[", "short").length
140
+ end
141
+
142
+ def test_filler
143
+ r.filler = '*'
144
+ assert_equal "Pay bearer: *******$123.4*******\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$123.4')
145
+
146
+ r.filler= '-->'
147
+ assert_equal "Pay bearer: -->-->-$123.4-->-->-\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$123.4')
148
+ assert_equal "Pay bearer: -->-->-$12.4-->-->--\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$12.4')
149
+ assert_equal "Pay bearer: -->-->--$1.4-->-->--\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$1.4')
150
+
151
+ r.filler = {
152
+ :left => 'l',
153
+ :right => 'r'
154
+ }
155
+ assert_equal "Pay bearer: lllllll$123.4rrrrrrr\n", r.format("Pay bearer: ^^^^^^^^^^^^^^^^^^^^", '$123.4')
156
+ end
157
+
158
+ def test_data_format
159
+ # String form
160
+ s1 = r.format "]]]].[[", "1234\n123"
161
+
162
+ # Array form
163
+ s2 = r.format "]]]].[[", [ 1234, 123 ]
164
+
165
+ # Array with another type
166
+ s3 = r.format "]]]].[[", [ 1234.0, 123.0 ]
167
+
168
+ assert_equal "1234.0 \n 123.0 \n", s1
169
+ assert_equal s1, s2
170
+ assert_equal s2, s3
171
+ end
172
+
173
+ def test_multiline_numbers
174
+ assert_equal "1 \n2 \n3 \n", r.format("[[[[[[", (1..3).to_a.map { |el| el.to_s })
175
+ end
176
+
177
+ def test_wrong_number_of_data_args
178
+ assert_nothing_raised do
179
+ r.format("]]]]]]]] ]]]]]]]]", 'a single data item')
180
+ end
181
+ end
182
+
183
+ def test_whitespace
184
+ assert_equal("EG> h e\t l lo \nEG> world \n",
185
+ r.format("EG> [[[[[[[[[[[[[[[[[[[[[",
186
+ "h e\t l lo\nworld\t\t\t\t\t"))
187
+
188
+ r.squeeze = true
189
+ assert_equal("EG> h e l lo \nEG> world \n",
190
+ r.format("EG> [[[[[[[[[[[[[[[[[[[[[",
191
+ "h e\t l lo\nworld\t\t\t\t\t"))
192
+
193
+ r.fill = true
194
+ assert_equal("EG> h e l lo world \n",
195
+ r.format("EG> [[[[[[[[[[[[[[[[[[[[[",
196
+ "h \n e\t l\n lo\nworld\t\n\t\t\t\t"))
197
+ r.squeeze = false
198
+ r.fill = false
199
+ end
200
+
201
+ def test_format_numbers
202
+ numbers = [ 1, 1.2, 1.23, 1.234, 1.2345, "1.2345" ]
203
+
204
+ assert_equal(" 1.0 \n 1.2 \n 1.23 \n 1.234 \n 1.234 \n 1.234 \n",
205
+ r.format(" ]]].[[[ ", numbers))
206
+ assert_equal(" 1.0 \n", r.format(" >>>.<<< ", numbers))
207
+ end
208
+
209
+ def test_basic_formatting_tests
210
+ assert_equal " test \n", r.format(" <<<<<<<<<<<<<<<<<< ", 'test')
211
+ assert_equal " test \n", r.format(" >>>>>>>>>>>>>>>>>> ", 'test')
212
+ assert_equal " this is a test \n", r.format(" <<<<<<<<<>>>>>>>>> ", 'this is a test')
213
+ assert_equal " test \n", r.format(" ^^^^^^^^^^^^^^^^^^ ", 'test')
214
+ end
215
+
216
+ def test_multi_formatting_tests
217
+ text = "A big long piece of text to be formatted exquisitely"
218
+ assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
219
+ r.format(" [[[[[[[[[[[[[[[[[[ ", text.dup)
220
+ assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
221
+ r.format(" ]]]]]]]]]]]]]]]]]] ", text.dup)
222
+ assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
223
+ r.format(" [[[[[[[[[[[]]]]]]] ", text.dup)
224
+ assert_equal " A big long piece \n of text to be for- \n matted exquisitely \n",
225
+ r.format(" |||||||||||||||||| ", text.dup)
226
+ end
227
+
228
+ def test_array_argument
229
+ assert_equal " a lotta words come \n", r.format(" <<<<<<<<<<<<<<<<<< ", %w{ a lotta words come here in an array }.join(' '))
230
+ end
231
+
232
+ def test_break_at
233
+ text = "supercalifragilousexpalidoucious"
234
+
235
+ @r.break = Text::Reform.break_at('-')
236
+
237
+ assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" [[[[[[[[[[[[[[[[[ ", text.dup))
238
+ assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" ]]]]]]]]]]]]]]]]] ", text.dup))
239
+ assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" [[[[[[[[[[[]]]]]] ", text.dup))
240
+ assert_equal(" supercalifragilo- \n usexpalidoucious \n", r.format(" ||||||||||||||||| ", text.dup))
241
+ end
242
+
243
+ def test_break_tex
244
+ begin
245
+ require 'tex/hyphen'
246
+ hy = TeX::Hyphen.new
247
+ rescue LoadError
248
+ begin
249
+ require 'rubygems'
250
+ require 'tex/hyphen'
251
+ hy = TeX::Hyphen.new
252
+ rescue LoadError
253
+ print 'S'
254
+ return true
255
+ end
256
+ end
257
+
258
+ text = "supercalifragilousexpalidoucious"
259
+
260
+ @r.break = Text::Reform.break_hyphenator(hy)
261
+
262
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[[[[[[[ ", text.dup))
263
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ]]]]]]]]]]]]]]]]] ", text.dup))
264
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[]]]]]] ", text.dup))
265
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ||||||||||||||||| ", text.dup))
266
+ end
267
+
268
+ def test_break_texthyphen
269
+ begin
270
+ require 'text/hyphen'
271
+ hy = Text::Hyphen.new
272
+ rescue LoadError
273
+ begin
274
+ require 'rubygems'
275
+ require 'text/hyphen'
276
+ hy = Text::Hyphen.new
277
+ rescue LoadError
278
+ print 'S'
279
+ return true
280
+ end
281
+ end
282
+ text = "supercalifragilousexpalidoucious"
283
+
284
+ hy = Text::Hyphen.new
285
+ @r.break = Text::Reform.break_hyphenator(hy)
286
+
287
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[[[[[[[ ", text.dup))
288
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ]]]]]]]]]]]]]]]]] ", text.dup))
289
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" [[[[[[[[[[[]]]]]] ", text.dup))
290
+ assert_equal(" supercalifrag- \n ilousexpali- \n doucious \n", r.format(" ||||||||||||||||| ", text.dup))
291
+ end
292
+
293
+ def test_interleave
294
+ passes = %w{ pass_1 pass_2 pass_3 }
295
+ fails = %w{ fail_1 fail_2 fail_3 }
296
+
297
+ r.interleave = true
298
+
299
+ fmt = "Passed:\n"
300
+ fmt << " [[[[[[[[[[[[[[[\n"
301
+ fmt << "Failed:\n"
302
+ fmt << " [[[[[[[[[[[[[[[\n"
303
+
304
+ str = r.format(fmt, passes, fails)
305
+
306
+ assert_equal "Passed:\n pass_1 \n pass_2 \n pass_3 \nFailed:\n fail_1 \n fail_2 \n fail_3 \n", str
307
+ end
308
+
309
+ def test_hyphenate_small
310
+ data = 'split'
311
+ s = r.format('~', data)
312
+
313
+ assert_equal "s\np\nl\ni\nt\n", s
314
+ assert_equal 0, data.length
315
+
316
+ data = 'split'
317
+ r.min_break = 1
318
+ s = r.format('~', data)
319
+
320
+ assert_equal "s-\np-\nl-\ni-\nt\n", s
321
+ assert_equal 0, data.length
322
+ end
323
+
324
+ class WrapAndSlop
325
+ def break str, initial, total
326
+ if initial==total
327
+ str =~ /\A(\s*\S*)(.*)/
328
+ return $1, $2
329
+ else
330
+ return '', str
331
+ end
332
+ end
333
+ end
334
+
335
+ def test_wrap_and_slop
336
+ r.break = Text::Reform.break_wrap
337
+ data = 'a looooooong word that should be wrapped and slopped'
338
+ assert_equal("a \nlooooooong\nword that\nshould be\nwrapped \nand \nslopped \n",
339
+ r.format('[[[[[[[[[', data))
340
+
341
+ r.break = WrapAndSlop.new
342
+
343
+ data = 'a looooooong word that should be wrapped and slopped'
344
+ assert_equal("a \nlooooooong\nword that\nshould be\nwrapped \nand \nslopped \n",
345
+ r.format('[[[[[[[[[', data))
346
+ end
347
+
348
+ def test_break_at_doku
349
+ r.break= Text::Reform.break_at('-')
350
+ s = r.format("[[[[[[[[[[[[[[", "The Newton-Raphson methodology")
351
+
352
+ assert_equal("The Newton- \nRaphson \nmethodology \n", s)
353
+
354
+ r.break= Text::Reform.break_with('-')
355
+ s = r.format("[[[[[[[[[[[[[[", "The Newton-Raphson methodology")
356
+
357
+ assert_equal("The Newton-Ra-\nphson methodo-\nlogy \n", s)
358
+ end
359
+
360
+ def test_number_formatting
361
+ s= r.format('(]]]]].[[)', %w{1 1.0 1.001 1.009 123.456 1234567 one two})
362
+
363
+ assert_equal("( 1.0 )\n( 1.0 )\n( 1.00)\n( 1.01)\n( 123.46)\n(#####.##)\n(?????.??)\n(?????.??)\n", s)
364
+ end
365
+
366
+ def test_num_all_places
367
+ r.numeric = Text::Reform::NUMBERS_ALL_PLACES
368
+ s = r.format('(]]]]].[[)', %w{1 1.0})
369
+ assert_equal("( 1.00)\n( 1.00)\n", s)
370
+ end
371
+
372
+ def test_skip_nan
373
+ r.numeric = Text::Reform::NUMBERS_SKIP_NAN
374
+ s= r.format('(]]]]].[[)', %w{1 two three 4})
375
+ assert_equal("( 1.0 )\n( 4.0 )\n", s)
376
+ end
377
+
378
+ def test_array
379
+ nvalues = [1, 10, 100, 1000]
380
+ svalues = %w{ 1 10 100 1000 }
381
+
382
+ assert_equal("( 1.0 )\n( 10.0 )\n( 100.0 )\n(1000.0 )\n", r.format("(]]]].[[)", nvalues))
383
+ assert_equal("( 1.0 )\n( 10.0 )\n( 100.0 )\n(1000.0 )\n", r.format("(]]]].[[)", svalues))
384
+ end
385
+
386
+ def test_pagination
387
+ small = Text::Reform.new
388
+ template = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['
389
+ data = <<DOLOR
390
+ Vestibulum at felis. Praesent turpis velit, elementum eget, porttitor id, bibendum auctor, metus. Ut facilisis commodo quam. Quisque nec urna ultricies justo condimentum tristique. Proin eget sem eu neque sollicitudin luctus. Etiam mi dolor, aliquet eget, pellentesque ultrices, nonummy et, diam. Sed odio enim, ultrices non, aliquam vitae, sodales sed, odio. Maecenas id justo. Quisque viverra malesuada neque. Cras auctor ipsum ac ante. Vestibulum consectetuer. Nullam mi est, pretium at, elementum at, dictum scelerisque, purus. Integer leo velit, adipiscing in, bibendum nec, euismod sit amet, quam. Sed porttitor lorem vitae neque. Maecenas sem lorem, aliquet non, vulputate vitae, consequat ornare, elit. Pellentesque nec lectus quis felis posuere pharetra.
391
+ DOLOR
392
+ r.header = lambda do |page| "Page #{page}\n\n" end
393
+ r.footer = lambda do |page, last|
394
+ if last
395
+ ''
396
+ else
397
+ ('-'*50 + "\n" + small.format('>'*50, "...#{page+1}"))
398
+ end
399
+ end
400
+ r.page_feed = "\n\n"
401
+ r.page_len = 10
402
+ r.page_num = 7
403
+
404
+ s = r.format(template, data.dup)
405
+ assert_equal(TEST_PAGINATION_1, s)
406
+
407
+ r.page_num = 1
408
+ r.header = { :right => 'Running head' }
409
+ r.footer = { :centre => lambda { |page| "page #{page}" } }
410
+ r.page_len = 10
411
+
412
+ assert_equal(TEST_PAGINATION_2, r.format(template, data.dup))
413
+
414
+ r.page_num = 1
415
+ r.header = { :right => 'Running head' }
416
+ r.footer = lambda do |page| { :center => "page #{page}" } end
417
+
418
+ assert_equal(TEST_PAGINATION_2, r.format(template, data.dup))
419
+ end
420
+
421
+ def test_page_width
422
+ r.page_width = 30
423
+ r.header = { :right => 'test' }
424
+ text = 'text'
425
+
426
+ assert_equal " test\ntext \n",
427
+ r.format( '[[[[[[[[[[[[[[[[[[[[[[[[[[[', text )
428
+ end
429
+
430
+ def test_cols
431
+ name = %w{Tom Dick Harry}
432
+ score = %w{ 88 54 99}
433
+ time = %w{ 15 13 18}
434
+
435
+ result = "-------------------------------\n"
436
+ result << "Name Score Time\n"
437
+ result << "-------------------------------\n"
438
+ result << "Tom 88 15 \n"
439
+ result << "Dick 54 13 \n"
440
+ result << "Harry 99 18 \n"
441
+
442
+ assert_equal(result, r.format('-------------------------------',
443
+ 'Name Score Time',
444
+ '-------------------------------',
445
+ '[[[[[[[[[[[[[[ ||||| ||||',
446
+ name, score, time))
447
+ end
448
+ end