aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_pp.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-14 23:36:45 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-14 23:36:45 +0000
commit29f73c43a5dde61a8ddea3dd31af7d806fc51c2b (patch)
tree0195b446819ac0b2cbb9644f197a78db91207f8e /test/test_pp.rb
parenta2f28cb6f5c388964161b105a851eaaf3697b891 (diff)
downloadruby-29f73c43a5dde61a8ddea3dd31af7d806fc51c2b.tar.gz
* 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
Diffstat (limited to 'test/test_pp.rb')
-rw-r--r--test/test_pp.rb184
1 files changed, 180 insertions, 4 deletions
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("#<struct Struct::OverriddenStruct members=1, class=2>\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 "<inspect:#{@a.inspect}>"
+ end
+end
+
+class HasPrettyPrint
+ def initialize(a)
+ @a = a
+ end
+
+ def pretty_print(q)
+ q.text "<pretty_print:"
+ q.pp @a
+ q.text ">"
+ end
+end
+
+class HasBoth
+ def initialize(a)
+ @a = a
+ end
+
+ def inspect
+ return "<inspect:#{@a.inspect}>"
+ end
+
+ def pretty_print(q)
+ q.text "<pretty_print:"
+ q.pp @a
+ 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("<inspect:1>\n", PP.pp(a, ''))
+ end
+
+ def test_hasprettyprint
+ a = HasPrettyPrint.new(1)
+ assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
+ end
+
+ def test_hasboth
+ a = HasBoth.new(1)
+ assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
+ end
+
+ def test_pretty_print_inspect
+ a = PrettyPrintInspect.new(1)
+ assert_equal("<pretty_print:1>", 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#<Object.*>\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("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\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("[<inspect:[...]>]\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