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