PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/test/rdoc/test_rdoc_text.rb

http://github.com/ruby/ruby
Ruby | 575 lines | 489 code | 77 blank | 9 comment | 0 complexity | 6c6aba849a9202755fd65736e7643ceb MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: true
  2. require_relative 'helper'
  3. require 'timeout'
  4. class TestRDocText < RDoc::TestCase
  5. include RDoc::Text
  6. def setup
  7. super
  8. @options = RDoc::Options.new
  9. @top_level = @store.add_file 'file.rb'
  10. @language = nil
  11. end
  12. def test_self_encode_fallback
  13. assert_equal '…',
  14. RDoc::Text::encode_fallback('…', Encoding::UTF_8, '...')
  15. assert_equal '...',
  16. RDoc::Text::encode_fallback('…', Encoding::US_ASCII, '...')
  17. end
  18. def test_expand_tabs
  19. assert_equal("hello\n dave",
  20. expand_tabs("hello\n dave"), 'spaces')
  21. assert_equal("hello\n dave",
  22. expand_tabs("hello\n\tdave"), 'tab')
  23. assert_equal("hello\n dave",
  24. expand_tabs("hello\n \tdave"), '1 space tab')
  25. assert_equal("hello\n dave",
  26. expand_tabs("hello\n \tdave"), '2 space tab')
  27. assert_equal("hello\n dave",
  28. expand_tabs("hello\n \tdave"), '3 space tab')
  29. assert_equal("hello\n dave",
  30. expand_tabs("hello\n \tdave"), '4 space tab')
  31. assert_equal("hello\n dave",
  32. expand_tabs("hello\n \tdave"), '5 space tab')
  33. assert_equal("hello\n dave",
  34. expand_tabs("hello\n \tdave"), '6 space tab')
  35. assert_equal("hello\n dave",
  36. expand_tabs("hello\n \tdave"), '7 space tab')
  37. assert_equal("hello\n dave",
  38. expand_tabs("hello\n \tdave"), '8 space tab')
  39. assert_equal('. .',
  40. expand_tabs(".\t\t."), 'dot tab tab dot')
  41. assert_equal('a a',
  42. Timeout.timeout(1) {expand_tabs("\ra\ta")}, "carriage return")
  43. end
  44. def test_expand_tabs_encoding
  45. inn = "hello\ns\tdave"
  46. inn = RDoc::Encoding.change_encoding inn, Encoding::BINARY
  47. out = expand_tabs inn
  48. assert_equal "hello\ns dave", out
  49. assert_equal Encoding::BINARY, out.encoding
  50. end
  51. def test_flush_left
  52. text = <<-TEXT
  53. we don't worry too much.
  54. The comments associated with
  55. TEXT
  56. expected = <<-EXPECTED
  57. we don't worry too much.
  58. The comments associated with
  59. EXPECTED
  60. assert_equal expected, flush_left(text)
  61. end
  62. def test_flush_left_encoding
  63. text = <<-TEXT
  64. we don't worry too much.
  65. The comments associated with
  66. TEXT
  67. text = RDoc::Encoding.change_encoding text, Encoding::US_ASCII
  68. expected = <<-EXPECTED
  69. we don't worry too much.
  70. The comments associated with
  71. EXPECTED
  72. result = flush_left text
  73. assert_equal expected, result
  74. assert_equal Encoding::US_ASCII, result.encoding
  75. end
  76. def test_markup_string
  77. out = markup('hi').gsub("\n", '')
  78. assert_equal '<p>hi</p>', out
  79. end
  80. def test_markup_comment
  81. out = markup(comment('hi')).gsub("\n", '')
  82. assert_equal '<p>hi</p>', out
  83. end
  84. def test_normalize_comment_hash
  85. text = <<-TEXT
  86. ##
  87. # we don't worry too much.
  88. #
  89. # The comments associated with
  90. TEXT
  91. expected = <<-EXPECTED.rstrip
  92. we don't worry too much.
  93. The comments associated with
  94. EXPECTED
  95. @language = :ruby
  96. assert_equal expected, normalize_comment(text)
  97. end
  98. def test_normalize_comment_stars_single_space
  99. text = <<-TEXT
  100. /*
  101. * we don't worry too much.
  102. *
  103. * The comments associated with
  104. */
  105. TEXT
  106. expected = <<-EXPECTED.rstrip
  107. we don't worry too much.
  108. The comments associated with
  109. EXPECTED
  110. @language = :c
  111. assert_equal expected, normalize_comment(text)
  112. end
  113. def test_normalize_comment_stars_single_double_space
  114. text = <<-TEXT
  115. /*
  116. * we don't worry too much.
  117. *
  118. * The comments associated with
  119. */
  120. TEXT
  121. expected = <<-EXPECTED.rstrip
  122. we don't worry too much.
  123. The comments associated with
  124. EXPECTED
  125. @language = :c
  126. assert_equal expected, normalize_comment(text)
  127. end
  128. def test_parse
  129. assert_kind_of RDoc::Markup::Document, parse('hi')
  130. end
  131. def test_parse_comment
  132. expected = RDoc::Markup::Document.new
  133. expected.file = @top_level
  134. c = comment ''
  135. parsed = parse c
  136. assert_equal expected, parsed
  137. assert_same parsed, parse(c)
  138. end
  139. def test_parse_document
  140. assert_equal RDoc::Markup::Document.new, parse(RDoc::Markup::Document.new)
  141. end
  142. def test_parse_empty
  143. assert_equal RDoc::Markup::Document.new, parse('')
  144. end
  145. def test_parse_empty_newline
  146. @language = :ruby
  147. assert_equal RDoc::Markup::Document.new, parse("#\n")
  148. end
  149. def test_parse_format_markdown
  150. expected =
  151. @RM::Document.new(
  152. @RM::Paragraph.new('it _works_'))
  153. parsed = parse 'it *works*', 'markdown'
  154. assert_equal expected, parsed
  155. end
  156. def test_parse_format_rd
  157. expected =
  158. @RM::Document.new(
  159. @RM::Paragraph.new('it <em>works</em>'))
  160. parsed = parse 'it ((*works*))', 'rd'
  161. assert_equal expected, parsed
  162. end
  163. def test_parse_format_tomdoc
  164. code = verb('1 + 1')
  165. code.format = :ruby
  166. expected =
  167. doc(
  168. para('It does a thing'),
  169. blank_line,
  170. head(3, 'Examples'),
  171. blank_line,
  172. code)
  173. text = <<-TOMDOC
  174. It does a thing
  175. Examples
  176. 1 + 1
  177. TOMDOC
  178. parsed = parse text, 'tomdoc'
  179. assert_equal expected, parsed
  180. end
  181. def test_parse_newline
  182. assert_equal RDoc::Markup::Document.new, parse("\n")
  183. end
  184. def test_snippet
  185. text = <<-TEXT
  186. This is one-hundred characters or more of text in a single paragraph. This
  187. paragraph will be cut off some point after the one-hundredth character.
  188. TEXT
  189. expected = <<-EXPECTED
  190. <p>This is one-hundred characters or more of text in a single paragraph. This paragraph will be cut off
  191. EXPECTED
  192. assert_equal expected, snippet(text)
  193. end
  194. def test_snippet_comment
  195. c = comment 'This is a comment'
  196. assert_equal "<p>This is a comment\n", snippet(c)
  197. end
  198. def test_snippet_short
  199. text = 'This is a comment'
  200. assert_equal "<p>#{text}\n", snippet(text)
  201. end
  202. def test_strip_hashes
  203. text = <<-TEXT
  204. ##
  205. # we don't worry too much.
  206. #
  207. # The comments associated with
  208. TEXT
  209. expected = <<-EXPECTED
  210. we don't worry too much.
  211. The comments associated with
  212. EXPECTED
  213. assert_equal expected, strip_hashes(text)
  214. end
  215. def test_strip_hashes_encoding
  216. text = <<-TEXT
  217. ##
  218. # we don't worry too much.
  219. #
  220. # The comments associated with
  221. TEXT
  222. text = RDoc::Encoding.change_encoding text, Encoding::CP852
  223. expected = <<-EXPECTED
  224. we don't worry too much.
  225. The comments associated with
  226. EXPECTED
  227. stripped = strip_hashes text
  228. assert_equal expected, stripped
  229. assert_equal Encoding::CP852, stripped.encoding
  230. end
  231. def test_strip_newlines
  232. assert_equal ' ', strip_newlines("\n \n")
  233. assert_equal 'hi', strip_newlines("\n\nhi")
  234. assert_equal 'hi', strip_newlines( "hi\n\n")
  235. assert_equal 'hi', strip_newlines("\n\nhi\n\n")
  236. end
  237. def test_strip_newlines_encoding
  238. assert_equal Encoding::UTF_8, ''.encoding, 'Encoding sanity check'
  239. text = " \n"
  240. text = RDoc::Encoding.change_encoding text, Encoding::US_ASCII
  241. stripped = strip_newlines text
  242. assert_equal ' ', stripped
  243. assert_equal Encoding::US_ASCII, stripped.encoding
  244. end
  245. def test_strip_stars
  246. text = <<-TEXT
  247. /*
  248. * * we don't worry too much.
  249. *
  250. * The comments associated with
  251. */
  252. TEXT
  253. expected = <<-EXPECTED
  254. * we don't worry too much.
  255. The comments associated with
  256. EXPECTED
  257. assert_equal expected, strip_stars(text)
  258. end
  259. def test_strip_stars_document_method
  260. text = <<-TEXT
  261. /*
  262. * Document-method: Zlib::GzipFile#mtime=
  263. *
  264. * A comment
  265. */
  266. TEXT
  267. expected = <<-EXPECTED
  268. A comment
  269. EXPECTED
  270. assert_equal expected, strip_stars(text)
  271. end
  272. def test_strip_stars_document_method_special
  273. text = <<-TEXT
  274. /*
  275. * Document-method: Zlib::GzipFile#mtime=
  276. * Document-method: []
  277. * Document-method: `
  278. * Document-method: |
  279. * Document-method: &
  280. * Document-method: <=>
  281. * Document-method: =~
  282. * Document-method: +
  283. * Document-method: -
  284. * Document-method: +@
  285. *
  286. * A comment
  287. */
  288. TEXT
  289. expected = <<-EXPECTED
  290. A comment
  291. EXPECTED
  292. assert_equal expected, strip_stars(text)
  293. end
  294. def test_strip_stars_encoding
  295. text = <<-TEXT
  296. /*
  297. * * we don't worry too much.
  298. *
  299. * The comments associated with
  300. */
  301. TEXT
  302. text = RDoc::Encoding.change_encoding text, Encoding::CP852
  303. expected = <<-EXPECTED
  304. * we don't worry too much.
  305. The comments associated with
  306. EXPECTED
  307. result = strip_stars text
  308. assert_equal expected, result
  309. assert_equal Encoding::CP852, result.encoding
  310. end
  311. def test_strip_stars_encoding2
  312. text = <<-TEXT
  313. /*
  314. * * we don't worry too much.
  315. *
  316. * The comments associated with
  317. */
  318. TEXT
  319. text = RDoc::Encoding.change_encoding text, Encoding::BINARY
  320. expected = <<-EXPECTED
  321. * we don't worry too much.
  322. The comments associated with
  323. EXPECTED
  324. result = strip_stars text
  325. assert_equal expected, result
  326. assert_equal Encoding::BINARY, result.encoding
  327. end
  328. def test_strip_stars_no_stars
  329. text = <<-TEXT
  330. * we don't worry too much.
  331. The comments associated with
  332. TEXT
  333. expected = <<-EXPECTED
  334. * we don't worry too much.
  335. The comments associated with
  336. EXPECTED
  337. assert_equal expected, strip_stars(text)
  338. end
  339. def test_to_html_apostrophe
  340. assert_equal '‘a', to_html("'a")
  341. assert_equal 'a’', to_html("a'")
  342. assert_equal '‘a’ ‘', to_html("'a' '")
  343. end
  344. def test_to_html_backslash
  345. assert_equal 'S', to_html('\\S')
  346. end
  347. def test_to_html_br
  348. assert_equal '<br>', to_html('<br>')
  349. end
  350. def test_to_html_copyright
  351. assert_equal '©', to_html('(c)')
  352. end
  353. def test_to_html_dash
  354. assert_equal '-', to_html('-')
  355. assert_equal '–', to_html('--')
  356. assert_equal '—', to_html('---')
  357. assert_equal '—-', to_html('----')
  358. end
  359. def test_to_html_double_backtick
  360. assert_equal '“a', to_html('``a')
  361. assert_equal '“a“', to_html('``a``')
  362. end
  363. def test_to_html_double_quote
  364. assert_equal '“a', to_html('"a')
  365. assert_equal '“a”', to_html('"a"')
  366. end
  367. def test_to_html_double_quote_quot
  368. assert_equal '“a', to_html('&quot;a')
  369. assert_equal '“a”', to_html('&quot;a&quot;')
  370. end
  371. def test_to_html_double_tick
  372. assert_equal '”a', to_html("''a")
  373. assert_equal '”a”', to_html("''a''")
  374. end
  375. def test_to_html_ellipsis
  376. assert_equal '..', to_html('..')
  377. assert_equal '…', to_html('...')
  378. assert_equal '.…', to_html('....')
  379. end
  380. def test_to_html_encoding
  381. s = '...(c)'.encode Encoding::Shift_JIS
  382. html = to_html s
  383. assert_equal Encoding::Shift_JIS, html.encoding
  384. expected = '…(c)'.encode Encoding::Shift_JIS
  385. assert_equal expected, html
  386. end
  387. def test_to_html_html_tag
  388. assert_equal '<a href="http://example">hi’s</a>',
  389. to_html('<a href="http://example">hi\'s</a>')
  390. end
  391. def test_to_html_registered_trademark
  392. assert_equal '®', to_html('(r)')
  393. end
  394. def test_to_html_tt_tag
  395. assert_equal '<tt>hi\'s</tt>', to_html('<tt>hi\'s</tt>')
  396. assert_equal '<tt>hi\\\'s</tt>', to_html('<tt>hi\\\\\'s</tt>')
  397. end
  398. def test_to_html_tt_tag_mismatch
  399. _, err = verbose_capture_output do
  400. assert_equal '<tt>hi', to_html('<tt>hi')
  401. end
  402. assert_equal "mismatched <tt> tag\n", err
  403. end
  404. def formatter
  405. RDoc::Markup::ToHtml.new @options
  406. end
  407. def options
  408. @options
  409. end
  410. end