text-format 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,474 @@
1
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
2
+
3
+ require 'text/format'
4
+ require 'test/unit'
5
+
6
+ class TestText__Format < Test::Unit::TestCase
7
+ attr_accessor :format_o
8
+
9
+ GETTYSBURG = <<-'EOS'
10
+ Four score and seven years ago our fathers brought forth on this
11
+ continent a new nation, conceived in liberty and dedicated to the
12
+ proposition that all men are created equal. Now we are engaged in
13
+ a great civil war, testing whether that nation or any nation so
14
+ conceived and so dedicated can long endure. We are met on a great
15
+ battlefield of that war. We have come to dedicate a portion of
16
+ that field as a final resting-place for those who here gave their
17
+ lives that that nation might live. It is altogether fitting and
18
+ proper that we should do this. But in a larger sense, we cannot
19
+ dedicate, we cannot consecrate, we cannot hallow this ground.
20
+ The brave men, living and dead who struggled here have consecrated
21
+ it far above our poor power to add or detract. The world will
22
+ little note nor long remember what we say here, but it can never
23
+ forget what they did here. It is for us the living rather to be
24
+ dedicated here to the unfinished work which they who fought here
25
+ have thus far so nobly advanced. It is rather for us to be here
26
+ dedicated to the great task remaining before us--that from these
27
+ honored dead we take increased devotion to that cause for which
28
+ they gave the last full measure of devotion--that we here highly
29
+ resolve that these dead shall not have died in vain, that this
30
+ nation under God shall have a new birth of freedom, and that
31
+ government of the people, by the people, for the people shall
32
+ not perish from the earth.
33
+
34
+ -- Pres. Abraham Lincoln, 19 November 1863
35
+ EOS
36
+
37
+ 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"
38
+
39
+ 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"
40
+
41
+ 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"
42
+
43
+ # Tests both abbreviations and abbreviations=
44
+ def test_abbreviations
45
+ abbr = [" Pres. Abraham Lincoln\n", " Pres. Abraham Lincoln\n"]
46
+
47
+ @format_o = Text::Format.new
48
+ assert_equal([], @format_o.abbreviations)
49
+
50
+ @format_o.abbreviations = [ 'foo', 'bar' ]
51
+ assert_equal([ 'foo', 'bar' ], @format_o.abbreviations)
52
+ assert_equal(abbr[0], @format_o.format(abbr[0]))
53
+
54
+ @format_o.extra_space = true
55
+ assert_equal(abbr[1], @format_o.format(abbr[0]))
56
+
57
+ @format_o.abbreviations = [ "Pres" ]
58
+ assert_equal([ "Pres" ], @format_o.abbreviations)
59
+ assert_equal(abbr[0], @format_o.format(abbr[0]))
60
+
61
+ @format_o.extra_space = false
62
+ assert_equal(abbr[0], @format_o.format(abbr[0]))
63
+ end
64
+
65
+ # Tests both body_indent and body_indent=
66
+ def test_body_indent
67
+ @format_o = Text::Format.new
68
+ assert_equal(0, @format_o.body_indent)
69
+
70
+ @format_o.body_indent = 7
71
+ assert_equal(7, @format_o.body_indent)
72
+
73
+ @format_o.body_indent = -3
74
+ assert_equal(3, @format_o.body_indent)
75
+
76
+ @format_o.body_indent = "9"
77
+ assert_equal(9, @format_o.body_indent)
78
+
79
+ @format_o.body_indent = "-2"
80
+ assert_equal(2, @format_o.body_indent)
81
+ assert_match(/^ [^ ]/, @format_o.format(GETTYSBURG).split("\n")[1])
82
+ end
83
+
84
+ # Tests both columns and columns=
85
+ def test_columns
86
+ @format_o = Text::Format.new
87
+ assert_equal(72, @format_o.columns)
88
+
89
+ @format_o.columns = 7
90
+ assert_equal(7, @format_o.columns)
91
+
92
+ @format_o.columns = -3
93
+ assert_equal(3, @format_o.columns)
94
+
95
+ @format_o.columns = "9"
96
+ assert_equal(9, @format_o.columns)
97
+
98
+ @format_o.columns = "-2"
99
+ assert_equal(2, @format_o.columns)
100
+
101
+ @format_o.columns = 40
102
+ assert_equal(40, @format_o.columns)
103
+ assert_match(/this continent$/,
104
+ @format_o.format(GETTYSBURG).split("\n")[1])
105
+ end
106
+
107
+ # Tests both extra_space and extra_space=
108
+ def test_extra_space
109
+ @format_o = Text::Format.new
110
+ assert_equal(false, @format_o.extra_space)
111
+
112
+ @format_o.extra_space = true
113
+ assert_equal(true, @format_o.extra_space)
114
+ end
115
+
116
+ # Tests both first_indent and first_indent=
117
+ def test_first_indent
118
+ @format_o = Text::Format.new
119
+ assert_equal(4, @format_o.first_indent)
120
+
121
+ @format_o.first_indent = 7
122
+ assert_equal(7, @format_o.first_indent)
123
+
124
+ @format_o.first_indent = -3
125
+ assert_equal(3, @format_o.first_indent)
126
+
127
+ @format_o.first_indent = "9"
128
+ assert_equal(9, @format_o.first_indent)
129
+
130
+ @format_o.first_indent = "-2"
131
+ assert_equal(2, @format_o.first_indent)
132
+ assert_match(/^ [^ ]/, @format_o.format(GETTYSBURG).split("\n")[0])
133
+ end
134
+
135
+ def test_format_style
136
+ @format_o = Text::Format.new
137
+ assert_equal(Text::Format::LEFT_ALIGN, @format_o.format_style)
138
+ assert_match(/^November 1863$/, @format_o.format(GETTYSBURG).split("\n")[-1])
139
+
140
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
141
+ assert_equal(Text::Format::RIGHT_ALIGN, @format_o.format_style)
142
+ assert_match(/^ +November 1863$/, @format_o.format(GETTYSBURG).split("\n")[-1])
143
+
144
+ @format_o.format_style = Text::Format::RIGHT_FILL
145
+ assert_equal(Text::Format::RIGHT_FILL, @format_o.format_style)
146
+ assert_match(/^November 1863 +$/, @format_o.format(GETTYSBURG).split("\n")[-1])
147
+
148
+ @format_o.format_style = Text::Format::JUSTIFY
149
+ assert_equal(Text::Format::JUSTIFY, @format_o.format_style)
150
+ assert_match(/^of freedom, and that government of the people, by the people, for the$/, @format_o.format(GETTYSBURG).split("\n")[-3])
151
+ assert_raises(ArgumentError) { @format_o.format_style = 33 }
152
+ end
153
+
154
+ def test_tag_paragraph
155
+ @format_o = Text::Format.new
156
+ assert_equal(false, @format_o.tag_paragraph)
157
+
158
+ @format_o.tag_paragraph = true
159
+ assert_equal(true, @format_o.tag_paragraph)
160
+ assert_not_equal(@format_o.paragraphs([GETTYSBURG, GETTYSBURG]),
161
+ Text::Format.new.paragraphs([GETTYSBURG, GETTYSBURG]))
162
+ end
163
+
164
+ def test_tag_text
165
+ @format_o = Text::Format.new
166
+ assert_equal([], @format_o.tag_text)
167
+ assert_equal(@format_o.format(GETTYSBURG),
168
+ Text::Format.new.format(GETTYSBURG))
169
+
170
+ @format_o.tag_paragraph = true
171
+ @format_o.tag_text = ["Gettysburg Address", "---"]
172
+
173
+ assert_not_equal(@format_o.format(GETTYSBURG), Text::Format.new.format(GETTYSBURG))
174
+ assert_not_equal(@format_o.paragraphs([GETTYSBURG, GETTYSBURG]), Text::Format.new.paragraphs([GETTYSBURG, GETTYSBURG]))
175
+ assert_not_equal(@format_o.paragraphs([GETTYSBURG, GETTYSBURG, GETTYSBURG]), Text::Format.new.paragraphs([GETTYSBURG, GETTYSBURG, GETTYSBURG]))
176
+ end
177
+
178
+ def test_justify?
179
+ @format_o = Text::Format.new
180
+ assert_equal(false, @format_o.justify?)
181
+
182
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
183
+ assert_equal(false, @format_o.justify?)
184
+
185
+ @format_o.format_style = Text::Format::RIGHT_FILL
186
+ assert_equal(false, @format_o.justify?)
187
+
188
+ @format_o.format_style = Text::Format::JUSTIFY
189
+ assert_equal(true, @format_o.justify?)
190
+ # The format testing is done in _format_style
191
+ end
192
+
193
+ def test_left_align?
194
+ @format_o = Text::Format.new
195
+ assert_equal(true, @format_o.left_align?)
196
+
197
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
198
+ assert_equal(false, @format_o.left_align?)
199
+
200
+ @format_o.format_style = Text::Format::RIGHT_FILL
201
+ assert_equal(false, @format_o.left_align?)
202
+
203
+ @format_o.format_style = Text::Format::JUSTIFY
204
+ assert_equal(false, @format_o.left_align?)
205
+ # The format testing is done in _format_style
206
+ end
207
+
208
+ def test_left_margin
209
+ @format_o = Text::Format.new
210
+ assert_equal(0, @format_o.left_margin)
211
+
212
+ @format_o.left_margin = -3
213
+ assert_equal(3, @format_o.left_margin)
214
+
215
+ @format_o.left_margin = "9"
216
+ assert_equal(9, @format_o.left_margin)
217
+
218
+ @format_o.left_margin = "-2"
219
+ assert_equal(2, @format_o.left_margin)
220
+
221
+ @format_o.left_margin = 7
222
+ assert_equal(7, @format_o.left_margin)
223
+
224
+ ft = @format_o.format(GETTYSBURG).split("\n")
225
+ assert_match(/^ {11}Four score/, ft[0])
226
+ assert_match(/^ {7}November/, ft[-1])
227
+ end
228
+
229
+ def test_hard_margins
230
+ @format_o = Text::Format.new
231
+ assert_equal(false, @format_o.hard_margins)
232
+
233
+ @format_o.hard_margins = true
234
+ @format_o.columns = 5
235
+ @format_o.first_indent = 0
236
+ @format_o.format_style = Text::Format::RIGHT_FILL
237
+ assert_equal(true, @format_o.hard_margins)
238
+ assert_equal(FIVE_COL, @format_o.format(GETTYSBURG))
239
+
240
+ @format_o.split_rules |= Text::Format::SPLIT_CONTINUATION
241
+ assert_equal(Text::Format::SPLIT_CONTINUATION_FIXED, @format_o.split_rules)
242
+ assert_equal(FIVE_CNF, @format_o.format(GETTYSBURG))
243
+
244
+ @format_o.split_rules = Text::Format::SPLIT_CONTINUATION
245
+ assert_equal(Text::Format::SPLIT_CONTINUATION, @format_o.split_rules)
246
+ assert_equal(FIVE_CNT, @format_o.format(GETTYSBURG))
247
+ end
248
+
249
+ # Tests both nobreak and nobreak_regex, since one is only useful
250
+ # with the other.
251
+ def test_nobreak
252
+ @format_o = Text::Format.new
253
+ assert_equal(false, @format_o.nobreak)
254
+ assert_equal(true, @format_o.nobreak_regex.empty?)
255
+
256
+ @format_o.nobreak = true
257
+ @format_o.nobreak_regex = { %r{this} => %r{continent} }
258
+ @format_o.columns = 77
259
+ assert_equal(true, @format_o.nobreak)
260
+ assert_equal({ %r{this} => %r{continent} }, @format_o.nobreak_regex)
261
+ assert_match(/^this continent/,
262
+ @format_o.format(GETTYSBURG).split("\n")[1])
263
+ end
264
+
265
+ def test_right_align?
266
+ @format_o = Text::Format.new
267
+ assert_equal(false, @format_o.right_align?)
268
+
269
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
270
+ assert_equal(true, @format_o.right_align?)
271
+
272
+ @format_o.format_style = Text::Format::RIGHT_FILL
273
+ assert_equal(false, @format_o.right_align?)
274
+
275
+ @format_o.format_style = Text::Format::JUSTIFY
276
+ assert_equal(false, @format_o.right_align?)
277
+ # The format testing is done in _format_style
278
+ end
279
+
280
+ def test_right_fill?
281
+ @format_o = Text::Format.new
282
+ assert_equal(false, @format_o.right_fill?)
283
+
284
+ @format_o.format_style = Text::Format::RIGHT_ALIGN
285
+ assert_equal(false, @format_o.right_fill?)
286
+
287
+ @format_o.format_style = Text::Format::RIGHT_FILL
288
+ assert_equal(true, @format_o.right_fill?)
289
+
290
+ @format_o.format_style = Text::Format::JUSTIFY
291
+ assert_equal(false, @format_o.right_fill?)
292
+ # The format testing is done in _format_style
293
+ end
294
+
295
+ def test_right_margin
296
+ @format_o = Text::Format.new
297
+ assert_equal(0, @format_o.right_margin)
298
+
299
+ @format_o.right_margin = -3
300
+ assert_equal(3, @format_o.right_margin)
301
+
302
+ @format_o.right_margin = "9"
303
+ assert_equal(9, @format_o.right_margin)
304
+
305
+ @format_o.right_margin = "-2"
306
+ assert_equal(2, @format_o.right_margin)
307
+
308
+ @format_o.right_margin = 7
309
+ assert_equal(7, @format_o.right_margin)
310
+
311
+ ft = @format_o.format(GETTYSBURG).split("\n")
312
+ assert_match(/^ {4}Four score.*forth on$/, ft[0])
313
+ assert_match(/^November/, ft[-1])
314
+ end
315
+
316
+ def test_tabstop
317
+ @format_o = Text::Format.new
318
+ assert_equal(8, @format_o.tabstop)
319
+
320
+ @format_o.tabstop = 7
321
+ assert_equal(7, @format_o.tabstop)
322
+
323
+ @format_o.tabstop = -3
324
+ assert_equal(3, @format_o.tabstop)
325
+
326
+ @format_o.tabstop = "9"
327
+ assert_equal(9, @format_o.tabstop)
328
+
329
+ @format_o.tabstop = "-2"
330
+ assert_equal(2, @format_o.tabstop)
331
+ end
332
+
333
+ def test_text
334
+ @format_o = Text::Format.new
335
+ assert_equal([], @format_o.text)
336
+
337
+ @format_o.text = "Test Text"
338
+ assert_equal("Test Text", @format_o.text)
339
+
340
+ @format_o.text = ["Line 1", "Line 2"]
341
+ assert_equal(["Line 1", "Line 2"], @format_o.text)
342
+ end
343
+
344
+ def test_new
345
+ @format_o = Text::Format.new { |fo| fo.text = "Test 1, 2, 3" }
346
+ assert_equal("Test 1, 2, 3", @format_o.text)
347
+
348
+ @format_o = Text::Format.new(:columns => 79)
349
+ assert_equal(79, @format_o.columns)
350
+
351
+ @format_o = Text::Format.new(:columns => 80) { |fo| fo.text = "Test 4, 5, 6" }
352
+ assert_equal("Test 4, 5, 6", @format_o.text)
353
+ assert_equal(80, @format_o.columns)
354
+
355
+ @format_o = Text::Format.new(:text => "Test A, B, C")
356
+ assert_equal("Test A, B, C", @format_o.text)
357
+
358
+ @format_o = Text::Format.new(:text => "Test X, Y, Z") { |fo| fo.columns = -5 }
359
+ assert_equal("Test X, Y, Z", @format_o.text)
360
+ assert_equal(5, @format_o.columns)
361
+ end
362
+
363
+ def test_center
364
+ @format_o = Text::Format.new
365
+
366
+ ct = @format_o.center(GETTYSBURG.split("\n")).split("\n")
367
+ assert_match(/^ Four score and seven years ago our fathers brought forth on this/, ct[0])
368
+ assert_match(/^ not perish from the earth./, ct[-3])
369
+ end
370
+
371
+ def test_expand
372
+ @format_o = Text::Format.new
373
+ assert_equal(" ", @format_o.expand("\t "))
374
+
375
+ @format_o.tabstop = 4
376
+ assert_equal(" ", @format_o.expand("\t "))
377
+ end
378
+
379
+ def test_unexpand
380
+ @format_o = Text::Format.new
381
+ assert_equal("\t ", @format_o.unexpand(" "))
382
+
383
+ @format_o.tabstop = 4
384
+ assert_equal("\t ", @format_o.unexpand(" "))
385
+ end
386
+
387
+ def test_space_only
388
+ assert_equal("", Text::Format.new.format(" "))
389
+ assert_equal("", Text::Format.new.format("\n"))
390
+ assert_equal("", Text::Format.new.format(" "))
391
+ assert_equal("", Text::Format.new.format(" \n"))
392
+ assert_equal("", Text::Format.new.paragraphs("\n"))
393
+ assert_equal("", Text::Format.new.paragraphs(" "))
394
+ assert_equal("", Text::Format.new.paragraphs(" "))
395
+ assert_equal("", Text::Format.new.paragraphs(" \n"))
396
+ assert_equal("", Text::Format.new.paragraphs(["\n"]))
397
+ assert_equal("", Text::Format.new.paragraphs([" "]))
398
+ assert_equal("", Text::Format.new.paragraphs([" "]))
399
+ assert_equal("", Text::Format.new.paragraphs([" \n"]))
400
+ end
401
+
402
+ def test_splendiferous
403
+ h = nil
404
+ test = "This is a splendiferous test"
405
+ @format_o = Text::Format.new(:columns => 6, :left_margin => 0, :indent => 0, :first_indent => 0)
406
+ assert_match(/^splendiferous$/, @format_o.format(test))
407
+
408
+ @format_o.hard_margins = true
409
+ assert_match(/^lendif$/, @format_o.format(test))
410
+
411
+ h = Object.new
412
+
413
+ @format_o.split_rules = Text::Format::SPLIT_HYPHENATION
414
+ class << h
415
+ def hyphenate_to(word, size)
416
+ return [nil, word] if size < 2
417
+ [word[0 ... size], word[size .. -1]]
418
+ end
419
+ end
420
+ @format_o.hyphenator = h
421
+ assert_match(/^ferous$/, @format_o.format(test))
422
+
423
+ h = Object.new
424
+
425
+ class << h
426
+ def hyphenate_to(word, size, formatter)
427
+ return [nil, word] if word.size < formatter.columns
428
+ [word[0 ... size], word[size .. -1]]
429
+ end
430
+ end
431
+ @format_o.hyphenator = h
432
+ assert_match(/^ferous$/, @format_o.format(test))
433
+ end
434
+
435
+ def test_encephelogram
436
+ hy = nil
437
+
438
+ begin
439
+ require 'text/hyphen'
440
+ hy = Text::Hyphen.new
441
+ rescue LoadError
442
+ begin
443
+ require 'rubygems'
444
+ require 'text/hyphen'
445
+ hy = Text::Hyphen.new
446
+ rescue LoadError
447
+ begin
448
+ require 'tex/hyphen'
449
+ hy = TeX::Hyphen.new
450
+ rescue LoadError
451
+ print 'S'
452
+ return true
453
+ end
454
+ end
455
+ end
456
+
457
+ tx = "something pancakes electroencephalogram"
458
+ fo = Text::Format.new(:body_indent => 15,
459
+ :columns => 30,
460
+ :hard_margins => true,
461
+ :split_rules => Text::Format::SPLIT_HYPHENATION,
462
+ :hyphenator => hy,
463
+ :text => tx)
464
+ res = fo.paragraphs
465
+ exp = <<-EOS
466
+ something pancakes elec-
467
+ troencephalo-
468
+ gram
469
+ EOS
470
+ exp.chomp!
471
+
472
+ assert_equal(exp, res)
473
+ end
474
+ end