text-format-revised 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Ruwiki version 0.8.0
4
+ # Copyright � 2002 - 2003, Digikata and HaloStatue
5
+ # Alan Chen (alan@digikata.com)
6
+ # Austin Ziegler (ruwiki@halostatue.ca)
7
+ #
8
+ # Licensed under the same terms as Ruby.
9
+ #
10
+ # $Id: testall.rb,v 1.1 2005/01/17 16:15:04 austin Exp $
11
+ #++
12
+
13
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
14
+
15
+ puts "Checking for test cases:"
16
+ Dir['tc*.rb'].each do |testcase|
17
+ puts "\t#{testcase}"
18
+ require testcase
19
+ end
20
+ puts " "
@@ -0,0 +1,475 @@
1
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
2
+
3
+ require 'text/format'
4
+ require 'test/unit'
5
+ require 'turn'
6
+
7
+ class TestText__Format < Test::Unit::TestCase
8
+ attr_accessor :format_o
9
+
10
+ GETTYSBURG = <<-'EOS'
11
+ Four score and seven years ago our fathers brought forth on this
12
+ continent a new nation, conceived in liberty and dedicated to the
13
+ proposition that all men are created equal. Now we are engaged in
14
+ a great civil war, testing whether that nation or any nation so
15
+ conceived and so dedicated can long endure. We are met on a great
16
+ battlefield of that war. We have come to dedicate a portion of
17
+ that field as a final resting-place for those who here gave their
18
+ lives that that nation might live. It is altogether fitting and
19
+ proper that we should do this. But in a larger sense, we cannot
20
+ dedicate, we cannot consecrate, we cannot hallow this ground.
21
+ The brave men, living and dead who struggled here have consecrated
22
+ it far above our poor power to add or detract. The world will
23
+ little note nor long remember what we say here, but it can never
24
+ forget what they did here. It is for us the living rather to be
25
+ dedicated here to the unfinished work which they who fought here
26
+ have thus far so nobly advanced. It is rather for us to be here
27
+ dedicated to the great task remaining before us--that from these
28
+ honored dead we take increased devotion to that cause for which
29
+ they gave the last full measure of devotion--that we here highly
30
+ resolve that these dead shall not have died in vain, that this
31
+ nation under God shall have a new birth of freedom, and that
32
+ government of the people, by the people, for the people shall
33
+ not perish from the earth.
34
+
35
+ -- Pres. Abraham Lincoln, 19 November 1863
36
+ EOS
37
+
38
+ FIVE_COL = "Four \nscore\nand s\neven \nyears\nago o\nur fa\nthers\nbroug\nht fo\nrth o\nn thi\ns con\ntinen\nt a n\new na\ntion,\nconce\nived \nin li\nberty\nand d\nedica\nted t\no the\npropo\nsitio\nn tha\nt all\nmen a\nre cr\neated\nequal\n. Now\nwe ar\ne eng\naged \nin a \ngreat\ncivil\nwar, \ntesti\nng wh\nether\nthat \nnatio\nn or \nany n\nation\nso co\nnceiv\ned an\nd so \ndedic\nated \ncan l\nong e\nndure\n. We \nare m\net on\na gre\nat ba\nttlef\nield \nof th\nat wa\nr. We\nhave \ncome \nto de\ndicat\ne a p\nortio\nn of \nthat \nfield\nas a \nfinal\nresti\nng-pl\nace f\nor th\nose w\nho he\nre ga\nve th\neir l\nives \nthat \nthat \nnatio\nn mig\nht li\nve. I\nt is \naltog\nether\nfitti\nng an\nd pro\nper t\nhat w\ne sho\nuld d\no thi\ns. Bu\nt in \na lar\nger s\nense,\nwe ca\nnnot \ndedic\nate, \nwe ca\nnnot \nconse\ncrate\n, we \ncanno\nt hal\nlow t\nhis g\nround\n. The\nbrave\nmen, \nlivin\ng and\ndead \nwho s\ntrugg\nled h\nere h\nave c\nonsec\nrated\nit fa\nr abo\nve ou\nr poo\nr pow\ner to\nadd o\nr det\nract.\nThe w\norld \nwill \nlittl\ne not\ne nor\nlong \nremem\nber w\nhat w\ne say\nhere,\nbut i\nt can\nnever\nforge\nt wha\nt the\ny did\nhere.\nIt is\nfor u\ns the\nlivin\ng rat\nher t\no be \ndedic\nated \nhere \nto th\ne unf\ninish\ned wo\nrk wh\nich t\nhey w\nho fo\nught \nhere \nhave \nthus \nfar s\no nob\nly ad\nvance\nd. It\nis ra\nther \nfor u\ns to \nbe he\nre de\ndicat\ned to\nthe g\nreat \ntask \nremai\nning \nbefor\ne us-\n-that\nfrom \nthese\nhonor\ned de\nad we\ntake \nincre\nased \ndevot\nion t\no tha\nt cau\nse fo\nr whi\nch th\ney ga\nve th\ne las\nt ful\nl mea\nsure \nof de\nvotio\nn--th\nat we\nhere \nhighl\ny res\nolve \nthat \nthese\ndead \nshall\nnot h\nave d\nied i\nn vai\nn, th\nat th\nis na\ntion \nunder\nGod s\nhall \nhave \na new\nbirth\nof fr\needom\n, and\nthat \ngover\nnment\nof th\ne peo\nple, \nby th\ne peo\nple, \nfor t\nhe pe\nople \nshall\nnot p\nerish\nfrom \nthe e\narth.\n-- Pr\nes. A\nbraha\nm Lin\ncoln,\n19 No\nvembe\nr 186\n3 \n"
39
+
40
+ FIVE_CNF = "Four \nscore\nand s\neven \nyears\nago o\nur f\\\nathe\\\nrs b\\\nroug\\\nht f\\\north \non t\\\nhis c\nonti\\\nnent \na new\nnati\\\non, c\nonce\\\nived \nin l\\\niber\\\nty a\\\nnd d\\\nedic\\\nated \nto t\\\nhe p\\\nropo\\\nsiti\\\non t\\\nhat a\nll m\\\nen a\\\nre c\\\nreat\\\ned e\\\nqual.\nNow w\ne are\nenga\\\nged i\nn a g\nreat \ncivil\nwar, \ntest\\\ning w\nheth\\\ner t\\\nhat n\nation\nor a\\\nny n\\\nation\nso c\\\nonce\\\nived \nand s\no de\\\ndica\\\nted c\nan l\\\nong e\nndur\\\ne. We\nare m\net on\na gr\\\neat b\nattl\\\nefie\\\nld of\nthat \nwar. \nWe h\\\nave c\nome t\no de\\\ndica\\\nte a \nport\\\nion o\nf th\\\nat f\\\nield \nas a \nfinal\nrest\\\ning-\\\nplace\nfor t\nhose \nwho h\nere g\nave t\nheir \nlives\nthat \nthat \nnati\\\non m\\\night \nlive.\nIt is\nalto\\\ngeth\\\ner f\\\nitti\\\nng a\\\nnd p\\\nroper\nthat \nwe s\\\nhould\ndo t\\\nhis. \nBut i\nn a l\narger\nsens\\\ne, we\ncann\\\not d\\\nedic\\\nate, \nwe c\\\nannot\ncons\\\necra\\\nte, w\ne ca\\\nnnot \nhall\\\now t\\\nhis g\nroun\\\nd. T\\\nhe b\\\nrave \nmen, \nlivi\\\nng a\\\nnd d\\\nead w\nho s\\\ntrug\\\ngled \nhere \nhave \ncons\\\necra\\\nted i\nt far\nabove\nour p\noor p\nower \nto a\\\ndd or\ndetr\\\nact. \nThe w\norld \nwill \nlitt\\\nle n\\\note n\nor l\\\nong r\nemem\\\nber w\nhat w\ne say\nhere,\nbut i\nt can\nnever\nforg\\\net w\\\nhat t\nhey d\nid h\\\nere. \nIt is\nfor u\ns the\nlivi\\\nng r\\\nather\nto be\ndedi\\\ncated\nhere \nto t\\\nhe u\\\nnfin\\\nished\nwork \nwhich\nthey \nwho f\nought\nhere \nhave \nthus \nfar s\no no\\\nbly a\ndvan\\\nced. \nIt is\nrath\\\ner f\\\nor us\nto be\nhere \ndedi\\\ncated\nto t\\\nhe g\\\nreat \ntask \nrema\\\nining\nbefo\\\nre u\\\ns--t\\\nhat f\nrom t\nhese \nhono\\\nred d\nead w\ne ta\\\nke i\\\nncre\\\nased \ndevo\\\ntion \nto t\\\nhat c\nause \nfor w\nhich \nthey \ngave \nthe l\nast f\null m\neasu\\\nre of\ndevo\\\ntion\\\n--th\\\nat we\nhere \nhigh\\\nly r\\\nesol\\\nve t\\\nhat t\nhese \ndead \nshall\nnot h\nave d\nied i\nn va\\\nin, t\nhat t\nhis n\nation\nunder\nGod s\nhall \nhave \na new\nbirth\nof f\\\nreed\\\nom, a\nnd t\\\nhat g\nover\\\nnment\nof t\\\nhe p\\\neopl\\\ne, by\nthe p\neopl\\\ne, f\\\nor t\\\nhe p\\\neople\nshall\nnot p\nerish\nfrom \nthe e\narth.\n-- P\\\nres. \nAbra\\\nham L\ninco\\\nln, 1\n9 No\\\nvemb\\\ner 1\\\n863 \n"
41
+
42
+ FIVE_CNT = "Four \nscore\nand \nseven\nyears\nago \nour f\nathe\\\nrs b\\\nroug\\\nht f\\\north \non t\\\nhis c\nonti\\\nnent \na new\nnati\\\non, c\nonce\\\nived \nin l\\\niber\\\nty a\\\nnd d\\\nedic\\\nated \nto t\\\nhe p\\\nropo\\\nsiti\\\non t\\\nhat \nall \nmen \nare c\nreat\\\ned e\\\nqual.\nNow \nwe a\\\nre e\\\nngag\\\ned in\na gr\\\neat \ncivil\nwar, \ntest\\\ning w\nheth\\\ner t\\\nhat n\nation\nor a\\\nny n\\\nation\nso c\\\nonce\\\nived \nand \nso d\\\nedic\\\nated \ncan \nlong \nendu\\\nre. \nWe a\\\nre m\\\net on\na gr\\\neat b\nattl\\\nefie\\\nld of\nthat \nwar. \nWe h\\\nave \ncome \nto d\\\nedic\\\nate a\nport\\\nion \nof t\\\nhat \nfield\nas a \nfinal\nrest\\\ning-\\\nplace\nfor \nthose\nwho \nhere \ngave \ntheir\nlives\nthat \nthat \nnati\\\non m\\\night \nlive.\nIt is\nalto\\\ngeth\\\ner f\\\nitti\\\nng a\\\nnd p\\\nroper\nthat \nwe s\\\nhould\ndo t\\\nhis. \nBut \nin a \nlarg\\\ner s\\\nense,\nwe c\\\nannot\ndedi\\\ncate,\nwe c\\\nannot\ncons\\\necra\\\nte, \nwe c\\\nannot\nhall\\\now t\\\nhis g\nroun\\\nd. T\\\nhe b\\\nrave \nmen, \nlivi\\\nng a\\\nnd d\\\nead \nwho s\ntrug\\\ngled \nhere \nhave \ncons\\\necra\\\nted \nit f\\\nar a\\\nbove \nour \npoor \npower\nto a\\\ndd or\ndetr\\\nact. \nThe \nworld\nwill \nlitt\\\nle n\\\note \nnor \nlong \nreme\\\nmber \nwhat \nwe s\\\nay h\\\nere, \nbut \nit c\\\nan n\\\never \nforg\\\net w\\\nhat \nthey \ndid \nhere.\nIt is\nfor \nus t\\\nhe l\\\niving\nrath\\\ner to\nbe d\\\nedic\\\nated \nhere \nto t\\\nhe u\\\nnfin\\\nished\nwork \nwhich\nthey \nwho f\nought\nhere \nhave \nthus \nfar \nso n\\\nobly \nadva\\\nnced.\nIt is\nrath\\\ner f\\\nor us\nto be\nhere \ndedi\\\ncated\nto t\\\nhe g\\\nreat \ntask \nrema\\\nining\nbefo\\\nre u\\\ns--t\\\nhat \nfrom \nthese\nhono\\\nred \ndead \nwe t\\\nake i\nncre\\\nased \ndevo\\\ntion \nto t\\\nhat \ncause\nfor \nwhich\nthey \ngave \nthe \nlast \nfull \nmeas\\\nure \nof d\\\nevot\\\nion-\\\n-that\nwe h\\\nere h\nighly\nreso\\\nlve \nthat \nthese\ndead \nshall\nnot \nhave \ndied \nin v\\\nain, \nthat \nthis \nnati\\\non u\\\nnder \nGod \nshall\nhave \na new\nbirth\nof f\\\nreed\\\nom, \nand \nthat \ngove\\\nrnme\\\nnt of\nthe p\neopl\\\ne, by\nthe p\neopl\\\ne, f\\\nor t\\\nhe p\\\neople\nshall\nnot p\nerish\nfrom \nthe e\narth.\n-- P\\\nres. \nAbra\\\nham L\ninco\\\nln, \n19 N\\\novem\\\nber \n1863 \n"
43
+
44
+ # Tests both abbreviations and abbreviations=
45
+ def test_abbreviations
46
+ abbr = [" Pres. Abraham Lincoln\n", " Pres. Abraham Lincoln\n"]
47
+
48
+ @format_o = Text::Format.new
49
+ assert_equal([], @format_o.abbreviations)
50
+
51
+ @format_o.abbreviations = [ 'foo', 'bar' ]
52
+ assert_equal([ 'foo', 'bar' ], @format_o.abbreviations)
53
+ assert_equal(abbr[0], @format_o.format(abbr[0]))
54
+
55
+ @format_o.extra_space = true
56
+ assert_equal(abbr[1], @format_o.format(abbr[0]))
57
+
58
+ @format_o.abbreviations = [ "Pres" ]
59
+ assert_equal([ "Pres" ], @format_o.abbreviations)
60
+ assert_equal(abbr[0], @format_o.format(abbr[0]))
61
+
62
+ @format_o.extra_space = false
63
+ assert_equal(abbr[0], @format_o.format(abbr[0]))
64
+ end
65
+
66
+ # Tests both body_indent and body_indent=
67
+ def test_body_indent
68
+ @format_o = Text::Format.new
69
+ assert_equal(0, @format_o.body_indent)
70
+
71
+ @format_o.body_indent = 7
72
+ assert_equal(7, @format_o.body_indent)
73
+
74
+ @format_o.body_indent = -3
75
+ assert_equal(3, @format_o.body_indent)
76
+
77
+ @format_o.body_indent = "9"
78
+ assert_equal(9, @format_o.body_indent)
79
+
80
+ @format_o.body_indent = "-2"
81
+ assert_equal(2, @format_o.body_indent)
82
+ assert_match(/^ [^ ]/, @format_o.format(GETTYSBURG).split("\n")[1])
83
+ end
84
+
85
+ # Tests both columns and columns=
86
+ def test_columns
87
+ @format_o = Text::Format.new
88
+ assert_equal(72, @format_o.columns)
89
+
90
+ @format_o.columns = 7
91
+ assert_equal(7, @format_o.columns)
92
+
93
+ @format_o.columns = -3
94
+ assert_equal(3, @format_o.columns)
95
+
96
+ @format_o.columns = "9"
97
+ assert_equal(9, @format_o.columns)
98
+
99
+ @format_o.columns = "-2"
100
+ assert_equal(2, @format_o.columns)
101
+
102
+ @format_o.columns = 40
103
+ assert_equal(40, @format_o.columns)
104
+ assert_match(/this continent$/,
105
+ @format_o.format(GETTYSBURG).split("\n")[1])
106
+ end
107
+
108
+ # Tests both extra_space and extra_space=
109
+ def test_extra_space
110
+ @format_o = Text::Format.new
111
+ assert_equal(false, @format_o.extra_space)
112
+
113
+ @format_o.extra_space = true
114
+ assert_equal(true, @format_o.extra_space)
115
+ end
116
+
117
+ # Tests both first_indent and first_indent=
118
+ def test_first_indent
119
+ @format_o = Text::Format.new
120
+ assert_equal(4, @format_o.first_indent)
121
+
122
+ @format_o.first_indent = 7
123
+ assert_equal(7, @format_o.first_indent)
124
+
125
+ @format_o.first_indent = -3
126
+ assert_equal(3, @format_o.first_indent)
127
+
128
+ @format_o.first_indent = "9"
129
+ assert_equal(9, @format_o.first_indent)
130
+
131
+ @format_o.first_indent = "-2"
132
+ assert_equal(2, @format_o.first_indent)
133
+ assert_match(/^ [^ ]/, @format_o.format(GETTYSBURG).split("\n")[0])
134
+ end
135
+
136
+ def test_format_style
137
+ @format_o = Text::Format.new
138
+ assert_equal(Text::Format::LEFT_ALIGN, @format_o.format_style)
139
+ assert_match(/^November 1863$/, @format_o.format(GETTYSBURG).split("\n")[-1])
140
+
141
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
142
+ assert_equal(Text::Format::RIGHT_ALIGN, @format_o.format_style)
143
+ assert_match(/^ +November 1863$/, @format_o.format(GETTYSBURG).split("\n")[-1])
144
+
145
+ @format_o.format_style = Text::Format::RIGHT_FILL
146
+ assert_equal(Text::Format::RIGHT_FILL, @format_o.format_style)
147
+ assert_match(/^November 1863 +$/, @format_o.format(GETTYSBURG).split("\n")[-1])
148
+
149
+ @format_o.format_style = Text::Format::JUSTIFY
150
+ assert_equal(Text::Format::JUSTIFY, @format_o.format_style)
151
+ assert_match(/^of freedom, and that government of the people, by the people, for the$/, @format_o.format(GETTYSBURG).split("\n")[-3])
152
+ assert_raises(ArgumentError) { @format_o.format_style = 33 }
153
+ end
154
+
155
+ def test_tag_paragraph
156
+ @format_o = Text::Format.new
157
+ assert_equal(false, @format_o.tag_paragraph)
158
+
159
+ @format_o.tag_paragraph = true
160
+ assert_equal(true, @format_o.tag_paragraph)
161
+ assert_not_equal(@format_o.paragraphs([GETTYSBURG, GETTYSBURG]),
162
+ Text::Format.new.paragraphs([GETTYSBURG, GETTYSBURG]))
163
+ end
164
+
165
+ def test_tag_text
166
+ @format_o = Text::Format.new
167
+ assert_equal([], @format_o.tag_text)
168
+ assert_equal(@format_o.format(GETTYSBURG),
169
+ Text::Format.new.format(GETTYSBURG))
170
+
171
+ @format_o.tag_paragraph = true
172
+ @format_o.tag_text = ["Gettysburg Address", "---"]
173
+
174
+ assert_not_equal(@format_o.format(GETTYSBURG), Text::Format.new.format(GETTYSBURG))
175
+ assert_not_equal(@format_o.paragraphs([GETTYSBURG, GETTYSBURG]), Text::Format.new.paragraphs([GETTYSBURG, GETTYSBURG]))
176
+ assert_not_equal(@format_o.paragraphs([GETTYSBURG, GETTYSBURG, GETTYSBURG]), Text::Format.new.paragraphs([GETTYSBURG, GETTYSBURG, GETTYSBURG]))
177
+ end
178
+
179
+ def test_justify?
180
+ @format_o = Text::Format.new
181
+ assert_equal(false, @format_o.justify?)
182
+
183
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
184
+ assert_equal(false, @format_o.justify?)
185
+
186
+ @format_o.format_style = Text::Format::RIGHT_FILL
187
+ assert_equal(false, @format_o.justify?)
188
+
189
+ @format_o.format_style = Text::Format::JUSTIFY
190
+ assert_equal(true, @format_o.justify?)
191
+ # The format testing is done in _format_style
192
+ end
193
+
194
+ def test_left_align?
195
+ @format_o = Text::Format.new
196
+ assert_equal(true, @format_o.left_align?)
197
+
198
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
199
+ assert_equal(false, @format_o.left_align?)
200
+
201
+ @format_o.format_style = Text::Format::RIGHT_FILL
202
+ assert_equal(false, @format_o.left_align?)
203
+
204
+ @format_o.format_style = Text::Format::JUSTIFY
205
+ assert_equal(false, @format_o.left_align?)
206
+ # The format testing is done in _format_style
207
+ end
208
+
209
+ def test_left_margin
210
+ @format_o = Text::Format.new
211
+ assert_equal(0, @format_o.left_margin)
212
+
213
+ @format_o.left_margin = -3
214
+ assert_equal(3, @format_o.left_margin)
215
+
216
+ @format_o.left_margin = "9"
217
+ assert_equal(9, @format_o.left_margin)
218
+
219
+ @format_o.left_margin = "-2"
220
+ assert_equal(2, @format_o.left_margin)
221
+
222
+ @format_o.left_margin = 7
223
+ assert_equal(7, @format_o.left_margin)
224
+
225
+ ft = @format_o.format(GETTYSBURG).split("\n")
226
+ assert_match(/^ {11}Four score/, ft[0])
227
+ assert_match(/^ {7}November/, ft[-1])
228
+ end
229
+
230
+ def test_hard_margins
231
+ @format_o = Text::Format.new
232
+ assert_equal(false, @format_o.hard_margins)
233
+
234
+ @format_o.hard_margins = true
235
+ @format_o.columns = 5
236
+ @format_o.first_indent = 0
237
+ @format_o.format_style = Text::Format::RIGHT_FILL
238
+ assert_equal(true, @format_o.hard_margins)
239
+ assert_equal(FIVE_COL, @format_o.format(GETTYSBURG))
240
+
241
+ @format_o.split_rules |= Text::Format::SPLIT_CONTINUATION
242
+ assert_equal(Text::Format::SPLIT_CONTINUATION_FIXED, @format_o.split_rules)
243
+ assert_equal(FIVE_CNF, @format_o.format(GETTYSBURG))
244
+
245
+ @format_o.split_rules = Text::Format::SPLIT_CONTINUATION
246
+ assert_equal(Text::Format::SPLIT_CONTINUATION, @format_o.split_rules)
247
+ assert_equal(FIVE_CNT, @format_o.format(GETTYSBURG))
248
+ end
249
+
250
+ # Tests both nobreak and nobreak_regex, since one is only useful
251
+ # with the other.
252
+ def test_nobreak
253
+ @format_o = Text::Format.new
254
+ assert_equal(false, @format_o.nobreak)
255
+ assert_equal(true, @format_o.nobreak_regex.empty?)
256
+
257
+ @format_o.nobreak = true
258
+ @format_o.nobreak_regex = { %r{this} => %r{continent} }
259
+ @format_o.columns = 77
260
+ assert_equal(true, @format_o.nobreak)
261
+ assert_equal({ %r{this} => %r{continent} }, @format_o.nobreak_regex)
262
+ assert_match(/^this continent/,
263
+ @format_o.format(GETTYSBURG).split("\n")[1])
264
+ end
265
+
266
+ def test_right_align?
267
+ @format_o = Text::Format.new
268
+ assert_equal(false, @format_o.right_align?)
269
+
270
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
271
+ assert_equal(true, @format_o.right_align?)
272
+
273
+ @format_o.format_style = Text::Format::RIGHT_FILL
274
+ assert_equal(false, @format_o.right_align?)
275
+
276
+ @format_o.format_style = Text::Format::JUSTIFY
277
+ assert_equal(false, @format_o.right_align?)
278
+ # The format testing is done in _format_style
279
+ end
280
+
281
+ def test_right_fill?
282
+ @format_o = Text::Format.new
283
+ assert_equal(false, @format_o.right_fill?)
284
+
285
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
286
+ assert_equal(false, @format_o.right_fill?)
287
+
288
+ @format_o.format_style = Text::Format::RIGHT_FILL
289
+ assert_equal(true, @format_o.right_fill?)
290
+
291
+ @format_o.format_style = Text::Format::JUSTIFY
292
+ assert_equal(false, @format_o.right_fill?)
293
+ # The format testing is done in _format_style
294
+ end
295
+
296
+ def test_right_margin
297
+ @format_o = Text::Format.new
298
+ assert_equal(0, @format_o.right_margin)
299
+
300
+ @format_o.right_margin = -3
301
+ assert_equal(3, @format_o.right_margin)
302
+
303
+ @format_o.right_margin = "9"
304
+ assert_equal(9, @format_o.right_margin)
305
+
306
+ @format_o.right_margin = "-2"
307
+ assert_equal(2, @format_o.right_margin)
308
+
309
+ @format_o.right_margin = 7
310
+ assert_equal(7, @format_o.right_margin)
311
+
312
+ ft = @format_o.format(GETTYSBURG).split("\n")
313
+ assert_match(/^ {4}Four score.*forth on$/, ft[0])
314
+ assert_match(/^November/, ft[-1])
315
+ end
316
+
317
+ def test_tabstop
318
+ @format_o = Text::Format.new
319
+ assert_equal(8, @format_o.tabstop)
320
+
321
+ @format_o.tabstop = 7
322
+ assert_equal(7, @format_o.tabstop)
323
+
324
+ @format_o.tabstop = -3
325
+ assert_equal(3, @format_o.tabstop)
326
+
327
+ @format_o.tabstop = "9"
328
+ assert_equal(9, @format_o.tabstop)
329
+
330
+ @format_o.tabstop = "-2"
331
+ assert_equal(2, @format_o.tabstop)
332
+ end
333
+
334
+ def test_text
335
+ @format_o = Text::Format.new
336
+ assert_equal([], @format_o.text)
337
+
338
+ @format_o.text = "Test Text"
339
+ assert_equal("Test Text", @format_o.text)
340
+
341
+ @format_o.text = ["Line 1", "Line 2"]
342
+ assert_equal(["Line 1", "Line 2"], @format_o.text)
343
+ end
344
+
345
+ def test_new
346
+ @format_o = Text::Format.new { |fo| fo.text = "Test 1, 2, 3" }
347
+ assert_equal("Test 1, 2, 3", @format_o.text)
348
+
349
+ @format_o = Text::Format.new(:columns => 79)
350
+ assert_equal(79, @format_o.columns)
351
+
352
+ @format_o = Text::Format.new(:columns => 80) { |fo| fo.text = "Test 4, 5, 6" }
353
+ assert_equal("Test 4, 5, 6", @format_o.text)
354
+ assert_equal(80, @format_o.columns)
355
+
356
+ @format_o = Text::Format.new(:text => "Test A, B, C")
357
+ assert_equal("Test A, B, C", @format_o.text)
358
+
359
+ @format_o = Text::Format.new(:text => "Test X, Y, Z") { |fo| fo.columns = -5 }
360
+ assert_equal("Test X, Y, Z", @format_o.text)
361
+ assert_equal(5, @format_o.columns)
362
+ end
363
+
364
+ def test_center
365
+ @format_o = Text::Format.new
366
+
367
+ ct = @format_o.center(GETTYSBURG.split("\n")).split("\n")
368
+ assert_match(/^ Four score and seven years ago our fathers brought forth on this/, ct[0])
369
+ assert_match(/^ not perish from the earth./, ct[-3])
370
+ end
371
+
372
+ def test_expand
373
+ @format_o = Text::Format.new
374
+ assert_equal(" ", @format_o.expand("\t "))
375
+
376
+ @format_o.tabstop = 4
377
+ assert_equal(" ", @format_o.expand("\t "))
378
+ end
379
+
380
+ def test_unexpand
381
+ @format_o = Text::Format.new
382
+ assert_equal("\t ", @format_o.unexpand(" "))
383
+
384
+ @format_o.tabstop = 4
385
+ assert_equal("\t ", @format_o.unexpand(" "))
386
+ end
387
+
388
+ def test_space_only
389
+ assert_equal("", Text::Format.new.format(" "))
390
+ assert_equal("", Text::Format.new.format("\n"))
391
+ assert_equal("", Text::Format.new.format(" "))
392
+ assert_equal("", Text::Format.new.format(" \n"))
393
+ assert_equal("", Text::Format.new.paragraphs("\n"))
394
+ assert_equal("", Text::Format.new.paragraphs(" "))
395
+ assert_equal("", Text::Format.new.paragraphs(" "))
396
+ assert_equal("", Text::Format.new.paragraphs(" \n"))
397
+ assert_equal("", Text::Format.new.paragraphs(["\n"]))
398
+ assert_equal("", Text::Format.new.paragraphs([" "]))
399
+ assert_equal("", Text::Format.new.paragraphs([" "]))
400
+ assert_equal("", Text::Format.new.paragraphs([" \n"]))
401
+ end
402
+
403
+ def test_splendiferous
404
+ h = nil
405
+ test = "This is a splendiferous test"
406
+ @format_o = Text::Format.new(:columns => 6, :left_margin => 0, :indent => 0, :first_indent => 0)
407
+ assert_match(/^splendiferous$/, @format_o.format(test))
408
+
409
+ @format_o.hard_margins = true
410
+ assert_match(/^lendif$/, @format_o.format(test))
411
+
412
+ h = Object.new
413
+
414
+ @format_o.split_rules = Text::Format::SPLIT_HYPHENATION
415
+ class << h
416
+ def hyphenate_to(word, size)
417
+ return [nil, word] if size < 2
418
+ [word[0 ... size], word[size .. -1]]
419
+ end
420
+ end
421
+ @format_o.hyphenator = h
422
+ assert_match(/^ferous$/, @format_o.format(test))
423
+
424
+ h = Object.new
425
+
426
+ class << h
427
+ def hyphenate_to(word, size, formatter)
428
+ return [nil, word] if word.size < formatter.columns
429
+ [word[0 ... size], word[size .. -1]]
430
+ end
431
+ end
432
+ @format_o.hyphenator = h
433
+ assert_match(/^ferous$/, @format_o.format(test))
434
+ end
435
+
436
+ def test_encephelogram
437
+ hy = nil
438
+
439
+ begin
440
+ require 'text/hyphen'
441
+ hy = Text::Hyphen.new
442
+ rescue LoadError
443
+ begin
444
+ require 'rubygems'
445
+ require 'text/hyphen'
446
+ hy = Text::Hyphen.new
447
+ rescue LoadError
448
+ begin
449
+ require 'tex/hyphen'
450
+ hy = TeX::Hyphen.new
451
+ rescue LoadError
452
+ print 'S'
453
+ return true
454
+ end
455
+ end
456
+ end
457
+
458
+ tx = "something pancakes electroencephalogram"
459
+ fo = Text::Format.new(:body_indent => 15,
460
+ :columns => 30,
461
+ :hard_margins => true,
462
+ :split_rules => Text::Format::SPLIT_HYPHENATION,
463
+ :hyphenator => hy,
464
+ :text => tx)
465
+ res = fo.paragraphs
466
+ exp = <<-EOS
467
+ something pancakes elec-
468
+ troencephalo-
469
+ gram
470
+ EOS
471
+ exp.chomp!
472
+
473
+ assert_equal(exp, res)
474
+ end
475
+ end