PageRenderTime 60ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/TDDBC_Yokohama2_PCUnit/Ruby/doc/ruby/ruby-1.9.2/test/test_prettyprint.rb

https://bitbucket.org/aetos/tddbc_yokohama2
Ruby | 519 lines | 471 code | 48 blank | 0 comment | 14 complexity | d566dbf709089795a3714c5b1eb3e67b MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, AGPL-3.0, 0BSD, Unlicense
  1. require 'prettyprint'
  2. require 'test/unit'
  3. module PrettyPrintTest
  4. class WadlerExample < Test::Unit::TestCase # :nodoc:
  5. def setup
  6. @tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"),
  7. Tree.new("dd")),
  8. Tree.new("eee"),
  9. Tree.new("ffff", Tree.new("gg"),
  10. Tree.new("hhh"),
  11. Tree.new("ii")))
  12. end
  13. def hello(width)
  14. PrettyPrint.format('', width) {|hello|
  15. hello.group {
  16. hello.group {
  17. hello.group {
  18. hello.group {
  19. hello.text 'hello'
  20. hello.breakable; hello.text 'a'
  21. }
  22. hello.breakable; hello.text 'b'
  23. }
  24. hello.breakable; hello.text 'c'
  25. }
  26. hello.breakable; hello.text 'd'
  27. }
  28. }
  29. end
  30. def test_hello_00_06
  31. expected = <<'End'.chomp
  32. hello
  33. a
  34. b
  35. c
  36. d
  37. End
  38. assert_equal(expected, hello(0))
  39. assert_equal(expected, hello(6))
  40. end
  41. def test_hello_07_08
  42. expected = <<'End'.chomp
  43. hello a
  44. b
  45. c
  46. d
  47. End
  48. assert_equal(expected, hello(7))
  49. assert_equal(expected, hello(8))
  50. end
  51. def test_hello_09_10
  52. expected = <<'End'.chomp
  53. hello a b
  54. c
  55. d
  56. End
  57. out = hello(9); assert_equal(expected, out)
  58. out = hello(10); assert_equal(expected, out)
  59. end
  60. def test_hello_11_12
  61. expected = <<'End'.chomp
  62. hello a b c
  63. d
  64. End
  65. assert_equal(expected, hello(11))
  66. assert_equal(expected, hello(12))
  67. end
  68. def test_hello_13
  69. expected = <<'End'.chomp
  70. hello a b c d
  71. End
  72. assert_equal(expected, hello(13))
  73. end
  74. def tree(width)
  75. PrettyPrint.format('', width) {|q| @tree.show(q)}
  76. end
  77. def test_tree_00_19
  78. expected = <<'End'.chomp
  79. aaaa[bbbbb[ccc,
  80. dd],
  81. eee,
  82. ffff[gg,
  83. hhh,
  84. ii]]
  85. End
  86. assert_equal(expected, tree(0))
  87. assert_equal(expected, tree(19))
  88. end
  89. def test_tree_20_22
  90. expected = <<'End'.chomp
  91. aaaa[bbbbb[ccc, dd],
  92. eee,
  93. ffff[gg,
  94. hhh,
  95. ii]]
  96. End
  97. assert_equal(expected, tree(20))
  98. assert_equal(expected, tree(22))
  99. end
  100. def test_tree_23_43
  101. expected = <<'End'.chomp
  102. aaaa[bbbbb[ccc, dd],
  103. eee,
  104. ffff[gg, hhh, ii]]
  105. End
  106. assert_equal(expected, tree(23))
  107. assert_equal(expected, tree(43))
  108. end
  109. def test_tree_44
  110. assert_equal(<<'End'.chomp, tree(44))
  111. aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]]
  112. End
  113. end
  114. def tree_alt(width)
  115. PrettyPrint.format('', width) {|q| @tree.altshow(q)}
  116. end
  117. def test_tree_alt_00_18
  118. expected = <<'End'.chomp
  119. aaaa[
  120. bbbbb[
  121. ccc,
  122. dd
  123. ],
  124. eee,
  125. ffff[
  126. gg,
  127. hhh,
  128. ii
  129. ]
  130. ]
  131. End
  132. assert_equal(expected, tree_alt(0))
  133. assert_equal(expected, tree_alt(18))
  134. end
  135. def test_tree_alt_19_20
  136. expected = <<'End'.chomp
  137. aaaa[
  138. bbbbb[ ccc, dd ],
  139. eee,
  140. ffff[
  141. gg,
  142. hhh,
  143. ii
  144. ]
  145. ]
  146. End
  147. assert_equal(expected, tree_alt(19))
  148. assert_equal(expected, tree_alt(20))
  149. end
  150. def test_tree_alt_20_49
  151. expected = <<'End'.chomp
  152. aaaa[
  153. bbbbb[ ccc, dd ],
  154. eee,
  155. ffff[ gg, hhh, ii ]
  156. ]
  157. End
  158. assert_equal(expected, tree_alt(21))
  159. assert_equal(expected, tree_alt(49))
  160. end
  161. def test_tree_alt_50
  162. expected = <<'End'.chomp
  163. aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ]
  164. End
  165. assert_equal(expected, tree_alt(50))
  166. end
  167. class Tree # :nodoc:
  168. def initialize(string, *children)
  169. @string = string
  170. @children = children
  171. end
  172. def show(q)
  173. q.group {
  174. q.text @string
  175. q.nest(@string.length) {
  176. unless @children.empty?
  177. q.text '['
  178. q.nest(1) {
  179. first = true
  180. @children.each {|t|
  181. if first
  182. first = false
  183. else
  184. q.text ','
  185. q.breakable
  186. end
  187. t.show(q)
  188. }
  189. }
  190. q.text ']'
  191. end
  192. }
  193. }
  194. end
  195. def altshow(q)
  196. q.group {
  197. q.text @string
  198. unless @children.empty?
  199. q.text '['
  200. q.nest(2) {
  201. q.breakable
  202. first = true
  203. @children.each {|t|
  204. if first
  205. first = false
  206. else
  207. q.text ','
  208. q.breakable
  209. end
  210. t.altshow(q)
  211. }
  212. }
  213. q.breakable
  214. q.text ']'
  215. end
  216. }
  217. end
  218. end
  219. end
  220. class StrictPrettyExample < Test::Unit::TestCase # :nodoc:
  221. def prog(width)
  222. PrettyPrint.format('', width) {|q|
  223. q.group {
  224. q.group {q.nest(2) {
  225. q.text "if"; q.breakable;
  226. q.group {
  227. q.nest(2) {
  228. q.group {q.text "a"; q.breakable; q.text "=="}
  229. q.breakable; q.text "b"}}}}
  230. q.breakable
  231. q.group {q.nest(2) {
  232. q.text "then"; q.breakable;
  233. q.group {
  234. q.nest(2) {
  235. q.group {q.text "a"; q.breakable; q.text "<<"}
  236. q.breakable; q.text "2"}}}}
  237. q.breakable
  238. q.group {q.nest(2) {
  239. q.text "else"; q.breakable;
  240. q.group {
  241. q.nest(2) {
  242. q.group {q.text "a"; q.breakable; q.text "+"}
  243. q.breakable; q.text "b"}}}}}
  244. }
  245. end
  246. def test_00_04
  247. expected = <<'End'.chomp
  248. if
  249. a
  250. ==
  251. b
  252. then
  253. a
  254. <<
  255. 2
  256. else
  257. a
  258. +
  259. b
  260. End
  261. assert_equal(expected, prog(0))
  262. assert_equal(expected, prog(4))
  263. end
  264. def test_05
  265. expected = <<'End'.chomp
  266. if
  267. a
  268. ==
  269. b
  270. then
  271. a
  272. <<
  273. 2
  274. else
  275. a +
  276. b
  277. End
  278. assert_equal(expected, prog(5))
  279. end
  280. def test_06
  281. expected = <<'End'.chomp
  282. if
  283. a ==
  284. b
  285. then
  286. a <<
  287. 2
  288. else
  289. a +
  290. b
  291. End
  292. assert_equal(expected, prog(6))
  293. end
  294. def test_07
  295. expected = <<'End'.chomp
  296. if
  297. a ==
  298. b
  299. then
  300. a <<
  301. 2
  302. else
  303. a + b
  304. End
  305. assert_equal(expected, prog(7))
  306. end
  307. def test_08
  308. expected = <<'End'.chomp
  309. if
  310. a == b
  311. then
  312. a << 2
  313. else
  314. a + b
  315. End
  316. assert_equal(expected, prog(8))
  317. end
  318. def test_09
  319. expected = <<'End'.chomp
  320. if a == b
  321. then
  322. a << 2
  323. else
  324. a + b
  325. End
  326. assert_equal(expected, prog(9))
  327. end
  328. def test_10
  329. expected = <<'End'.chomp
  330. if a == b
  331. then
  332. a << 2
  333. else a + b
  334. End
  335. assert_equal(expected, prog(10))
  336. end
  337. def test_11_31
  338. expected = <<'End'.chomp
  339. if a == b
  340. then a << 2
  341. else a + b
  342. End
  343. assert_equal(expected, prog(11))
  344. assert_equal(expected, prog(15))
  345. assert_equal(expected, prog(31))
  346. end
  347. def test_32
  348. expected = <<'End'.chomp
  349. if a == b then a << 2 else a + b
  350. End
  351. assert_equal(expected, prog(32))
  352. end
  353. end
  354. class TailGroup < Test::Unit::TestCase # :nodoc:
  355. def test_1
  356. out = PrettyPrint.format('', 10) {|q|
  357. q.group {
  358. q.group {
  359. q.text "abc"
  360. q.breakable
  361. q.text "def"
  362. }
  363. q.group {
  364. q.text "ghi"
  365. q.breakable
  366. q.text "jkl"
  367. }
  368. }
  369. }
  370. assert_equal("abc defghi\njkl", out)
  371. end
  372. end
  373. class NonString < Test::Unit::TestCase # :nodoc:
  374. def format(width)
  375. PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q|
  376. q.text(3, 3)
  377. q.breakable(1, 1)
  378. q.text(3, 3)
  379. }
  380. end
  381. def test_6
  382. assert_equal([3, "newline", "0 spaces", 3], format(6))
  383. end
  384. def test_7
  385. assert_equal([3, 1, 3], format(7))
  386. end
  387. end
  388. class Fill < Test::Unit::TestCase # :nodoc:
  389. def format(width)
  390. PrettyPrint.format('', width) {|q|
  391. q.group {
  392. q.text 'abc'
  393. q.fill_breakable
  394. q.text 'def'
  395. q.fill_breakable
  396. q.text 'ghi'
  397. q.fill_breakable
  398. q.text 'jkl'
  399. q.fill_breakable
  400. q.text 'mno'
  401. q.fill_breakable
  402. q.text 'pqr'
  403. q.fill_breakable
  404. q.text 'stu'
  405. }
  406. }
  407. end
  408. def test_00_06
  409. expected = <<'End'.chomp
  410. abc
  411. def
  412. ghi
  413. jkl
  414. mno
  415. pqr
  416. stu
  417. End
  418. assert_equal(expected, format(0))
  419. assert_equal(expected, format(6))
  420. end
  421. def test_07_10
  422. expected = <<'End'.chomp
  423. abc def
  424. ghi jkl
  425. mno pqr
  426. stu
  427. End
  428. assert_equal(expected, format(7))
  429. assert_equal(expected, format(10))
  430. end
  431. def test_11_14
  432. expected = <<'End'.chomp
  433. abc def ghi
  434. jkl mno pqr
  435. stu
  436. End
  437. assert_equal(expected, format(11))
  438. assert_equal(expected, format(14))
  439. end
  440. def test_15_18
  441. expected = <<'End'.chomp
  442. abc def ghi jkl
  443. mno pqr stu
  444. End
  445. assert_equal(expected, format(15))
  446. assert_equal(expected, format(18))
  447. end
  448. def test_19_22
  449. expected = <<'End'.chomp
  450. abc def ghi jkl mno
  451. pqr stu
  452. End
  453. assert_equal(expected, format(19))
  454. assert_equal(expected, format(22))
  455. end
  456. def test_23_26
  457. expected = <<'End'.chomp
  458. abc def ghi jkl mno pqr
  459. stu
  460. End
  461. assert_equal(expected, format(23))
  462. assert_equal(expected, format(26))
  463. end
  464. def test_27
  465. expected = <<'End'.chomp
  466. abc def ghi jkl mno pqr stu
  467. End
  468. assert_equal(expected, format(27))
  469. end
  470. end
  471. end