From 29f73c43a5dde61a8ddea3dd31af7d806fc51c2b Mon Sep 17 00:00:00 2001 From: akr Date: Sat, 14 Feb 2009 23:36:45 +0000 Subject: * test/test_pp.rb: extract from lib/pp.rb. * test/test_prettyprint.rb: extract from lib/prettyprint.rb. * test/test_tsort.rb: extract from lib/tsort.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22321 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 + lib/pp.rb | 183 ----------------- lib/prettyprint.rb | 517 ---------------------------------------------- lib/tsort.rb | 47 ----- test/test_pp.rb | 184 ++++++++++++++++- test/test_prettyprint.rb | 519 ++++++++++++++++++++++++++++++++++++++++++++++- test/test_tsort.rb | 48 ++++- 7 files changed, 747 insertions(+), 759 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1ed25423b1..a6f05b3c6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sun Feb 15 08:35:33 2009 Tanaka Akira + + * test/test_pp.rb: extract from lib/pp.rb. + + * test/test_prettyprint.rb: extract from lib/prettyprint.rb. + + * test/test_tsort.rb: extract from lib/tsort.rb. + Sun Feb 15 06:34:22 2009 Tanaka Akira * lib/time.rb (Time.parse): raise ArgumentError if Date._parse don't diff --git a/lib/pp.rb b/lib/pp.rb index 41f51b0046..d7425cd5f5 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -515,186 +515,3 @@ end end } } - -# :enddoc: -if __FILE__ == $0 - require 'test/unit' - - class PPTest < Test::Unit::TestCase - def test_list0123_12 - assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12)) - end - - def test_list0123_11 - assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11)) - end - - OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) - def test_struct_override_members # [ruby-core:7865] - a = OverriddenStruct.new(1,2) - assert_equal("#\n", PP.pp(a, '')) - end - - def test_redefined_method - o = "" - def o.method - end - assert_equal(%(""\n), PP.pp(o, "")) - end - end - - class HasInspect - def initialize(a) - @a = a - end - - def inspect - return "" - end - end - - class HasPrettyPrint - def initialize(a) - @a = a - end - - def pretty_print(q) - q.text "" - end - end - - class HasBoth - def initialize(a) - @a = a - end - - def inspect - return "" - end - - def pretty_print(q) - q.text "" - end - end - - class PrettyPrintInspect < HasPrettyPrint - alias inspect pretty_print_inspect - end - - class PrettyPrintInspectWithoutPrettyPrint - alias inspect pretty_print_inspect - end - - class PPInspectTest < Test::Unit::TestCase - def test_hasinspect - a = HasInspect.new(1) - assert_equal("\n", PP.pp(a, '')) - end - - def test_hasprettyprint - a = HasPrettyPrint.new(1) - assert_equal("\n", PP.pp(a, '')) - end - - def test_hasboth - a = HasBoth.new(1) - assert_equal("\n", PP.pp(a, '')) - end - - def test_pretty_print_inspect - a = PrettyPrintInspect.new(1) - assert_equal("", a.inspect) - a = PrettyPrintInspectWithoutPrettyPrint.new - assert_raise(RuntimeError) { a.inspect } - end - - def test_proc - a = proc {1} - assert_equal("#{a.inspect}\n", PP.pp(a, '')) - end - - def test_to_s_with_iv - a = Object.new - def a.to_s() "aaa" end - a.instance_eval { @a = nil } - result = PP.pp(a, '') - assert_equal("#{a.inspect}\n", result) - assert_match(/\A#\n\z/m, result) - a = 1.0 - a.instance_eval { @a = nil } - result = PP.pp(a, '') - assert_equal("#{a.inspect}\n", result) - end - - def test_to_s_without_iv - a = Object.new - def a.to_s() "aaa" end - result = PP.pp(a, '') - assert_equal("#{a.inspect}\n", result) - assert_equal("aaa\n", result) - end - end - - class PPCycleTest < Test::Unit::TestCase - def test_array - a = [] - a << a - assert_equal("[[...]]\n", PP.pp(a, '')) - assert_equal("#{a.inspect}\n", PP.pp(a, '')) - end - - def test_hash - a = {} - a[0] = a - assert_equal("{0=>{...}}\n", PP.pp(a, '')) - assert_equal("#{a.inspect}\n", PP.pp(a, '')) - end - - S = Struct.new("S", :a, :b) - def test_struct - a = S.new(1,2) - a.b = a - assert_equal("#>\n", PP.pp(a, '')) - assert_equal("#{a.inspect}\n", PP.pp(a, '')) - end - - def test_object - a = Object.new - a.instance_eval {@a = a} - assert_equal(a.inspect + "\n", PP.pp(a, '')) - end - - def test_anonymous - a = Class.new.new - assert_equal(a.inspect + "\n", PP.pp(a, '')) - end - - def test_withinspect - a = [] - a << HasInspect.new(a) - assert_equal("[]\n", PP.pp(a, '')) - assert_equal("#{a.inspect}\n", PP.pp(a, '')) - end - - def test_share_nil - begin - PP.sharing_detection = true - a = [nil, nil] - assert_equal("[nil, nil]\n", PP.pp(a, '')) - ensure - PP.sharing_detection = false - end - end - end - - class PPSingleLineTest < Test::Unit::TestCase - def test_hash - assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699] - assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, '')) - end - end -end diff --git a/lib/prettyprint.rb b/lib/prettyprint.rb index 911565392d..2c1fff3258 100644 --- a/lib/prettyprint.rb +++ b/lib/prettyprint.rb @@ -375,520 +375,3 @@ class PrettyPrint end end end - -if __FILE__ == $0 - require 'test/unit' - - class WadlerExample < Test::Unit::TestCase # :nodoc: - def setup - @tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"), - Tree.new("dd")), - Tree.new("eee"), - Tree.new("ffff", Tree.new("gg"), - Tree.new("hhh"), - Tree.new("ii"))) - end - - def hello(width) - PrettyPrint.format('', width) {|hello| - hello.group { - hello.group { - hello.group { - hello.group { - hello.text 'hello' - hello.breakable; hello.text 'a' - } - hello.breakable; hello.text 'b' - } - hello.breakable; hello.text 'c' - } - hello.breakable; hello.text 'd' - } - } - end - - def test_hello_00_06 - expected = <<'End'.chomp -hello -a -b -c -d -End - assert_equal(expected, hello(0)) - assert_equal(expected, hello(6)) - end - - def test_hello_07_08 - expected = <<'End'.chomp -hello a -b -c -d -End - assert_equal(expected, hello(7)) - assert_equal(expected, hello(8)) - end - - def test_hello_09_10 - expected = <<'End'.chomp -hello a b -c -d -End - out = hello(9); assert_equal(expected, out) - out = hello(10); assert_equal(expected, out) - end - - def test_hello_11_12 - expected = <<'End'.chomp -hello a b c -d -End - assert_equal(expected, hello(11)) - assert_equal(expected, hello(12)) - end - - def test_hello_13 - expected = <<'End'.chomp -hello a b c d -End - assert_equal(expected, hello(13)) - end - - def tree(width) - PrettyPrint.format('', width) {|q| @tree.show(q)} - end - - def test_tree_00_19 - expected = <<'End'.chomp -aaaa[bbbbb[ccc, - dd], - eee, - ffff[gg, - hhh, - ii]] -End - assert_equal(expected, tree(0)) - assert_equal(expected, tree(19)) - end - - def test_tree_20_22 - expected = <<'End'.chomp -aaaa[bbbbb[ccc, dd], - eee, - ffff[gg, - hhh, - ii]] -End - assert_equal(expected, tree(20)) - assert_equal(expected, tree(22)) - end - - def test_tree_23_43 - expected = <<'End'.chomp -aaaa[bbbbb[ccc, dd], - eee, - ffff[gg, hhh, ii]] -End - assert_equal(expected, tree(23)) - assert_equal(expected, tree(43)) - end - - def test_tree_44 - assert_equal(<<'End'.chomp, tree(44)) -aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]] -End - end - - def tree_alt(width) - PrettyPrint.format('', width) {|q| @tree.altshow(q)} - end - - def test_tree_alt_00_18 - expected = <<'End'.chomp -aaaa[ - bbbbb[ - ccc, - dd - ], - eee, - ffff[ - gg, - hhh, - ii - ] -] -End - assert_equal(expected, tree_alt(0)) - assert_equal(expected, tree_alt(18)) - end - - def test_tree_alt_19_20 - expected = <<'End'.chomp -aaaa[ - bbbbb[ ccc, dd ], - eee, - ffff[ - gg, - hhh, - ii - ] -] -End - assert_equal(expected, tree_alt(19)) - assert_equal(expected, tree_alt(20)) - end - - def test_tree_alt_20_49 - expected = <<'End'.chomp -aaaa[ - bbbbb[ ccc, dd ], - eee, - ffff[ gg, hhh, ii ] -] -End - assert_equal(expected, tree_alt(21)) - assert_equal(expected, tree_alt(49)) - end - - def test_tree_alt_50 - expected = <<'End'.chomp -aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ] -End - assert_equal(expected, tree_alt(50)) - end - - class Tree # :nodoc: - def initialize(string, *children) - @string = string - @children = children - end - - def show(q) - q.group { - q.text @string - q.nest(@string.length) { - unless @children.empty? - q.text '[' - q.nest(1) { - first = true - @children.each {|t| - if first - first = false - else - q.text ',' - q.breakable - end - t.show(q) - } - } - q.text ']' - end - } - } - end - - def altshow(q) - q.group { - q.text @string - unless @children.empty? - q.text '[' - q.nest(2) { - q.breakable - first = true - @children.each {|t| - if first - first = false - else - q.text ',' - q.breakable - end - t.altshow(q) - } - } - q.breakable - q.text ']' - end - } - end - - end - end - - class StrictPrettyExample < Test::Unit::TestCase # :nodoc: - def prog(width) - PrettyPrint.format('', width) {|q| - q.group { - q.group {q.nest(2) { - q.text "if"; q.breakable; - q.group { - q.nest(2) { - q.group {q.text "a"; q.breakable; q.text "=="} - q.breakable; q.text "b"}}}} - q.breakable - q.group {q.nest(2) { - q.text "then"; q.breakable; - q.group { - q.nest(2) { - q.group {q.text "a"; q.breakable; q.text "<<"} - q.breakable; q.text "2"}}}} - q.breakable - q.group {q.nest(2) { - q.text "else"; q.breakable; - q.group { - q.nest(2) { - q.group {q.text "a"; q.breakable; q.text "+"} - q.breakable; q.text "b"}}}}} - } - end - - def test_00_04 - expected = <<'End'.chomp -if - a - == - b -then - a - << - 2 -else - a - + - b -End - assert_equal(expected, prog(0)) - assert_equal(expected, prog(4)) - end - - def test_05 - expected = <<'End'.chomp -if - a - == - b -then - a - << - 2 -else - a + - b -End - assert_equal(expected, prog(5)) - end - - def test_06 - expected = <<'End'.chomp -if - a == - b -then - a << - 2 -else - a + - b -End - assert_equal(expected, prog(6)) - end - - def test_07 - expected = <<'End'.chomp -if - a == - b -then - a << - 2 -else - a + b -End - assert_equal(expected, prog(7)) - end - - def test_08 - expected = <<'End'.chomp -if - a == b -then - a << 2 -else - a + b -End - assert_equal(expected, prog(8)) - end - - def test_09 - expected = <<'End'.chomp -if a == b -then - a << 2 -else - a + b -End - assert_equal(expected, prog(9)) - end - - def test_10 - expected = <<'End'.chomp -if a == b -then - a << 2 -else a + b -End - assert_equal(expected, prog(10)) - end - - def test_11_31 - expected = <<'End'.chomp -if a == b -then a << 2 -else a + b -End - assert_equal(expected, prog(11)) - assert_equal(expected, prog(15)) - assert_equal(expected, prog(31)) - end - - def test_32 - expected = <<'End'.chomp -if a == b then a << 2 else a + b -End - assert_equal(expected, prog(32)) - end - - end - - class TailGroup < Test::Unit::TestCase # :nodoc: - def test_1 - out = PrettyPrint.format('', 10) {|q| - q.group { - q.group { - q.text "abc" - q.breakable - q.text "def" - } - q.group { - q.text "ghi" - q.breakable - q.text "jkl" - } - } - } - assert_equal("abc defghi\njkl", out) - end - end - - class NonString < Test::Unit::TestCase # :nodoc: - def format(width) - PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q| - q.text(3, 3) - q.breakable(1, 1) - q.text(3, 3) - } - end - - def test_6 - assert_equal([3, "newline", "0 spaces", 3], format(6)) - end - - def test_7 - assert_equal([3, 1, 3], format(7)) - end - - end - - class Fill < Test::Unit::TestCase # :nodoc: - def format(width) - PrettyPrint.format('', width) {|q| - q.group { - q.text 'abc' - q.fill_breakable - q.text 'def' - q.fill_breakable - q.text 'ghi' - q.fill_breakable - q.text 'jkl' - q.fill_breakable - q.text 'mno' - q.fill_breakable - q.text 'pqr' - q.fill_breakable - q.text 'stu' - } - } - end - - def test_00_06 - expected = <<'End'.chomp -abc -def -ghi -jkl -mno -pqr -stu -End - assert_equal(expected, format(0)) - assert_equal(expected, format(6)) - end - - def test_07_10 - expected = <<'End'.chomp -abc def -ghi jkl -mno pqr -stu -End - assert_equal(expected, format(7)) - assert_equal(expected, format(10)) - end - - def test_11_14 - expected = <<'End'.chomp -abc def ghi -jkl mno pqr -stu -End - assert_equal(expected, format(11)) - assert_equal(expected, format(14)) - end - - def test_15_18 - expected = <<'End'.chomp -abc def ghi jkl -mno pqr stu -End - assert_equal(expected, format(15)) - assert_equal(expected, format(18)) - end - - def test_19_22 - expected = <<'End'.chomp -abc def ghi jkl mno -pqr stu -End - assert_equal(expected, format(19)) - assert_equal(expected, format(22)) - end - - def test_23_26 - expected = <<'End'.chomp -abc def ghi jkl mno pqr -stu -End - assert_equal(expected, format(23)) - assert_equal(expected, format(26)) - end - - def test_27 - expected = <<'End'.chomp -abc def ghi jkl mno pqr stu -End - assert_equal(expected, format(27)) - end - - end -end diff --git a/lib/tsort.rb b/lib/tsort.rb index 9fc4feadcd..aa562ad1c5 100644 --- a/lib/tsort.rb +++ b/lib/tsort.rb @@ -241,50 +241,3 @@ module TSort raise NotImplementedError.new end end - -if __FILE__ == $0 - require 'test/unit' - - class TSortHash < Hash # :nodoc: - include TSort - alias tsort_each_node each_key - def tsort_each_child(node, &block) - fetch(node).each(&block) - end - end - - class TSortArray < Array # :nodoc: - include TSort - alias tsort_each_node each_index - def tsort_each_child(node, &block) - fetch(node).each(&block) - end - end - - class TSortTest < Test::Unit::TestCase # :nodoc: - def test_dag - h = TSortHash[{1=>[2, 3], 2=>[3], 3=>[]}] - assert_equal([3, 2, 1], h.tsort) - assert_equal([[3], [2], [1]], h.strongly_connected_components) - end - - def test_cycle - h = TSortHash[{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}] - assert_equal([[4], [2, 3], [1]], - h.strongly_connected_components.map {|nodes| nodes.sort}) - assert_raise(TSort::Cyclic) { h.tsort } - end - - def test_array - a = TSortArray[[1], [0], [0], [2]] - assert_equal([[0, 1], [2], [3]], - a.strongly_connected_components.map {|nodes| nodes.sort}) - - a = TSortArray[[], [0]] - assert_equal([[0], [1]], - a.strongly_connected_components.map {|nodes| nodes.sort}) - end - end - -end - diff --git a/test/test_pp.rb b/test/test_pp.rb index 981fedd98f..1809770049 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -1,4 +1,180 @@ -require 'pathname' -require Pathname.new(__FILE__).dirname.join('inlinetest.rb') -target = __FILE__[/test_(.*\.rb)$/, 1] -InlineTest.loadtest(target) +require 'pp' +require 'test/unit' + +class PPTest < Test::Unit::TestCase + def test_list0123_12 + assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12)) + end + + def test_list0123_11 + assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11)) + end + + OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) + def test_struct_override_members # [ruby-core:7865] + a = OverriddenStruct.new(1,2) + assert_equal("#\n", PP.pp(a, '')) + end + + def test_redefined_method + o = "" + def o.method + end + assert_equal(%(""\n), PP.pp(o, "")) + end +end + +class HasInspect + def initialize(a) + @a = a + end + + def inspect + return "" + end +end + +class HasPrettyPrint + def initialize(a) + @a = a + end + + def pretty_print(q) + q.text "" + end +end + +class HasBoth + def initialize(a) + @a = a + end + + def inspect + return "" + end + + def pretty_print(q) + q.text "" + end +end + +class PrettyPrintInspect < HasPrettyPrint + alias inspect pretty_print_inspect +end + +class PrettyPrintInspectWithoutPrettyPrint + alias inspect pretty_print_inspect +end + +class PPInspectTest < Test::Unit::TestCase + def test_hasinspect + a = HasInspect.new(1) + assert_equal("\n", PP.pp(a, '')) + end + + def test_hasprettyprint + a = HasPrettyPrint.new(1) + assert_equal("\n", PP.pp(a, '')) + end + + def test_hasboth + a = HasBoth.new(1) + assert_equal("\n", PP.pp(a, '')) + end + + def test_pretty_print_inspect + a = PrettyPrintInspect.new(1) + assert_equal("", a.inspect) + a = PrettyPrintInspectWithoutPrettyPrint.new + assert_raise(RuntimeError) { a.inspect } + end + + def test_proc + a = proc {1} + assert_equal("#{a.inspect}\n", PP.pp(a, '')) + end + + def test_to_s_with_iv + a = Object.new + def a.to_s() "aaa" end + a.instance_eval { @a = nil } + result = PP.pp(a, '') + assert_equal("#{a.inspect}\n", result) + assert_match(/\A#\n\z/m, result) + a = 1.0 + a.instance_eval { @a = nil } + result = PP.pp(a, '') + assert_equal("#{a.inspect}\n", result) + end + + def test_to_s_without_iv + a = Object.new + def a.to_s() "aaa" end + result = PP.pp(a, '') + assert_equal("#{a.inspect}\n", result) + assert_equal("aaa\n", result) + end +end + +class PPCycleTest < Test::Unit::TestCase + def test_array + a = [] + a << a + assert_equal("[[...]]\n", PP.pp(a, '')) + assert_equal("#{a.inspect}\n", PP.pp(a, '')) + end + + def test_hash + a = {} + a[0] = a + assert_equal("{0=>{...}}\n", PP.pp(a, '')) + assert_equal("#{a.inspect}\n", PP.pp(a, '')) + end + + S = Struct.new("S", :a, :b) + def test_struct + a = S.new(1,2) + a.b = a + assert_equal("#>\n", PP.pp(a, '')) + assert_equal("#{a.inspect}\n", PP.pp(a, '')) + end + + def test_object + a = Object.new + a.instance_eval {@a = a} + assert_equal(a.inspect + "\n", PP.pp(a, '')) + end + + def test_anonymous + a = Class.new.new + assert_equal(a.inspect + "\n", PP.pp(a, '')) + end + + def test_withinspect + a = [] + a << HasInspect.new(a) + assert_equal("[]\n", PP.pp(a, '')) + assert_equal("#{a.inspect}\n", PP.pp(a, '')) + end + + def test_share_nil + begin + PP.sharing_detection = true + a = [nil, nil] + assert_equal("[nil, nil]\n", PP.pp(a, '')) + ensure + PP.sharing_detection = false + end + end +end + +class PPSingleLineTest < Test::Unit::TestCase + def test_hash + assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699] + assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, '')) + end +end diff --git a/test/test_prettyprint.rb b/test/test_prettyprint.rb index 981fedd98f..42178e7d03 100644 --- a/test/test_prettyprint.rb +++ b/test/test_prettyprint.rb @@ -1,4 +1,515 @@ -require 'pathname' -require Pathname.new(__FILE__).dirname.join('inlinetest.rb') -target = __FILE__[/test_(.*\.rb)$/, 1] -InlineTest.loadtest(target) +require 'prettyprint' +require 'test/unit' + +class WadlerExample < Test::Unit::TestCase # :nodoc: + def setup + @tree = Tree.new("aaaa", Tree.new("bbbbb", Tree.new("ccc"), + Tree.new("dd")), + Tree.new("eee"), + Tree.new("ffff", Tree.new("gg"), + Tree.new("hhh"), + Tree.new("ii"))) + end + + def hello(width) + PrettyPrint.format('', width) {|hello| + hello.group { + hello.group { + hello.group { + hello.group { + hello.text 'hello' + hello.breakable; hello.text 'a' + } + hello.breakable; hello.text 'b' + } + hello.breakable; hello.text 'c' + } + hello.breakable; hello.text 'd' + } + } + end + + def test_hello_00_06 + expected = <<'End'.chomp +hello +a +b +c +d +End + assert_equal(expected, hello(0)) + assert_equal(expected, hello(6)) + end + + def test_hello_07_08 + expected = <<'End'.chomp +hello a +b +c +d +End + assert_equal(expected, hello(7)) + assert_equal(expected, hello(8)) + end + + def test_hello_09_10 + expected = <<'End'.chomp +hello a b +c +d +End + out = hello(9); assert_equal(expected, out) + out = hello(10); assert_equal(expected, out) + end + + def test_hello_11_12 + expected = <<'End'.chomp +hello a b c +d +End + assert_equal(expected, hello(11)) + assert_equal(expected, hello(12)) + end + + def test_hello_13 + expected = <<'End'.chomp +hello a b c d +End + assert_equal(expected, hello(13)) + end + + def tree(width) + PrettyPrint.format('', width) {|q| @tree.show(q)} + end + + def test_tree_00_19 + expected = <<'End'.chomp +aaaa[bbbbb[ccc, + dd], + eee, + ffff[gg, + hhh, + ii]] +End + assert_equal(expected, tree(0)) + assert_equal(expected, tree(19)) + end + + def test_tree_20_22 + expected = <<'End'.chomp +aaaa[bbbbb[ccc, dd], + eee, + ffff[gg, + hhh, + ii]] +End + assert_equal(expected, tree(20)) + assert_equal(expected, tree(22)) + end + + def test_tree_23_43 + expected = <<'End'.chomp +aaaa[bbbbb[ccc, dd], + eee, + ffff[gg, hhh, ii]] +End + assert_equal(expected, tree(23)) + assert_equal(expected, tree(43)) + end + + def test_tree_44 + assert_equal(<<'End'.chomp, tree(44)) +aaaa[bbbbb[ccc, dd], eee, ffff[gg, hhh, ii]] +End + end + + def tree_alt(width) + PrettyPrint.format('', width) {|q| @tree.altshow(q)} + end + + def test_tree_alt_00_18 + expected = <<'End'.chomp +aaaa[ + bbbbb[ + ccc, + dd + ], + eee, + ffff[ + gg, + hhh, + ii + ] +] +End + assert_equal(expected, tree_alt(0)) + assert_equal(expected, tree_alt(18)) + end + + def test_tree_alt_19_20 + expected = <<'End'.chomp +aaaa[ + bbbbb[ ccc, dd ], + eee, + ffff[ + gg, + hhh, + ii + ] +] +End + assert_equal(expected, tree_alt(19)) + assert_equal(expected, tree_alt(20)) + end + + def test_tree_alt_20_49 + expected = <<'End'.chomp +aaaa[ + bbbbb[ ccc, dd ], + eee, + ffff[ gg, hhh, ii ] +] +End + assert_equal(expected, tree_alt(21)) + assert_equal(expected, tree_alt(49)) + end + + def test_tree_alt_50 + expected = <<'End'.chomp +aaaa[ bbbbb[ ccc, dd ], eee, ffff[ gg, hhh, ii ] ] +End + assert_equal(expected, tree_alt(50)) + end + + class Tree # :nodoc: + def initialize(string, *children) + @string = string + @children = children + end + + def show(q) + q.group { + q.text @string + q.nest(@string.length) { + unless @children.empty? + q.text '[' + q.nest(1) { + first = true + @children.each {|t| + if first + first = false + else + q.text ',' + q.breakable + end + t.show(q) + } + } + q.text ']' + end + } + } + end + + def altshow(q) + q.group { + q.text @string + unless @children.empty? + q.text '[' + q.nest(2) { + q.breakable + first = true + @children.each {|t| + if first + first = false + else + q.text ',' + q.breakable + end + t.altshow(q) + } + } + q.breakable + q.text ']' + end + } + end + + end +end + +class StrictPrettyExample < Test::Unit::TestCase # :nodoc: + def prog(width) + PrettyPrint.format('', width) {|q| + q.group { + q.group {q.nest(2) { + q.text "if"; q.breakable; + q.group { + q.nest(2) { + q.group {q.text "a"; q.breakable; q.text "=="} + q.breakable; q.text "b"}}}} + q.breakable + q.group {q.nest(2) { + q.text "then"; q.breakable; + q.group { + q.nest(2) { + q.group {q.text "a"; q.breakable; q.text "<<"} + q.breakable; q.text "2"}}}} + q.breakable + q.group {q.nest(2) { + q.text "else"; q.breakable; + q.group { + q.nest(2) { + q.group {q.text "a"; q.breakable; q.text "+"} + q.breakable; q.text "b"}}}}} + } + end + + def test_00_04 + expected = <<'End'.chomp +if + a + == + b +then + a + << + 2 +else + a + + + b +End + assert_equal(expected, prog(0)) + assert_equal(expected, prog(4)) + end + + def test_05 + expected = <<'End'.chomp +if + a + == + b +then + a + << + 2 +else + a + + b +End + assert_equal(expected, prog(5)) + end + + def test_06 + expected = <<'End'.chomp +if + a == + b +then + a << + 2 +else + a + + b +End + assert_equal(expected, prog(6)) + end + + def test_07 + expected = <<'End'.chomp +if + a == + b +then + a << + 2 +else + a + b +End + assert_equal(expected, prog(7)) + end + + def test_08 + expected = <<'End'.chomp +if + a == b +then + a << 2 +else + a + b +End + assert_equal(expected, prog(8)) + end + + def test_09 + expected = <<'End'.chomp +if a == b +then + a << 2 +else + a + b +End + assert_equal(expected, prog(9)) + end + + def test_10 + expected = <<'End'.chomp +if a == b +then + a << 2 +else a + b +End + assert_equal(expected, prog(10)) + end + + def test_11_31 + expected = <<'End'.chomp +if a == b +then a << 2 +else a + b +End + assert_equal(expected, prog(11)) + assert_equal(expected, prog(15)) + assert_equal(expected, prog(31)) + end + + def test_32 + expected = <<'End'.chomp +if a == b then a << 2 else a + b +End + assert_equal(expected, prog(32)) + end + +end + +class TailGroup < Test::Unit::TestCase # :nodoc: + def test_1 + out = PrettyPrint.format('', 10) {|q| + q.group { + q.group { + q.text "abc" + q.breakable + q.text "def" + } + q.group { + q.text "ghi" + q.breakable + q.text "jkl" + } + } + } + assert_equal("abc defghi\njkl", out) + end +end + +class NonString < Test::Unit::TestCase # :nodoc: + def format(width) + PrettyPrint.format([], width, 'newline', lambda {|n| "#{n} spaces"}) {|q| + q.text(3, 3) + q.breakable(1, 1) + q.text(3, 3) + } + end + + def test_6 + assert_equal([3, "newline", "0 spaces", 3], format(6)) + end + + def test_7 + assert_equal([3, 1, 3], format(7)) + end + +end + +class Fill < Test::Unit::TestCase # :nodoc: + def format(width) + PrettyPrint.format('', width) {|q| + q.group { + q.text 'abc' + q.fill_breakable + q.text 'def' + q.fill_breakable + q.text 'ghi' + q.fill_breakable + q.text 'jkl' + q.fill_breakable + q.text 'mno' + q.fill_breakable + q.text 'pqr' + q.fill_breakable + q.text 'stu' + } + } + end + + def test_00_06 + expected = <<'End'.chomp +abc +def +ghi +jkl +mno +pqr +stu +End + assert_equal(expected, format(0)) + assert_equal(expected, format(6)) + end + + def test_07_10 + expected = <<'End'.chomp +abc def +ghi jkl +mno pqr +stu +End + assert_equal(expected, format(7)) + assert_equal(expected, format(10)) + end + + def test_11_14 + expected = <<'End'.chomp +abc def ghi +jkl mno pqr +stu +End + assert_equal(expected, format(11)) + assert_equal(expected, format(14)) + end + + def test_15_18 + expected = <<'End'.chomp +abc def ghi jkl +mno pqr stu +End + assert_equal(expected, format(15)) + assert_equal(expected, format(18)) + end + + def test_19_22 + expected = <<'End'.chomp +abc def ghi jkl mno +pqr stu +End + assert_equal(expected, format(19)) + assert_equal(expected, format(22)) + end + + def test_23_26 + expected = <<'End'.chomp +abc def ghi jkl mno pqr +stu +End + assert_equal(expected, format(23)) + assert_equal(expected, format(26)) + end + + def test_27 + expected = <<'End'.chomp +abc def ghi jkl mno pqr stu +End + assert_equal(expected, format(27)) + end + +end diff --git a/test/test_tsort.rb b/test/test_tsort.rb index 981fedd98f..38018c49c0 100644 --- a/test/test_tsort.rb +++ b/test/test_tsort.rb @@ -1,4 +1,44 @@ -require 'pathname' -require Pathname.new(__FILE__).dirname.join('inlinetest.rb') -target = __FILE__[/test_(.*\.rb)$/, 1] -InlineTest.loadtest(target) +require 'tsort' +require 'test/unit' + +class TSortHash < Hash # :nodoc: + include TSort + alias tsort_each_node each_key + def tsort_each_child(node, &block) + fetch(node).each(&block) + end +end + +class TSortArray < Array # :nodoc: + include TSort + alias tsort_each_node each_index + def tsort_each_child(node, &block) + fetch(node).each(&block) + end +end + +class TSortTest < Test::Unit::TestCase # :nodoc: + def test_dag + h = TSortHash[{1=>[2, 3], 2=>[3], 3=>[]}] + assert_equal([3, 2, 1], h.tsort) + assert_equal([[3], [2], [1]], h.strongly_connected_components) + end + + def test_cycle + h = TSortHash[{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}] + assert_equal([[4], [2, 3], [1]], + h.strongly_connected_components.map {|nodes| nodes.sort}) + assert_raise(TSort::Cyclic) { h.tsort } + end + + def test_array + a = TSortArray[[1], [0], [0], [2]] + assert_equal([[0, 1], [2], [3]], + a.strongly_connected_components.map {|nodes| nodes.sort}) + + a = TSortArray[[], [0]] + assert_equal([[0], [1]], + a.strongly_connected_components.map {|nodes| nodes.sort}) + end +end + -- cgit v1.2.3