PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_pp.rb

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