PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/text-format-1.0.0/tests/tc_text_format.rb

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