PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/lib/ruby/1.8/rdoc/markup/test/TestParse.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 503 lines | 408 code | 94 blank | 1 comment | 4 complexity | 8ade4ae9ff8483f5a58c1600a760271e MD5 | raw file
  1. require 'test/unit'
  2. $:.unshift "../../.."
  3. require 'rdoc/markup/simple_markup'
  4. include SM
  5. class TestParse < Test::Unit::TestCase
  6. class MockOutput
  7. def start_accepting
  8. @res = []
  9. end
  10. def end_accepting
  11. @res
  12. end
  13. def accept_paragraph(am, fragment)
  14. @res << fragment.to_s
  15. end
  16. def accept_verbatim(am, fragment)
  17. @res << fragment.to_s
  18. end
  19. def accept_list_start(am, fragment)
  20. @res << fragment.to_s
  21. end
  22. def accept_list_end(am, fragment)
  23. @res << fragment.to_s
  24. end
  25. def accept_list_item(am, fragment)
  26. @res << fragment.to_s
  27. end
  28. def accept_blank_line(am, fragment)
  29. @res << fragment.to_s
  30. end
  31. def accept_heading(am, fragment)
  32. @res << fragment.to_s
  33. end
  34. def accept_rule(am, fragment)
  35. @res << fragment.to_s
  36. end
  37. end
  38. def basic_conv(str)
  39. sm = SimpleMarkup.new
  40. mock = MockOutput.new
  41. sm.convert(str, mock)
  42. sm.content
  43. end
  44. def line_types(str, expected)
  45. p = SimpleMarkup.new
  46. mock = MockOutput.new
  47. p.convert(str, mock)
  48. assert_equal(expected, p.get_line_types.map{|type| type.to_s[0,1]}.join(''))
  49. end
  50. def line_groups(str, expected)
  51. p = SimpleMarkup.new
  52. mock = MockOutput.new
  53. block = p.convert(str, mock)
  54. if block != expected
  55. rows = (0...([expected.size, block.size].max)).collect{|i|
  56. [expected[i]||"nil", block[i]||"nil"]
  57. }
  58. printf "\n\n%35s %35s\n", "Expected", "Got"
  59. rows.each {|e,g| printf "%35s %35s\n", e.dump, g.dump }
  60. end
  61. assert_equal(expected, block)
  62. end
  63. def test_tabs
  64. str = "hello\n dave"
  65. assert_equal(str, basic_conv(str))
  66. str = "hello\n\tdave"
  67. assert_equal("hello\n dave", basic_conv(str))
  68. str = "hello\n \tdave"
  69. assert_equal("hello\n dave", basic_conv(str))
  70. str = "hello\n \tdave"
  71. assert_equal("hello\n dave", basic_conv(str))
  72. str = "hello\n \tdave"
  73. assert_equal("hello\n dave", basic_conv(str))
  74. str = "hello\n \tdave"
  75. assert_equal("hello\n dave", basic_conv(str))
  76. str = "hello\n \tdave"
  77. assert_equal("hello\n dave", basic_conv(str))
  78. str = "hello\n \tdave"
  79. assert_equal("hello\n dave", basic_conv(str))
  80. str = "hello\n \tdave"
  81. assert_equal("hello\n dave", basic_conv(str))
  82. str = "hello\n \tdave"
  83. assert_equal("hello\n dave", basic_conv(str))
  84. str = ".\t\t."
  85. assert_equal(". .", basic_conv(str))
  86. end
  87. def test_whitespace
  88. assert_equal("hello", basic_conv("hello"))
  89. assert_equal("hello", basic_conv(" hello "))
  90. assert_equal("hello", basic_conv(" \t \t hello\t\t"))
  91. assert_equal("1\n 2\n 3", basic_conv("1\n 2\n 3"))
  92. assert_equal("1\n 2\n 3", basic_conv(" 1\n 2\n 3"))
  93. assert_equal("1\n 2\n 3\n1\n 2", basic_conv("1\n 2\n 3\n1\n 2"))
  94. assert_equal("1\n 2\n 3\n1\n 2", basic_conv(" 1\n 2\n 3\n 1\n 2"))
  95. assert_equal("1\n 2\n\n 3", basic_conv(" 1\n 2\n\n 3"))
  96. end
  97. def test_types
  98. str = "now is the time"
  99. line_types(str, 'P')
  100. str = "now is the time\nfor all good men"
  101. line_types(str, 'PP')
  102. str = "now is the time\n code\nfor all good men"
  103. line_types(str, 'PVP')
  104. str = "now is the time\n code\n more code\nfor all good men"
  105. line_types(str, 'PVVP')
  106. str = "now is\n---\nthe time"
  107. line_types(str, 'PRP')
  108. str = %{\
  109. now is
  110. * l1
  111. * l2
  112. the time}
  113. line_types(str, 'PLLP')
  114. str = %{\
  115. now is
  116. * l1
  117. l1+
  118. * l2
  119. the time}
  120. line_types(str, 'PLPLP')
  121. str = %{\
  122. now is
  123. * l1
  124. * l1.1
  125. * l2
  126. the time}
  127. line_types(str, 'PLLLP')
  128. str = %{\
  129. now is
  130. * l1
  131. * l1.1
  132. text
  133. code
  134. code
  135. text
  136. * l2
  137. the time}
  138. line_types(str, 'PLLPVVBPLP')
  139. str = %{\
  140. now is
  141. 1. l1
  142. * l1.1
  143. 2. l2
  144. the time}
  145. line_types(str, 'PLLLP')
  146. str = %{\
  147. now is
  148. [cat] l1
  149. * l1.1
  150. [dog] l2
  151. the time}
  152. line_types(str, 'PLLLP')
  153. str = %{\
  154. now is
  155. [cat] l1
  156. continuation
  157. [dog] l2
  158. the time}
  159. line_types(str, 'PLPLP')
  160. end
  161. def test_groups
  162. str = "now is the time"
  163. line_groups(str, ["L0: Paragraph\nnow is the time"] )
  164. str = "now is the time\nfor all good men"
  165. line_groups(str, ["L0: Paragraph\nnow is the time for all good men"] )
  166. str = %{\
  167. now is the time
  168. code _line_ here
  169. for all good men}
  170. line_groups(str,
  171. [ "L0: Paragraph\nnow is the time",
  172. "L0: Verbatim\n code _line_ here\n",
  173. "L0: Paragraph\nfor all good men"
  174. ] )
  175. str = "now is the time\n code\n more code\nfor all good men"
  176. line_groups(str,
  177. [ "L0: Paragraph\nnow is the time",
  178. "L0: Verbatim\n code\n more code\n",
  179. "L0: Paragraph\nfor all good men"
  180. ] )
  181. str = %{\
  182. now is
  183. * l1
  184. * l2
  185. the time}
  186. line_groups(str,
  187. [ "L0: Paragraph\nnow is",
  188. "L1: ListStart\n",
  189. "L1: ListItem\nl1",
  190. "L1: ListItem\nl2",
  191. "L1: ListEnd\n",
  192. "L0: Paragraph\nthe time"
  193. ])
  194. str = %{\
  195. now is
  196. * l1
  197. l1+
  198. * l2
  199. the time}
  200. line_groups(str,
  201. [ "L0: Paragraph\nnow is",
  202. "L1: ListStart\n",
  203. "L1: ListItem\nl1 l1+",
  204. "L1: ListItem\nl2",
  205. "L1: ListEnd\n",
  206. "L0: Paragraph\nthe time"
  207. ])
  208. str = %{\
  209. now is
  210. * l1
  211. * l1.1
  212. * l2
  213. the time}
  214. line_groups(str,
  215. [ "L0: Paragraph\nnow is",
  216. "L1: ListStart\n",
  217. "L1: ListItem\nl1",
  218. "L2: ListStart\n",
  219. "L2: ListItem\nl1.1",
  220. "L2: ListEnd\n",
  221. "L1: ListItem\nl2",
  222. "L1: ListEnd\n",
  223. "L0: Paragraph\nthe time"
  224. ])
  225. str = %{\
  226. now is
  227. * l1
  228. * l1.1
  229. text
  230. code
  231. code
  232. text
  233. * l2
  234. the time}
  235. line_groups(str,
  236. [ "L0: Paragraph\nnow is",
  237. "L1: ListStart\n",
  238. "L1: ListItem\nl1",
  239. "L2: ListStart\n",
  240. "L2: ListItem\nl1.1 text",
  241. "L2: Verbatim\n code\n code\n",
  242. "L2: Paragraph\ntext",
  243. "L2: ListEnd\n",
  244. "L1: ListItem\nl2",
  245. "L1: ListEnd\n",
  246. "L0: Paragraph\nthe time"
  247. ])
  248. str = %{\
  249. now is
  250. 1. l1
  251. * l1.1
  252. 2. l2
  253. the time}
  254. line_groups(str,
  255. [ "L0: Paragraph\nnow is",
  256. "L1: ListStart\n",
  257. "L1: ListItem\nl1",
  258. "L2: ListStart\n",
  259. "L2: ListItem\nl1.1",
  260. "L2: ListEnd\n",
  261. "L1: ListItem\nl2",
  262. "L1: ListEnd\n",
  263. "L0: Paragraph\nthe time"
  264. ])
  265. str = %{\
  266. now is
  267. [cat] l1
  268. * l1.1
  269. [dog] l2
  270. the time}
  271. line_groups(str,
  272. [ "L0: Paragraph\nnow is",
  273. "L1: ListStart\n",
  274. "L1: ListItem\nl1",
  275. "L2: ListStart\n",
  276. "L2: ListItem\nl1.1",
  277. "L2: ListEnd\n",
  278. "L1: ListItem\nl2",
  279. "L1: ListEnd\n",
  280. "L0: Paragraph\nthe time"
  281. ])
  282. str = %{\
  283. now is
  284. [cat] l1
  285. continuation
  286. [dog] l2
  287. the time}
  288. line_groups(str,
  289. [ "L0: Paragraph\nnow is",
  290. "L1: ListStart\n",
  291. "L1: ListItem\nl1 continuation",
  292. "L1: ListItem\nl2",
  293. "L1: ListEnd\n",
  294. "L0: Paragraph\nthe time"
  295. ])
  296. end
  297. def test_verbatim_merge
  298. str = %{\
  299. now is
  300. code
  301. the time}
  302. line_groups(str,
  303. [ "L0: Paragraph\nnow is",
  304. "L0: Verbatim\n code\n",
  305. "L0: Paragraph\nthe time"
  306. ])
  307. str = %{\
  308. now is
  309. code
  310. code1
  311. the time}
  312. line_groups(str,
  313. [ "L0: Paragraph\nnow is",
  314. "L0: Verbatim\n code\n code1\n",
  315. "L0: Paragraph\nthe time"
  316. ])
  317. str = %{\
  318. now is
  319. code
  320. code1
  321. the time}
  322. line_groups(str,
  323. [ "L0: Paragraph\nnow is",
  324. "L0: Verbatim\n code\n\n code1\n",
  325. "L0: Paragraph\nthe time"
  326. ])
  327. str = %{\
  328. now is
  329. code
  330. code1
  331. the time}
  332. line_groups(str,
  333. [ "L0: Paragraph\nnow is",
  334. "L0: Verbatim\n code\n\n code1\n",
  335. "L0: Paragraph\nthe time"
  336. ])
  337. str = %{\
  338. now is
  339. code
  340. code1
  341. code2
  342. the time}
  343. line_groups(str,
  344. [ "L0: Paragraph\nnow is",
  345. "L0: Verbatim\n code\n\n code1\n\n code2\n",
  346. "L0: Paragraph\nthe time"
  347. ])
  348. # Folds multiple blank lines
  349. str = %{\
  350. now is
  351. code
  352. code1
  353. the time}
  354. line_groups(str,
  355. [ "L0: Paragraph\nnow is",
  356. "L0: Verbatim\n code\n\n code1\n",
  357. "L0: Paragraph\nthe time"
  358. ])
  359. end
  360. def test_list_split
  361. str = %{\
  362. now is
  363. * l1
  364. 1. n1
  365. 2. n2
  366. * l2
  367. the time}
  368. line_groups(str,
  369. [ "L0: Paragraph\nnow is",
  370. "L1: ListStart\n",
  371. "L1: ListItem\nl1",
  372. "L1: ListEnd\n",
  373. "L1: ListStart\n",
  374. "L1: ListItem\nn1",
  375. "L1: ListItem\nn2",
  376. "L1: ListEnd\n",
  377. "L1: ListStart\n",
  378. "L1: ListItem\nl2",
  379. "L1: ListEnd\n",
  380. "L0: Paragraph\nthe time"
  381. ])
  382. end
  383. def test_headings
  384. str = "= heading one"
  385. line_groups(str,
  386. [ "L0: Heading\nheading one"
  387. ])
  388. str = "=== heading three"
  389. line_groups(str,
  390. [ "L0: Heading\nheading three"
  391. ])
  392. str = "text\n === heading three"
  393. line_groups(str,
  394. [ "L0: Paragraph\ntext",
  395. "L0: Verbatim\n === heading three\n"
  396. ])
  397. str = "text\n code\n === heading three"
  398. line_groups(str,
  399. [ "L0: Paragraph\ntext",
  400. "L0: Verbatim\n code\n === heading three\n"
  401. ])
  402. str = "text\n code\n=== heading three"
  403. line_groups(str,
  404. [ "L0: Paragraph\ntext",
  405. "L0: Verbatim\n code\n",
  406. "L0: Heading\nheading three"
  407. ])
  408. end
  409. end