1require 'pp' 2require 'delegate' 3require 'test/unit' 4 5module PPTestModule 6 7class PPTest < Test::Unit::TestCase 8 def test_list0123_12 9 assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12)) 10 end 11 12 def test_list0123_11 13 assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11)) 14 end 15 16 OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) 17 def test_struct_override_members # [ruby-core:7865] 18 a = OverriddenStruct.new(1,2) 19 assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, '')) 20 end 21 22 def test_redefined_method 23 o = "" 24 def o.method 25 end 26 assert_equal(%(""\n), PP.pp(o, "")) 27 end 28end 29 30class HasInspect 31 def initialize(a) 32 @a = a 33 end 34 35 def inspect 36 return "<inspect:#{@a.inspect}>" 37 end 38end 39 40class HasPrettyPrint 41 def initialize(a) 42 @a = a 43 end 44 45 def pretty_print(q) 46 q.text "<pretty_print:" 47 q.pp @a 48 q.text ">" 49 end 50end 51 52class HasBoth 53 def initialize(a) 54 @a = a 55 end 56 57 def inspect 58 return "<inspect:#{@a.inspect}>" 59 end 60 61 def pretty_print(q) 62 q.text "<pretty_print:" 63 q.pp @a 64 q.text ">" 65 end 66end 67 68class PrettyPrintInspect < HasPrettyPrint 69 alias inspect pretty_print_inspect 70end 71 72class PrettyPrintInspectWithoutPrettyPrint 73 alias inspect pretty_print_inspect 74end 75 76class PPInspectTest < Test::Unit::TestCase 77 def test_hasinspect 78 a = HasInspect.new(1) 79 assert_equal("<inspect:1>\n", PP.pp(a, '')) 80 end 81 82 def test_hasprettyprint 83 a = HasPrettyPrint.new(1) 84 assert_equal("<pretty_print:1>\n", PP.pp(a, '')) 85 end 86 87 def test_hasboth 88 a = HasBoth.new(1) 89 assert_equal("<pretty_print:1>\n", PP.pp(a, '')) 90 end 91 92 def test_pretty_print_inspect 93 a = PrettyPrintInspect.new(1) 94 assert_equal("<pretty_print:1>", a.inspect) 95 a = PrettyPrintInspectWithoutPrettyPrint.new 96 assert_raise(RuntimeError) { a.inspect } 97 end 98 99 def test_proc 100 a = proc {1} 101 assert_equal("#{a.inspect}\n", PP.pp(a, '')) 102 end 103 104 def test_to_s_with_iv 105 a = Object.new 106 def a.to_s() "aaa" end 107 a.instance_eval { @a = nil } 108 result = PP.pp(a, '') 109 assert_equal("#{a.inspect}\n", result) 110 end 111 112 def test_to_s_without_iv 113 a = Object.new 114 def a.to_s() "aaa" end 115 result = PP.pp(a, '') 116 assert_equal("#{a.inspect}\n", result) 117 end 118end 119 120class PPCycleTest < Test::Unit::TestCase 121 def test_array 122 a = [] 123 a << a 124 assert_equal("[[...]]\n", PP.pp(a, '')) 125 assert_equal("#{a.inspect}\n", PP.pp(a, '')) 126 end 127 128 def test_hash 129 a = {} 130 a[0] = a 131 assert_equal("{0=>{...}}\n", PP.pp(a, '')) 132 assert_equal("#{a.inspect}\n", PP.pp(a, '')) 133 end 134 135 S = Struct.new("S", :a, :b) 136 def test_struct 137 a = S.new(1,2) 138 a.b = a 139 assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, '')) 140 assert_equal("#{a.inspect}\n", PP.pp(a, '')) 141 end 142 143 def test_object 144 a = Object.new 145 a.instance_eval {@a = a} 146 assert_equal(a.inspect + "\n", PP.pp(a, '')) 147 end 148 149 def test_anonymous 150 a = Class.new.new 151 assert_equal(a.inspect + "\n", PP.pp(a, '')) 152 end 153 154 def test_withinspect 155 a = [] 156 a << HasInspect.new(a) 157 assert_equal("[<inspect:[...]>]\n", PP.pp(a, '')) 158 assert_equal("#{a.inspect}\n", PP.pp(a, '')) 159 end 160 161 def test_share_nil 162 begin 163 PP.sharing_detection = true 164 a = [nil, nil] 165 assert_equal("[nil, nil]\n", PP.pp(a, '')) 166 ensure 167 PP.sharing_detection = false 168 end 169 end 170end 171 172class PPSingleLineTest < Test::Unit::TestCase 173 def test_hash 174 assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699] 175 assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, '')) 176 end 177end 178 179class PPDelegateTest < Test::Unit::TestCase 180 class A < DelegateClass(Array); end 181 182 def test_delegate 183 assert_equal("[]\n", A.new([]).pretty_inspect, "[ruby-core:25804]") 184 end 185end 186 187end 188