PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mri/ruby/test_module.rb

http://github.com/jruby/jruby
Ruby | 2213 lines | 1911 code | 290 blank | 12 comment | 9 complexity | 721ec603704ebfd00a43bbdd27584131 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'pp'
  4. $m0 = Module.nesting
  5. class TestModule < Test::Unit::TestCase
  6. def _wrap_assertion
  7. yield
  8. end
  9. def assert_method_defined?(klass, mid, message="")
  10. message = build_message(message, "#{klass}\##{mid} expected to be defined.")
  11. _wrap_assertion do
  12. klass.method_defined?(mid) or
  13. raise Test::Unit::AssertionFailedError, message, caller(3)
  14. end
  15. end
  16. def assert_method_not_defined?(klass, mid, message="")
  17. message = build_message(message, "#{klass}\##{mid} expected to not be defined.")
  18. _wrap_assertion do
  19. klass.method_defined?(mid) and
  20. raise Test::Unit::AssertionFailedError, message, caller(3)
  21. end
  22. end
  23. def setup
  24. @verbose = $VERBOSE
  25. $VERBOSE = nil
  26. end
  27. def teardown
  28. $VERBOSE = @verbose
  29. end
  30. def test_LT_0
  31. assert_equal true, String < Object
  32. assert_equal false, Object < String
  33. assert_nil String < Array
  34. assert_equal true, Array < Enumerable
  35. assert_equal false, Enumerable < Array
  36. assert_nil Proc < Comparable
  37. assert_nil Comparable < Proc
  38. end
  39. def test_GT_0
  40. assert_equal false, String > Object
  41. assert_equal true, Object > String
  42. assert_nil String > Array
  43. assert_equal false, Array > Enumerable
  44. assert_equal true, Enumerable > Array
  45. assert_nil Comparable > Proc
  46. assert_nil Proc > Comparable
  47. end
  48. def test_CMP_0
  49. assert_equal(-1, (String <=> Object))
  50. assert_equal 1, (Object <=> String)
  51. assert_nil(Array <=> String)
  52. end
  53. ExpectedException = NoMethodError
  54. # Support stuff
  55. module Mixin
  56. MIXIN = 1
  57. def mixin
  58. end
  59. end
  60. module User
  61. USER = 2
  62. include Mixin
  63. def user
  64. end
  65. def user2
  66. end
  67. protected :user2
  68. def user3
  69. end
  70. private :user3
  71. end
  72. module Other
  73. def other
  74. end
  75. end
  76. class AClass
  77. def AClass.cm1
  78. "cm1"
  79. end
  80. def AClass.cm2
  81. cm1 + "cm2" + cm3
  82. end
  83. def AClass.cm3
  84. "cm3"
  85. end
  86. private_class_method :cm1, "cm3"
  87. def aClass
  88. :aClass
  89. end
  90. def aClass1
  91. :aClass1
  92. end
  93. def aClass2
  94. :aClass2
  95. end
  96. private :aClass1
  97. protected :aClass2
  98. end
  99. class BClass < AClass
  100. def bClass1
  101. :bClass1
  102. end
  103. private
  104. def bClass2
  105. :bClass2
  106. end
  107. protected
  108. def bClass3
  109. :bClass3
  110. end
  111. end
  112. class CClass < BClass
  113. def self.cClass
  114. end
  115. end
  116. MyClass = AClass.clone
  117. class MyClass
  118. public_class_method :cm1
  119. end
  120. # -----------------------------------------------------------
  121. def test_CMP # '<=>'
  122. assert_equal( 0, Mixin <=> Mixin)
  123. assert_equal(-1, User <=> Mixin)
  124. assert_equal( 1, Mixin <=> User)
  125. assert_equal( 0, Object <=> Object)
  126. assert_equal(-1, String <=> Object)
  127. assert_equal( 1, Object <=> String)
  128. end
  129. def test_GE # '>='
  130. assert_operator(Mixin, :>=, User)
  131. assert_operator(Mixin, :>=, Mixin)
  132. assert_not_operator(User, :>=, Mixin)
  133. assert_operator(Object, :>=, String)
  134. assert_operator(String, :>=, String)
  135. assert_not_operator(String, :>=, Object)
  136. end
  137. def test_GT # '>'
  138. assert_operator(Mixin, :>, User)
  139. assert_not_operator(Mixin, :>, Mixin)
  140. assert_not_operator(User, :>, Mixin)
  141. assert_operator(Object, :>, String)
  142. assert_not_operator(String, :>, String)
  143. assert_not_operator(String, :>, Object)
  144. end
  145. def test_LE # '<='
  146. assert_operator(User, :<=, Mixin)
  147. assert_operator(Mixin, :<=, Mixin)
  148. assert_not_operator(Mixin, :<=, User)
  149. assert_operator(String, :<=, Object)
  150. assert_operator(String, :<=, String)
  151. assert_not_operator(Object, :<=, String)
  152. end
  153. def test_LT # '<'
  154. assert_operator(User, :<, Mixin)
  155. assert_not_operator(Mixin, :<, Mixin)
  156. assert_not_operator(Mixin, :<, User)
  157. assert_operator(String, :<, Object)
  158. assert_not_operator(String, :<, String)
  159. assert_not_operator(Object, :<, String)
  160. end
  161. def test_VERY_EQUAL # '==='
  162. assert_operator(Object, :===, self)
  163. assert_operator(Test::Unit::TestCase, :===, self)
  164. assert_operator(TestModule, :===, self)
  165. assert_not_operator(String, :===, self)
  166. end
  167. def test_ancestors
  168. assert_equal([User, Mixin], User.ancestors)
  169. assert_equal([Mixin], Mixin.ancestors)
  170. ancestors = Object.ancestors
  171. mixins = ancestors - [Object, Kernel, BasicObject]
  172. mixins << JSON::Ext::Generator::GeneratorMethods::String if defined?(JSON::Ext::Generator::GeneratorMethods::String)
  173. assert_equal([Object, Kernel, BasicObject], ancestors - mixins)
  174. assert_equal([String, Comparable, Object, Kernel, BasicObject], String.ancestors - mixins)
  175. end
  176. CLASS_EVAL = 2
  177. @@class_eval = 'b'
  178. def test_class_eval
  179. Other.class_eval("CLASS_EVAL = 1")
  180. assert_equal(1, Other::CLASS_EVAL)
  181. assert_include(Other.constants, :CLASS_EVAL)
  182. assert_equal(2, Other.class_eval { CLASS_EVAL })
  183. Other.class_eval("@@class_eval = 'a'")
  184. assert_equal('a', Other.class_variable_get(:@@class_eval))
  185. assert_equal('b', Other.class_eval { @@class_eval })
  186. Other.class_eval do
  187. module_function
  188. def class_eval_test
  189. "foo"
  190. end
  191. end
  192. assert_equal("foo", Other.class_eval_test)
  193. assert_equal([Other], Other.class_eval { |*args| args })
  194. end
  195. def test_const_defined?
  196. assert_operator(Math, :const_defined?, :PI)
  197. assert_operator(Math, :const_defined?, "PI")
  198. assert_not_operator(Math, :const_defined?, :IP)
  199. assert_not_operator(Math, :const_defined?, "IP")
  200. end
  201. def each_bad_constants(m, &b)
  202. [
  203. "#<Class:0x7b8b718b>",
  204. ":Object",
  205. "",
  206. ":",
  207. ["String::", "[Bug #7573]"],
  208. "\u3042",
  209. "Name?",
  210. ].each do |name, msg|
  211. expected = "wrong constant name %s" % quote(name)
  212. msg = "#{msg}#{': ' if msg}wrong constant name #{name.dump}"
  213. assert_raise_with_message(NameError, expected, "#{msg} to #{m}") do
  214. yield name
  215. end
  216. end
  217. end
  218. def test_bad_constants_get
  219. each_bad_constants("get") {|name|
  220. Object.const_get name
  221. }
  222. end
  223. def test_bad_constants_defined
  224. each_bad_constants("defined?") {|name|
  225. Object.const_defined? name
  226. }
  227. end
  228. def test_leading_colons
  229. assert_equal Object, AClass.const_get('::Object')
  230. end
  231. def test_const_get
  232. assert_equal(Math::PI, Math.const_get("PI"))
  233. assert_equal(Math::PI, Math.const_get(:PI))
  234. n = Object.new
  235. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "PI"; end
  236. def n.count; @count; end
  237. assert_equal(Math::PI, Math.const_get(n))
  238. assert_equal(1, n.count)
  239. end
  240. def test_nested_get
  241. assert_equal Other, Object.const_get([self.class, Other].join('::'))
  242. assert_equal User::USER, self.class.const_get([User, 'USER'].join('::'))
  243. end
  244. def test_nested_get_symbol
  245. const = [self.class, Other].join('::').to_sym
  246. assert_raise(NameError) {Object.const_get(const)}
  247. const = [User, 'USER'].join('::').to_sym
  248. assert_raise(NameError) {self.class.const_get(const)}
  249. end
  250. def test_nested_get_const_missing
  251. classes = []
  252. klass = Class.new {
  253. define_singleton_method(:const_missing) { |name|
  254. classes << name
  255. klass
  256. }
  257. }
  258. klass.const_get("Foo::Bar::Baz")
  259. assert_equal [:Foo, :Bar, :Baz], classes
  260. end
  261. def test_nested_get_bad_class
  262. assert_raise(TypeError) do
  263. self.class.const_get([User, 'USER', 'Foo'].join('::'))
  264. end
  265. end
  266. def test_nested_defined
  267. assert_send([Object, :const_defined?, [self.class.name, 'Other'].join('::')])
  268. assert_send([self.class, :const_defined?, 'User::USER'])
  269. assert_not_send([self.class, :const_defined?, 'User::Foo'])
  270. end
  271. def test_nested_defined_symbol
  272. const = [self.class, Other].join('::').to_sym
  273. assert_raise(NameError) {Object.const_defined?(const)}
  274. const = [User, 'USER'].join('::').to_sym
  275. assert_raise(NameError) {self.class.const_defined?(const)}
  276. end
  277. def test_nested_defined_bad_class
  278. assert_raise(TypeError) do
  279. self.class.const_defined?('User::USER::Foo')
  280. end
  281. end
  282. def test_const_set
  283. assert_not_operator(Other, :const_defined?, :KOALA)
  284. Other.const_set(:KOALA, 99)
  285. assert_operator(Other, :const_defined?, :KOALA)
  286. assert_equal(99, Other::KOALA)
  287. Other.const_set("WOMBAT", "Hi")
  288. assert_equal("Hi", Other::WOMBAT)
  289. n = Object.new
  290. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "HOGE"; end
  291. def n.count; @count; end
  292. def n.count=(v); @count=v; end
  293. assert_not_operator(Other, :const_defined?, :HOGE)
  294. Other.const_set(n, 999)
  295. assert_equal(1, n.count)
  296. n.count = 0
  297. assert_equal(999, Other.const_get(n))
  298. assert_equal(1, n.count)
  299. n.count = 0
  300. assert_equal(true, Other.const_defined?(n))
  301. assert_equal(1, n.count)
  302. end
  303. def test_constants
  304. assert_equal([:MIXIN], Mixin.constants)
  305. assert_equal([:MIXIN, :USER], User.constants.sort)
  306. end
  307. def test_self_initialize_copy
  308. bug9535 = '[ruby-dev:47989] [Bug #9535]'
  309. m = Module.new do
  310. def foo
  311. :ok
  312. end
  313. initialize_copy(self)
  314. end
  315. assert_equal(:ok, Object.new.extend(m).foo, bug9535)
  316. end
  317. def test_initialize_copy_empty
  318. bug9813 = '[ruby-dev:48182] [Bug #9813]'
  319. m = Module.new do
  320. def x
  321. end
  322. const_set(:X, 1)
  323. @x = 2
  324. end
  325. assert_equal([:x], m.instance_methods)
  326. assert_equal([:@x], m.instance_variables)
  327. assert_equal([:X], m.constants)
  328. m.module_eval do
  329. initialize_copy(Module.new)
  330. end
  331. assert_empty(m.instance_methods, bug9813)
  332. assert_empty(m.instance_variables, bug9813)
  333. assert_empty(m.constants, bug9813)
  334. end
  335. def test_dup
  336. bug6454 = '[ruby-core:45132]'
  337. a = Module.new
  338. Other.const_set :BUG6454, a
  339. b = a.dup
  340. Other.const_set :BUG6454_dup, b
  341. assert_equal "TestModule::Other::BUG6454_dup", b.inspect, bug6454
  342. end
  343. def test_dup_anonymous
  344. bug6454 = '[ruby-core:45132]'
  345. a = Module.new
  346. original = a.inspect
  347. b = a.dup
  348. assert_not_equal original, b.inspect, bug6454
  349. end
  350. def test_public_include
  351. assert_nothing_raised('#8846') do
  352. Module.new.include(Module.new { def foo; end }).instance_methods == [:foo]
  353. end
  354. end
  355. def test_include_toplevel
  356. assert_separately([], <<-EOS)
  357. Mod = Module.new {def foo; :include_foo end}
  358. TOPLEVEL_BINDING.eval('include Mod')
  359. assert_equal(:include_foo, TOPLEVEL_BINDING.eval('foo'))
  360. assert_equal([Object, Mod], Object.ancestors.slice(0, 2))
  361. EOS
  362. end
  363. def test_included_modules
  364. assert_equal([], Mixin.included_modules)
  365. assert_equal([Mixin], User.included_modules)
  366. mixins = Object.included_modules - [Kernel]
  367. mixins << JSON::Ext::Generator::GeneratorMethods::String if defined?(JSON::Ext::Generator::GeneratorMethods::String)
  368. assert_equal([Kernel], Object.included_modules - mixins)
  369. assert_equal([Comparable, Kernel], String.included_modules - mixins)
  370. end
  371. def test_instance_methods
  372. assert_equal([:user, :user2], User.instance_methods(false).sort)
  373. assert_equal([:user, :user2, :mixin].sort, User.instance_methods(true).sort)
  374. assert_equal([:mixin], Mixin.instance_methods)
  375. assert_equal([:mixin], Mixin.instance_methods(true))
  376. assert_equal([:cClass], (class << CClass; self; end).instance_methods(false))
  377. assert_equal([], (class << BClass; self; end).instance_methods(false))
  378. assert_equal([:cm2], (class << AClass; self; end).instance_methods(false))
  379. # Ruby 1.8 feature change:
  380. # #instance_methods includes protected methods.
  381. #assert_equal([:aClass], AClass.instance_methods(false))
  382. assert_equal([:aClass, :aClass2], AClass.instance_methods(false).sort)
  383. assert_equal([:aClass, :aClass2],
  384. (AClass.instance_methods(true) - Object.instance_methods(true)).sort)
  385. end
  386. def test_method_defined?
  387. assert !User.method_defined?(:wombat)
  388. assert User.method_defined?(:mixin)
  389. assert User.method_defined?(:user)
  390. assert User.method_defined?(:user2)
  391. assert !User.method_defined?(:user3)
  392. assert !User.method_defined?("wombat")
  393. assert User.method_defined?("mixin")
  394. assert User.method_defined?("user")
  395. assert User.method_defined?("user2")
  396. assert !User.method_defined?("user3")
  397. end
  398. def module_exec_aux
  399. Proc.new do
  400. def dynamically_added_method_3; end
  401. end
  402. end
  403. def module_exec_aux_2(&block)
  404. User.module_exec(&block)
  405. end
  406. def test_module_exec
  407. User.module_exec do
  408. def dynamically_added_method_1; end
  409. end
  410. assert_method_defined?(User, :dynamically_added_method_1)
  411. block = Proc.new do
  412. def dynamically_added_method_2; end
  413. end
  414. User.module_exec(&block)
  415. assert_method_defined?(User, :dynamically_added_method_2)
  416. User.module_exec(&module_exec_aux)
  417. assert_method_defined?(User, :dynamically_added_method_3)
  418. module_exec_aux_2 do
  419. def dynamically_added_method_4; end
  420. end
  421. assert_method_defined?(User, :dynamically_added_method_4)
  422. end
  423. def test_module_eval
  424. User.module_eval("MODULE_EVAL = 1")
  425. assert_equal(1, User::MODULE_EVAL)
  426. assert_include(User.constants, :MODULE_EVAL)
  427. User.instance_eval("remove_const(:MODULE_EVAL)")
  428. assert_not_include(User.constants, :MODULE_EVAL)
  429. end
  430. def test_name
  431. assert_equal("Fixnum", Fixnum.name)
  432. assert_equal("TestModule::Mixin", Mixin.name)
  433. assert_equal("TestModule::User", User.name)
  434. end
  435. def test_classpath
  436. m = Module.new
  437. n = Module.new
  438. m.const_set(:N, n)
  439. assert_nil(m.name)
  440. assert_nil(n.name)
  441. assert_equal([:N], m.constants)
  442. m.module_eval("module O end")
  443. assert_equal([:N, :O], m.constants)
  444. m.module_eval("class C; end")
  445. assert_equal([:N, :O, :C], m.constants)
  446. assert_nil(m::N.name)
  447. assert_match(/\A#<Module:.*>::O\z/, m::O.name)
  448. assert_match(/\A#<Module:.*>::C\z/, m::C.name)
  449. self.class.const_set(:M, m)
  450. prefix = self.class.name + "::M::"
  451. assert_equal(prefix+"N", m.const_get(:N).name)
  452. assert_equal(prefix+"O", m.const_get(:O).name)
  453. assert_equal(prefix+"C", m.const_get(:C).name)
  454. end
  455. def test_private_class_method
  456. assert_raise(ExpectedException) { AClass.cm1 }
  457. assert_raise(ExpectedException) { AClass.cm3 }
  458. assert_equal("cm1cm2cm3", AClass.cm2)
  459. end
  460. def test_private_instance_methods
  461. assert_equal([:aClass1], AClass.private_instance_methods(false))
  462. assert_equal([:bClass2], BClass.private_instance_methods(false))
  463. assert_equal([:aClass1, :bClass2],
  464. (BClass.private_instance_methods(true) -
  465. Object.private_instance_methods(true)).sort)
  466. end
  467. def test_protected_instance_methods
  468. assert_equal([:aClass2], AClass.protected_instance_methods)
  469. assert_equal([:bClass3], BClass.protected_instance_methods(false))
  470. assert_equal([:bClass3, :aClass2].sort,
  471. (BClass.protected_instance_methods(true) -
  472. Object.protected_instance_methods(true)).sort)
  473. end
  474. def test_public_class_method
  475. assert_equal("cm1", MyClass.cm1)
  476. assert_equal("cm1cm2cm3", MyClass.cm2)
  477. assert_raise(ExpectedException) { eval "MyClass.cm3" }
  478. end
  479. def test_public_instance_methods
  480. assert_equal([:aClass], AClass.public_instance_methods(false))
  481. assert_equal([:bClass1], BClass.public_instance_methods(false))
  482. end
  483. def test_s_constants
  484. c1 = Module.constants
  485. Object.module_eval "WALTER = 99"
  486. c2 = Module.constants
  487. assert_equal([:WALTER], c2 - c1)
  488. assert_equal([], Module.constants(true))
  489. assert_equal([], Module.constants(false))
  490. src = <<-INPUT
  491. ary = Module.constants
  492. module M
  493. WALTER = 99
  494. end
  495. class Module
  496. include M
  497. end
  498. p Module.constants - ary, Module.constants(true), Module.constants(false)
  499. INPUT
  500. assert_in_out_err([], src, %w([:M] [:WALTER] []), [])
  501. klass = Class.new do
  502. const_set(:X, 123)
  503. end
  504. assert_equal(false, klass.class_eval { Module.constants }.include?(:X))
  505. end
  506. module M1
  507. $m1 = Module.nesting
  508. module M2
  509. $m2 = Module.nesting
  510. end
  511. end
  512. def test_s_nesting
  513. assert_equal([], $m0)
  514. assert_equal([TestModule::M1, TestModule], $m1)
  515. assert_equal([TestModule::M1::M2,
  516. TestModule::M1, TestModule], $m2)
  517. end
  518. def test_s_new
  519. m = Module.new
  520. assert_instance_of(Module, m)
  521. end
  522. def test_freeze
  523. m = Module.new do
  524. def self.baz; end
  525. def bar; end
  526. end
  527. m.freeze
  528. assert_raise(RuntimeError) do
  529. m.module_eval do
  530. def foo; end
  531. end
  532. end
  533. assert_raise(RuntimeError) do
  534. m.__send__ :private, :bar
  535. end
  536. assert_raise(RuntimeError) do
  537. m.private_class_method :baz
  538. end
  539. end
  540. def test_attr_obsoleted_flag
  541. c = Class.new
  542. c.class_eval do
  543. def initialize
  544. @foo = :foo
  545. @bar = :bar
  546. end
  547. attr :foo, true
  548. attr :bar, false
  549. end
  550. o = c.new
  551. assert_equal(true, o.respond_to?(:foo))
  552. assert_equal(true, o.respond_to?(:foo=))
  553. assert_equal(true, o.respond_to?(:bar))
  554. assert_equal(false, o.respond_to?(:bar=))
  555. end
  556. def test_const_get_evaled
  557. c1 = Class.new
  558. c2 = Class.new(c1)
  559. eval("c1::Foo = :foo")
  560. assert_equal(:foo, c1::Foo)
  561. assert_equal(:foo, c2::Foo)
  562. assert_equal(:foo, c2.const_get(:Foo))
  563. assert_raise(NameError) { c2.const_get(:Foo, false) }
  564. eval("c1::Foo = :foo")
  565. assert_raise(NameError) { c1::Bar }
  566. assert_raise(NameError) { c2::Bar }
  567. assert_raise(NameError) { c2.const_get(:Bar) }
  568. assert_raise(NameError) { c2.const_get(:Bar, false) }
  569. assert_raise(NameError) { c2.const_get("Bar", false) }
  570. assert_raise(NameError) { c2.const_get("BaR11", false) }
  571. assert_raise(NameError) { Object.const_get("BaR11", false) }
  572. c1.instance_eval do
  573. def const_missing(x)
  574. x
  575. end
  576. end
  577. assert_equal(:Bar, c1::Bar)
  578. assert_equal(:Bar, c2::Bar)
  579. assert_equal(:Bar, c2.const_get(:Bar))
  580. assert_equal(:Bar, c2.const_get(:Bar, false))
  581. assert_equal(:Bar, c2.const_get("Bar"))
  582. assert_equal(:Bar, c2.const_get("Bar", false))
  583. v = c2.const_get("Bar11", false)
  584. assert_equal("Bar11".to_sym, v)
  585. assert_raise(NameError) { c1.const_get(:foo) }
  586. end
  587. def test_const_set_invalid_name
  588. c1 = Class.new
  589. assert_raise_with_message(NameError, /foo/) { c1.const_set(:foo, :foo) }
  590. assert_raise_with_message(NameError, /bar/) { c1.const_set("bar", :foo) }
  591. assert_raise_with_message(TypeError, /1/) { c1.const_set(1, :foo) }
  592. assert_nothing_raised(NameError) { c1.const_set("X\u{3042}", :foo) }
  593. assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-16be"), :foo) }
  594. assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-16le"), :foo) }
  595. assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-32be"), :foo) }
  596. assert_raise(NameError) { c1.const_set("X\u{3042}".encode("utf-32le"), :foo) }
  597. cx = EnvUtil.labeled_class("X\u{3042}")
  598. EnvUtil.with_default_external(Encoding::UTF_8) {
  599. assert_raise_with_message(TypeError, /X\u{3042}/) { c1.const_set(cx, :foo) }
  600. }
  601. end
  602. def test_const_get_invalid_name
  603. c1 = Class.new
  604. assert_raise(NameError) { c1.const_get(:foo) }
  605. bug5084 = '[ruby-dev:44200]'
  606. assert_raise(TypeError, bug5084) { c1.const_get(1) }
  607. bug7574 = '[ruby-dev:46749]'
  608. assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
  609. Object.const_get("String\0")
  610. }
  611. end
  612. def test_const_defined_invalid_name
  613. c1 = Class.new
  614. assert_raise(NameError) { c1.const_defined?(:foo) }
  615. bug5084 = '[ruby-dev:44200]'
  616. assert_raise(TypeError, bug5084) { c1.const_defined?(1) }
  617. bug7574 = '[ruby-dev:46749]'
  618. assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
  619. Object.const_defined?("String\0")
  620. }
  621. end
  622. def test_const_get_no_inherited
  623. bug3422 = '[ruby-core:30719]'
  624. assert_in_out_err([], <<-INPUT, %w[1 NameError A], [], bug3422)
  625. BasicObject::A = 1
  626. puts [true, false].map {|inh|
  627. begin
  628. Object.const_get(:A, inh)
  629. rescue NameError => e
  630. [e.class, e.name]
  631. end
  632. }
  633. INPUT
  634. end
  635. def test_const_get_inherited
  636. bug3423 = '[ruby-core:30720]'
  637. assert_in_out_err([], <<-INPUT, %w[NameError A NameError A], [], bug3423)
  638. module Foo; A = 1; end
  639. class Object; include Foo; end
  640. class Bar; include Foo; end
  641. puts [Object, Bar].map {|klass|
  642. begin
  643. klass.const_get(:A, false)
  644. rescue NameError => e
  645. [e.class, e.name]
  646. end
  647. }
  648. INPUT
  649. end
  650. def test_const_in_module
  651. bug3423 = '[ruby-core:37698]'
  652. assert_in_out_err([], <<-INPUT, %w[ok], [], bug3423)
  653. module LangModuleSpecInObject
  654. module LangModuleTop
  655. end
  656. end
  657. include LangModuleSpecInObject
  658. module LangModuleTop
  659. end
  660. puts "ok" if LangModuleSpecInObject::LangModuleTop == LangModuleTop
  661. INPUT
  662. bug5264 = '[ruby-core:39227]'
  663. assert_in_out_err([], <<-'INPUT', [], [], bug5264)
  664. class A
  665. class X; end
  666. end
  667. class B < A
  668. module X; end
  669. end
  670. INPUT
  671. end
  672. def test_class_variable_get
  673. c = Class.new
  674. c.class_eval('@@foo = :foo')
  675. assert_equal(:foo, c.class_variable_get(:@@foo))
  676. assert_raise(NameError) { c.class_variable_get(:@@bar) } # c.f. instance_variable_get
  677. assert_raise(NameError) { c.class_variable_get(:'@@') }
  678. assert_raise(NameError) { c.class_variable_get('@@') }
  679. assert_raise(NameError) { c.class_variable_get(:foo) }
  680. assert_raise(NameError) { c.class_variable_get("bar") }
  681. assert_raise(TypeError) { c.class_variable_get(1) }
  682. n = Object.new
  683. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
  684. def n.count; @count; end
  685. assert_equal(:foo, c.class_variable_get(n))
  686. assert_equal(1, n.count)
  687. end
  688. def test_class_variable_set
  689. c = Class.new
  690. c.class_variable_set(:@@foo, :foo)
  691. assert_equal(:foo, c.class_eval('@@foo'))
  692. assert_raise(NameError) { c.class_variable_set(:'@@', 1) }
  693. assert_raise(NameError) { c.class_variable_set('@@', 1) }
  694. assert_raise(NameError) { c.class_variable_set(:foo, 1) }
  695. assert_raise(NameError) { c.class_variable_set("bar", 1) }
  696. assert_raise(TypeError) { c.class_variable_set(1, 1) }
  697. n = Object.new
  698. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
  699. def n.count; @count; end
  700. c.class_variable_set(n, :bar)
  701. assert_equal(:bar, c.class_eval('@@foo'))
  702. assert_equal(1, n.count)
  703. end
  704. def test_class_variable_defined
  705. c = Class.new
  706. c.class_eval('@@foo = :foo')
  707. assert_equal(true, c.class_variable_defined?(:@@foo))
  708. assert_equal(false, c.class_variable_defined?(:@@bar))
  709. assert_raise(NameError) { c.class_variable_defined?(:'@@') }
  710. assert_raise(NameError) { c.class_variable_defined?('@@') }
  711. assert_raise(NameError) { c.class_variable_defined?(:foo) }
  712. assert_raise(NameError) { c.class_variable_defined?("bar") }
  713. assert_raise(TypeError) { c.class_variable_defined?(1) }
  714. n = Object.new
  715. def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
  716. def n.count; @count; end
  717. assert_equal(true, c.class_variable_defined?(n))
  718. assert_equal(1, n.count)
  719. end
  720. def test_remove_class_variable
  721. c = Class.new
  722. c.class_eval('@@foo = :foo')
  723. c.class_eval { remove_class_variable(:@@foo) }
  724. assert_equal(false, c.class_variable_defined?(:@@foo))
  725. assert_raise(NameError) do
  726. c.class_eval { remove_class_variable(:@var) }
  727. end
  728. end
  729. def test_export_method
  730. m = Module.new
  731. assert_raise(NameError) do
  732. m.instance_eval { public(:foo) }
  733. end
  734. end
  735. def test_attr
  736. assert_in_out_err([], <<-INPUT, %w(nil))
  737. $VERBOSE = true
  738. c = Class.new
  739. c.instance_eval do
  740. private
  741. attr_reader :foo
  742. end
  743. o = c.new
  744. p(o.instance_eval { foo })
  745. INPUT
  746. c = Class.new
  747. assert_raise(NameError) do
  748. c.instance_eval { attr_reader :"." }
  749. end
  750. end
  751. def test_undef
  752. c = Class.new
  753. assert_raise(NameError) do
  754. c.instance_eval { undef_method(:foo) }
  755. end
  756. m = Module.new
  757. assert_raise(NameError) do
  758. m.instance_eval { undef_method(:foo) }
  759. end
  760. o = Object.new
  761. assert_raise(NameError) do
  762. class << o; self; end.instance_eval { undef_method(:foo) }
  763. end
  764. %w(object_id __send__ initialize).each do |n|
  765. assert_in_out_err([], <<-INPUT, [], %r"warning: undefining `#{n}' may cause serious problems$")
  766. $VERBOSE = false
  767. Class.new.instance_eval { undef_method(:#{n}) }
  768. INPUT
  769. end
  770. end
  771. def test_alias
  772. m = Module.new
  773. assert_raise(NameError) do
  774. m.class_eval { alias foo bar }
  775. end
  776. assert_in_out_err([], <<-INPUT, %w(2), /discarding old foo$/)
  777. $VERBOSE = true
  778. c = Class.new
  779. c.class_eval do
  780. def foo; 1; end
  781. def bar; 2; end
  782. end
  783. c.class_eval { alias foo bar }
  784. p c.new.foo
  785. INPUT
  786. end
  787. def test_mod_constants
  788. m = Module.new
  789. m.const_set(:Foo, :foo)
  790. assert_equal([:Foo], m.constants(true))
  791. assert_equal([:Foo], m.constants(false))
  792. m.instance_eval { remove_const(:Foo) }
  793. end
  794. class Bug9413
  795. class << self
  796. Foo = :foo
  797. end
  798. end
  799. def test_singleton_constants
  800. bug9413 = '[ruby-core:59763] [Bug #9413]'
  801. c = Bug9413.singleton_class
  802. assert_include(c.constants(true), :Foo, bug9413)
  803. assert_include(c.constants(false), :Foo, bug9413)
  804. end
  805. def test_frozen_module
  806. m = Module.new
  807. m.freeze
  808. assert_raise(RuntimeError) do
  809. m.instance_eval { undef_method(:foo) }
  810. end
  811. end
  812. def test_frozen_class
  813. c = Class.new
  814. c.freeze
  815. assert_raise(RuntimeError) do
  816. c.instance_eval { undef_method(:foo) }
  817. end
  818. end
  819. def test_frozen_singleton_class
  820. klass = Class.new
  821. o = klass.new
  822. c = class << o; self; end
  823. c.freeze
  824. assert_raise_with_message(RuntimeError, /frozen/) do
  825. c.instance_eval { undef_method(:foo) }
  826. end
  827. klass.class_eval do
  828. def self.foo
  829. end
  830. end
  831. end
  832. def test_method_defined
  833. c = Class.new
  834. c.class_eval do
  835. def foo; end
  836. def bar; end
  837. def baz; end
  838. public :foo
  839. protected :bar
  840. private :baz
  841. end
  842. assert_equal(true, c.public_method_defined?(:foo))
  843. assert_equal(false, c.public_method_defined?(:bar))
  844. assert_equal(false, c.public_method_defined?(:baz))
  845. # Test if string arguments are converted to symbols
  846. assert_equal(true, c.public_method_defined?("foo"))
  847. assert_equal(false, c.public_method_defined?("bar"))
  848. assert_equal(false, c.public_method_defined?("baz"))
  849. assert_equal(false, c.protected_method_defined?(:foo))
  850. assert_equal(true, c.protected_method_defined?(:bar))
  851. assert_equal(false, c.protected_method_defined?(:baz))
  852. # Test if string arguments are converted to symbols
  853. assert_equal(false, c.protected_method_defined?("foo"))
  854. assert_equal(true, c.protected_method_defined?("bar"))
  855. assert_equal(false, c.protected_method_defined?("baz"))
  856. assert_equal(false, c.private_method_defined?(:foo))
  857. assert_equal(false, c.private_method_defined?(:bar))
  858. assert_equal(true, c.private_method_defined?(:baz))
  859. # Test if string arguments are converted to symbols
  860. assert_equal(false, c.private_method_defined?("foo"))
  861. assert_equal(false, c.private_method_defined?("bar"))
  862. assert_equal(true, c.private_method_defined?("baz"))
  863. end
  864. def test_top_public_private
  865. assert_in_out_err([], <<-INPUT, %w([:foo] [:bar]), [])
  866. private
  867. def foo; :foo; end
  868. public
  869. def bar; :bar; end
  870. p self.private_methods.grep(/^foo$|^bar$/)
  871. p self.methods.grep(/^foo$|^bar$/)
  872. INPUT
  873. end
  874. def test_append_features
  875. t = nil
  876. m = Module.new
  877. m.module_eval do
  878. def foo; :foo; end
  879. end
  880. class << m; self; end.class_eval do
  881. define_method(:append_features) do |mod|
  882. t = mod
  883. super(mod)
  884. end
  885. end
  886. m2 = Module.new
  887. m2.module_eval { include(m) }
  888. assert_equal(m2, t)
  889. o = Object.new
  890. o.extend(m2)
  891. assert_equal(true, o.respond_to?(:foo))
  892. end
  893. def test_append_features_raise
  894. m = Module.new
  895. m.module_eval do
  896. def foo; :foo; end
  897. end
  898. class << m; self; end.class_eval do
  899. define_method(:append_features) {|mod| raise }
  900. end
  901. m2 = Module.new
  902. assert_raise(RuntimeError) do
  903. m2.module_eval { include(m) }
  904. end
  905. o = Object.new
  906. o.extend(m2)
  907. assert_equal(false, o.respond_to?(:foo))
  908. end
  909. def test_append_features_type_error
  910. assert_raise(TypeError) do
  911. Module.new.instance_eval { append_features(1) }
  912. end
  913. end
  914. def test_included
  915. m = Module.new
  916. m.module_eval do
  917. def foo; :foo; end
  918. end
  919. class << m; self; end.class_eval do
  920. define_method(:included) {|mod| raise }
  921. end
  922. m2 = Module.new
  923. assert_raise(RuntimeError) do
  924. m2.module_eval { include(m) }
  925. end
  926. o = Object.new
  927. o.extend(m2)
  928. assert_equal(true, o.respond_to?(:foo))
  929. end
  930. def test_cyclic_include
  931. m1 = Module.new
  932. m2 = Module.new
  933. m1.instance_eval { include(m2) }
  934. assert_raise(ArgumentError) do
  935. m2.instance_eval { include(m1) }
  936. end
  937. end
  938. def test_include_p
  939. m = Module.new
  940. c1 = Class.new
  941. c1.instance_eval { include(m) }
  942. c2 = Class.new(c1)
  943. assert_equal(true, c1.include?(m))
  944. assert_equal(true, c2.include?(m))
  945. assert_equal(false, m.include?(m))
  946. end
  947. def test_send
  948. a = AClass.new
  949. assert_equal(:aClass, a.__send__(:aClass))
  950. assert_equal(:aClass1, a.__send__(:aClass1))
  951. assert_equal(:aClass2, a.__send__(:aClass2))
  952. b = BClass.new
  953. assert_equal(:aClass, b.__send__(:aClass))
  954. assert_equal(:aClass1, b.__send__(:aClass1))
  955. assert_equal(:aClass2, b.__send__(:aClass2))
  956. assert_equal(:bClass1, b.__send__(:bClass1))
  957. assert_equal(:bClass2, b.__send__(:bClass2))
  958. assert_equal(:bClass3, b.__send__(:bClass3))
  959. end
  960. def test_nonascii_name
  961. c = eval("class ::C\u{df}; self; end")
  962. assert_equal("C\u{df}", c.name, '[ruby-core:24600]')
  963. c = eval("class C\u{df}; self; end")
  964. assert_equal("TestModule::C\u{df}", c.name, '[ruby-core:24600]')
  965. c = Module.new.module_eval("class X\u{df} < Module; self; end")
  966. assert_match(/::X\u{df}:/, c.new.to_s)
  967. end
  968. def test_method_added
  969. memo = []
  970. mod = Module.new do
  971. mod = self
  972. (class << self ; self ; end).class_eval do
  973. define_method :method_added do |sym|
  974. memo << sym
  975. memo << mod.instance_methods(false)
  976. memo << (mod.instance_method(sym) rescue nil)
  977. end
  978. end
  979. def f
  980. end
  981. alias g f
  982. attr_reader :a
  983. attr_writer :a
  984. end
  985. assert_equal :f, memo.shift
  986. assert_equal [:f], memo.shift, '[ruby-core:25536]'
  987. assert_equal mod.instance_method(:f), memo.shift
  988. assert_equal :g, memo.shift
  989. assert_equal [:f, :g].sort, memo.shift.sort
  990. assert_equal mod.instance_method(:f), memo.shift
  991. assert_equal :a, memo.shift
  992. assert_equal [:f, :g, :a].sort, memo.shift.sort
  993. assert_equal mod.instance_method(:a), memo.shift
  994. assert_equal :a=, memo.shift
  995. assert_equal [:f, :g, :a, :a=].sort, memo.shift.sort
  996. assert_equal mod.instance_method(:a=), memo.shift
  997. end
  998. def test_method_undefined
  999. added = []
  1000. undefed = []
  1001. removed = []
  1002. mod = Module.new do
  1003. mod = self
  1004. def f
  1005. end
  1006. (class << self ; self ; end).class_eval do
  1007. define_method :method_added do |sym|
  1008. added << sym
  1009. end
  1010. define_method :method_undefined do |sym|
  1011. undefed << sym
  1012. end
  1013. define_method :method_removed do |sym|
  1014. removed << sym
  1015. end
  1016. end
  1017. end
  1018. assert_method_defined?(mod, :f)
  1019. mod.module_eval do
  1020. undef :f
  1021. end
  1022. assert_equal [], added
  1023. assert_equal [:f], undefed
  1024. assert_equal [], removed
  1025. end
  1026. def test_method_removed
  1027. added = []
  1028. undefed = []
  1029. removed = []
  1030. mod = Module.new do
  1031. mod = self
  1032. def f
  1033. end
  1034. (class << self ; self ; end).class_eval do
  1035. define_method :method_added do |sym|
  1036. added << sym
  1037. end
  1038. define_method :method_undefined do |sym|
  1039. undefed << sym
  1040. end
  1041. define_method :method_removed do |sym|
  1042. removed << sym
  1043. end
  1044. end
  1045. end
  1046. assert_method_defined?(mod, :f)
  1047. mod.module_eval do
  1048. remove_method :f
  1049. end
  1050. assert_equal [], added
  1051. assert_equal [], undefed
  1052. assert_equal [:f], removed
  1053. end
  1054. def test_method_redefinition
  1055. feature2155 = '[ruby-dev:39400]'
  1056. line = __LINE__+4
  1057. stderr = EnvUtil.verbose_warning do
  1058. Module.new do
  1059. def foo; end
  1060. def foo; end
  1061. end
  1062. end
  1063. assert_match(/:#{line}: warning: method redefined; discarding old foo/, stderr)
  1064. assert_match(/:#{line-1}: warning: previous definition of foo/, stderr, feature2155)
  1065. assert_warning '' do
  1066. Module.new do
  1067. def foo; end
  1068. alias bar foo
  1069. def foo; end
  1070. end
  1071. end
  1072. assert_warning '' do
  1073. Module.new do
  1074. def foo; end
  1075. alias bar foo
  1076. alias bar foo
  1077. end
  1078. end
  1079. line = __LINE__+4
  1080. stderr = EnvUtil.verbose_warning do
  1081. Module.new do
  1082. define_method(:foo) do end
  1083. def foo; end
  1084. end
  1085. end
  1086. assert_match(/:#{line}: warning: method redefined; discarding old foo/, stderr)
  1087. assert_match(/:#{line-1}: warning: previous definition of foo/, stderr, feature2155)
  1088. assert_warning '' do
  1089. Module.new do
  1090. define_method(:foo) do end
  1091. alias bar foo
  1092. alias bar foo
  1093. end
  1094. end
  1095. assert_warning('', '[ruby-dev:39397]') do
  1096. Module.new do
  1097. module_function
  1098. def foo; end
  1099. module_function :foo
  1100. end
  1101. end
  1102. assert_warning '' do
  1103. Module.new do
  1104. def foo; end
  1105. undef foo
  1106. end
  1107. end
  1108. stderr = EnvUtil.verbose_warning do
  1109. Module.new do
  1110. def foo; end
  1111. mod = self
  1112. c = Class.new do
  1113. include mod
  1114. end
  1115. c.new.foo
  1116. def foo; end
  1117. end
  1118. end
  1119. assert_match(/: warning: method redefined; discarding old foo/, stderr)
  1120. assert_match(/: warning: previous definition of foo/, stderr)
  1121. end
  1122. def test_protected_singleton_method
  1123. klass = Class.new
  1124. x = klass.new
  1125. class << x
  1126. protected
  1127. def foo
  1128. end
  1129. end
  1130. assert_raise(NoMethodError) do
  1131. x.foo
  1132. end
  1133. klass.send(:define_method, :bar) do
  1134. x.foo
  1135. end
  1136. assert_nothing_raised do
  1137. x.bar
  1138. end
  1139. y = klass.new
  1140. assert_raise(NoMethodError) do
  1141. y.bar
  1142. end
  1143. end
  1144. def test_uninitialized_toplevel_constant
  1145. bug3123 = '[ruby-dev:40951]'
  1146. e = assert_raise(NameError) {eval("Bug3123", TOPLEVEL_BINDING)}
  1147. assert_not_match(/Object::/, e.message, bug3123)
  1148. end
  1149. def test_attr_inherited_visibility
  1150. bug3406 = '[ruby-core:30638]'
  1151. c = Class.new do
  1152. class << self
  1153. private
  1154. def attr_accessor(*); super; end
  1155. end
  1156. attr_accessor :x
  1157. end.new
  1158. assert_nothing_raised(bug3406) {c.x = 1}
  1159. assert_equal(1, c.x, bug3406)
  1160. end
  1161. def test_attr_writer_with_no_arguments
  1162. bug8540 = "[ruby-core:55543]"
  1163. c = Class.new do
  1164. attr_writer :foo
  1165. end
  1166. assert_raise(ArgumentError, bug8540) { c.new.send :foo= }
  1167. end
  1168. def test_private_constant
  1169. c = Class.new
  1170. c.const_set(:FOO, "foo")
  1171. assert_equal("foo", c::FOO)
  1172. c.private_constant(:FOO)
  1173. assert_raise(NameError) { c::FOO }
  1174. assert_equal("foo", c.class_eval("FOO"))
  1175. assert_equal("foo", c.const_get("FOO"))
  1176. $VERBOSE, verbose = nil, $VERBOSE
  1177. c.const_set(:FOO, "foo")
  1178. $VERBOSE = verbose
  1179. assert_raise(NameError) { c::FOO }
  1180. end
  1181. def test_private_constant2
  1182. c = Class.new
  1183. c.const_set(:FOO, "foo")
  1184. c.const_set(:BAR, "bar")
  1185. assert_equal("foo", c::FOO)
  1186. assert_equal("bar", c::BAR)
  1187. c.private_constant(:FOO, :BAR)
  1188. assert_raise(NameError) { c::FOO }
  1189. assert_raise(NameError) { c::BAR }
  1190. assert_equal("foo", c.class_eval("FOO"))
  1191. assert_equal("bar", c.class_eval("BAR"))
  1192. end
  1193. def test_private_constant_with_no_args
  1194. assert_in_out_err([], <<-RUBY, [], ["-:3: warning: private_constant with no argument is just ignored"])
  1195. $-w = true
  1196. class X
  1197. private_constant
  1198. end
  1199. RUBY
  1200. end
  1201. class PrivateClass
  1202. end
  1203. private_constant :PrivateClass
  1204. def test_define_module_under_private_constant
  1205. assert_raise(NameError) do
  1206. eval %q{class TestModule::PrivateClass; end}
  1207. end
  1208. assert_raise(NameError) do
  1209. eval %q{module TestModule::PrivateClass::TestModule; end}
  1210. end
  1211. eval %q{class PrivateClass; end}
  1212. eval %q{module PrivateClass::TestModule; end}
  1213. assert_instance_of(Module, PrivateClass::TestModule)
  1214. PrivateClass.class_eval { remove_const(:TestModule) }
  1215. end
  1216. def test_public_constant
  1217. c = Class.new
  1218. c.const_set(:FOO, "foo")
  1219. assert_equal("foo", c::FOO)
  1220. c.private_constant(:FOO)
  1221. assert_raise(NameError) { c::FOO }
  1222. assert_equal("foo", c.class_eval("FOO"))
  1223. c.public_constant(:FOO)
  1224. assert_equal("foo", c::FOO)
  1225. end
  1226. def test_deprecate_constant
  1227. c = Class.new
  1228. c.const_set(:FOO, "foo")
  1229. c.deprecate_constant(:FOO)
  1230. assert_warn(/deprecated/) {c::FOO}
  1231. end
  1232. def test_constants_with_private_constant
  1233. assert_not_include(::TestModule.constants, :PrivateClass)
  1234. end
  1235. def test_toplevel_private_constant
  1236. src = <<-INPUT
  1237. class Object
  1238. private_constant :Object
  1239. end
  1240. p Object
  1241. begin
  1242. p ::Object
  1243. rescue
  1244. p :ok
  1245. end
  1246. INPUT
  1247. assert_in_out_err([], src, %w(Object :ok), [])
  1248. end
  1249. def test_private_constants_clear_inlinecache
  1250. bug5702 = '[ruby-dev:44929]'
  1251. src = <<-INPUT
  1252. class A
  1253. C = :Const
  1254. def self.get_C
  1255. A::C
  1256. end
  1257. # fill cache
  1258. A.get_C
  1259. private_constant :C, :D rescue nil
  1260. begin
  1261. A.get_C
  1262. rescue NameError
  1263. puts "A.get_C"
  1264. end
  1265. end
  1266. INPUT
  1267. assert_in_out_err([], src, %w(A.get_C), [], bug5702)
  1268. end
  1269. def test_constant_lookup_in_method_defined_by_class_eval
  1270. src = <<-INPUT
  1271. class A
  1272. B = 42
  1273. end
  1274. A.class_eval do
  1275. def self.f
  1276. B
  1277. end
  1278. def f
  1279. B
  1280. end
  1281. end
  1282. begin
  1283. A.f
  1284. rescue NameError
  1285. puts "A.f"
  1286. end
  1287. begin
  1288. A.new.f
  1289. rescue NameError
  1290. puts "A.new.f"
  1291. end
  1292. INPUT
  1293. assert_in_out_err([], src, %w(A.f A.new.f), [])
  1294. end
  1295. def test_constant_lookup_in_toplevel_class_eval
  1296. src = <<-INPUT
  1297. module X
  1298. A = 123
  1299. end
  1300. begin
  1301. X.class_eval { A }
  1302. rescue NameError => e
  1303. puts e
  1304. end
  1305. INPUT
  1306. assert_in_out_err([], src, ["uninitialized constant A"], [])
  1307. end
  1308. def test_constant_lookup_in_module_in_class_eval
  1309. src = <<-INPUT
  1310. class A
  1311. B = 42
  1312. end
  1313. A.class_eval do
  1314. module C
  1315. begin
  1316. B
  1317. rescue NameError
  1318. puts "NameError"
  1319. end
  1320. end
  1321. end
  1322. INPUT
  1323. assert_in_out_err([], src, ["NameError"], [])
  1324. end
  1325. module M0
  1326. def m1; [:M0] end
  1327. end
  1328. module M1
  1329. def m1; [:M1, *super] end
  1330. end
  1331. module M2
  1332. def m1; [:M2, *super] end
  1333. end
  1334. M3 = Module.new do
  1335. def m1; [:M3, *super] end
  1336. end
  1337. module M4
  1338. def m1; [:M4, *super] end
  1339. end
  1340. class C
  1341. def m1; end
  1342. end
  1343. class C0 < C
  1344. include M0
  1345. prepend M1
  1346. def m1; [:C0, *super] end
  1347. end
  1348. class C1 < C0
  1349. prepend M2, M3
  1350. include M4
  1351. def m1; [:C1, *super] end
  1352. end
  1353. def test_prepend
  1354. obj = C0.new
  1355. expected = [:M1,:C0,:M0]
  1356. assert_equal(expected, obj.m1)
  1357. obj = C1.new
  1358. expected = [:M2,:M3,:C1,:M4,:M1,:C0,:M0]
  1359. assert_equal(expected, obj.m1)
  1360. end
  1361. def test_public_prepend
  1362. assert_nothing_raised('#8846') do
  1363. Class.new.prepend(Module.new)
  1364. end
  1365. end
  1366. def test_prepend_inheritance
  1367. bug6654 = '[ruby-core:45914]'
  1368. a = labeled_module("a")
  1369. b = labeled_module("b") {include a}
  1370. c = labeled_class("c") {prepend b}
  1371. assert_operator(c, :<, b, bug6654)
  1372. assert_operator(c, :<, a, bug6654)
  1373. bug8357 = '[ruby-core:54736] [Bug #8357]'
  1374. b = labeled_module("b") {prepend a}
  1375. c = labeled_class("c") {include b}
  1376. assert_operator(c, :<, b, bug8357)
  1377. assert_operator(c, :<, a, bug8357)
  1378. bug8357 = '[ruby-core:54742] [Bug #8357]'
  1379. assert_kind_of(b, c.new, bug8357)
  1380. end
  1381. def test_prepend_instance_methods
  1382. bug6655 = '[ruby-core:45915]'
  1383. assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655)
  1384. end
  1385. def test_prepend_singleton_methods
  1386. o = Object.new
  1387. o.singleton_class.class_eval {prepend Module.new}
  1388. assert_equal([], o.singleton_methods)
  1389. end
  1390. def test_prepend_remove_method
  1391. c = Class.new do
  1392. prepend Module.new {def foo; end}
  1393. end
  1394. assert_raise(NameError) do
  1395. c.class_eval do
  1396. remove_method(:foo)
  1397. end
  1398. end
  1399. c.class_eval do
  1400. def foo; end
  1401. end
  1402. removed = nil
  1403. c.singleton_class.class_eval do
  1404. define_method(:method_removed) {|id| removed = id}
  1405. end
  1406. assert_nothing_raised(NoMethodError, NameError, '[Bug #7843]') do
  1407. c.class_eval do
  1408. remove_method(:foo)
  1409. end
  1410. end
  1411. assert_equal(:foo, removed)
  1412. end
  1413. def test_prepend_class_ancestors
  1414. bug6658 = '[ruby-core:45919]'
  1415. m = labeled_module("m")
  1416. c = labeled_class("c") {prepend m}
  1417. assert_equal([m, c], c.ancestors[0, 2], bug6658)
  1418. bug6662 = '[ruby-dev:45868]'
  1419. c2 = labeled_class("c2", c)
  1420. anc = c2.ancestors
  1421. assert_equal([c2, m, c, Object], anc[0..anc.index(Object)], bug6662)
  1422. end
  1423. def test_prepend_module_ancestors
  1424. bug6659 = '[ruby-dev:45861]'
  1425. m0 = labeled_module("m0") {def x; [:m0, *super] end}
  1426. m1 = labeled_module("m1") {def x; [:m1, *super] end; prepend m0}
  1427. m2 = labeled_module("m2") {def x; [:m2, *super] end; prepend m1}
  1428. c0 = labeled_class("c0") {def x; [:c0] end}
  1429. c1 = labeled_class("c1") {def x; [:c1] end; prepend m2}
  1430. c2 = labeled_class("c2", c0) {def x; [:c2, *super] end; include m2}
  1431. assert_equal([m0, m1], m1.ancestors, bug6659)
  1432. bug6662 = '[ruby-dev:45868]'
  1433. assert_equal([m0, m1, m2], m2.ancestors, bug6662)
  1434. assert_equal([m0, m1, m2, c1], c1.ancestors[0, 4], bug6662)
  1435. assert_equal([:m0, :m1, :m2, :c1], c1.new.x)
  1436. assert_equal([c2, m0, m1, m2, c0], c2.ancestors[0, 5], bug6662)
  1437. assert_equal([:c2, :m0, :m1, :m2, :c0], c2.new.x)
  1438. m3 = labeled_module("m3") {include m1; prepend m1}
  1439. assert_equal([m3, m0, m1], m3.ancestors)
  1440. m3 = labeled_module("m3") {prepend m1; include m1}
  1441. assert_equal([m0, m1, m3], m3.ancestors)
  1442. m3 = labeled_module("m3") {prepend m1; prepend m1}
  1443. assert_equal([m0, m1, m3], m3.ancestors)
  1444. m3 = labeled_module("m3") {include m1; include m1}
  1445. assert_equal([m3, m0, m1], m3.ancestors)
  1446. end
  1447. def labeled_module(name, &block)
  1448. EnvUtil.labeled_module(name, &block)
  1449. end
  1450. def labeled_class(name, superclass = Object, &block)
  1451. EnvUtil.labeled_class(name, superclass, &block)
  1452. end
  1453. def test_prepend_instance_methods_false
  1454. bug6660 = '[ruby-dev:45863]'
  1455. assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660)
  1456. assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660)
  1457. end
  1458. def test_cyclic_prepend
  1459. bug7841 = '[ruby-core:52205] [Bug #7841]'
  1460. m1 = Module.new
  1461. m2 = Module.new
  1462. m1.instance_eval { prepend(m2) }
  1463. assert_raise(ArgumentError, bug7841) do
  1464. m2.instance_eval { prepend(m1) }
  1465. end
  1466. end
  1467. def test_prepend_optmethod
  1468. bug7983 = '[ruby-dev:47124] [Bug #7983]'
  1469. assert_separately [], %{
  1470. module M
  1471. def /(other)
  1472. to_f / other
  1473. end
  1474. end
  1475. Fixnum.send(:prepend, M)
  1476. assert_equal(0.5, 1 / 2, "#{bug7983}")
  1477. }
  1478. assert_equal(0, 1 / 2)
  1479. end
  1480. def test_redefine_optmethod_after_prepend
  1481. bug11826 = '[ruby-core:72188] [Bug #11826]'
  1482. assert_separately [], %{
  1483. module M
  1484. end
  1485. class Fixnum
  1486. prepend M
  1487. def /(other)
  1488. quo(other)
  1489. end
  1490. end
  1491. assert_equal(1 / 2r, 1 / 2, "#{bug11826}")
  1492. }, ignore_stderr: true
  1493. assert_equal(0, 1 / 2)
  1494. end
  1495. def test_override_optmethod_after_prepend
  1496. bug11836 = '[ruby-core:72226] [Bug #11836]'
  1497. assert_separately [], %{
  1498. module M
  1499. end
  1500. class Fixnum
  1501. prepend M
  1502. end
  1503. module M
  1504. def /(other)
  1505. quo(other)
  1506. end
  1507. end
  1508. assert_equal(1 / 2r, 1 / 2, "#{bug11836}")
  1509. }, ignore_stderr: true
  1510. assert_equal(0, 1 / 2)
  1511. end
  1512. def test_prepend_visibility
  1513. bug8005 = '[ruby-core:53106] [Bug #8005]'
  1514. c = Class.new do
  1515. prepend Module.new {}
  1516. def foo() end
  1517. protected :foo
  1518. end
  1519. a = c.new
  1520. assert_respond_to a, [:foo, true], bug8005
  1521. assert_nothing_raised(NoMethodError, bug8005) {a.send :foo}
  1522. end
  1523. def test_prepend_visibility_inherited
  1524. bug8238 = '[ruby-core:54105] [Bug #8238]'
  1525. assert_separately [], <<-"end;", timeout: 20
  1526. class A
  1527. def foo() A; end
  1528. private :foo
  1529. end
  1530. class B < A
  1531. public :foo
  1532. prepend Module.new
  1533. end
  1534. assert_equal(A, B.new.foo, "#{bug8238}")
  1535. end;
  1536. end
  1537. def test_prepend_included_modules
  1538. bug8025 = '[ruby-core:53158] [Bug #8025]'
  1539. mixin = labeled_module("mixin")
  1540. c = labeled_module("c") {prepend mixin}
  1541. im = c.included_modules
  1542. assert_not_include(im, c, bug8025)
  1543. assert_include(im, mixin, bug8025)
  1544. c1 = labeled_class("c1") {prepend mixin}
  1545. c2 = labeled_class("c2", c1)
  1546. im = c2.included_modules
  1547. assert_not_include(im, c1, bug8025)
  1548. assert_not_include(im, c2, bug8025)
  1549. assert_include(im, mixin, bug8025)
  1550. end
  1551. def test_prepend_super_in_alias
  1552. bug7842 = '[Bug #7842]'
  1553. p = labeled_module("P") do
  1554. def m; "P"+super; end
  1555. end
  1556. a = labeled_class("A") do
  1557. def m; "A"; end
  1558. end
  1559. b = labeled_class("B", a) do
  1560. def m; "B"+super; end
  1561. alias m2 m
  1562. prepend p
  1563. alias m3 m
  1564. end
  1565. assert_equal("BA", b.new.m2, bug7842)
  1566. assert_equal("PBA", b.new.m3, bug7842)
  1567. end
  1568. def test_include_super_in_alias
  1569. bug9236 = '[Bug #9236]'
  1570. fun = labeled_module("Fun") do
  1571. def hello
  1572. orig_hello
  1573. end
  1574. end
  1575. m1 = labeled_module("M1") do
  1576. def hello
  1577. 'hello!'
  1578. end
  1579. end
  1580. m2 = labeled_module("M2") do
  1581. def hello
  1582. super
  1583. end
  1584. end
  1585. foo = labeled_class("Foo") do
  1586. include m1
  1587. include m2
  1588. alias orig_hello hello
  1589. include fun
  1590. end
  1591. assert_equal('hello!', foo.new.hello, bug9236)
  1592. end
  1593. def test_prepend_each_classes
  1594. m = labeled_module("M")
  1595. c1 = labeled_class("C1") {prepend m}
  1596. c2 = labeled_class("C2", c1) {prepend m}
  1597. assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should be able to prepend each classes")
  1598. end
  1599. def test_prepend_no_duplication
  1600. m = labeled_module("M")
  1601. c = labeled_class("C") {prepend m; prepend m}
  1602. assert_equal([m, c], c.ancestors[0, 2], "should never duplicate")
  1603. end
  1604. def test_prepend_in_superclass
  1605. m = labeled_module("M")
  1606. c1 = labeled_class("C1")
  1607. c2 = labeled_class("C2", c1) {prepend m}
  1608. c1.class_eval {prepend m}
  1609. assert_equal([m, c2, m, c1], c2.ancestors[0, 4], "should accesisble prepended module in superclass")
  1610. end
  1611. def test_prepend_call_super
  1612. assert_separately([], <<-'end;') #do
  1613. bug10847 = '[ruby-core:68093] [Bug #10847]'
  1614. module M; end
  1615. Float.prepend M
  1616. assert_nothing_raised(SystemStackError, bug10847) do
  1617. 0.3.numerator
  1618. end
  1619. end;
  1620. end
  1621. def test_class_variables
  1622. m = Module.new
  1623. m.class_variable_set(:@@foo, 1)
  1624. m2 = Module.new
  1625. m2.send(:include, m)
  1626. m2.class_variable_set(:@@bar, 2)
  1627. assert_equal([:@@foo], m.class_variables)
  1628. assert_equal([:@@bar, :@@foo], m2.class_variables.sort)
  1629. assert_equal([:@@bar, :@@foo], m2.class_variables(true).sort)
  1630. assert_equal([:@@bar], m2.class_variables(false))
  1631. end
  1632. Bug6891 = '[ruby-core:47241]'
  1633. def test_extend_module_with_protected_method
  1634. list = []
  1635. x = Class.new {
  1636. @list = list
  1637. extend Module.new {
  1638. protected
  1639. def inherited(klass)
  1640. @list << "protected"
  1641. super(klass)
  1642. end
  1643. }
  1644. extend Module.new {
  1645. def inherited(klass)
  1646. @list << "public"
  1647. super(klass)
  1648. end
  1649. }
  1650. }
  1651. assert_nothing_raised(NoMethodError, Bug6891) {Class.new(x)}
  1652. assert_equal(['public', 'protected'], list)
  1653. end
  1654. def test_extend_module_with_protected_bmethod
  1655. list = []
  1656. x = Class.new {
  1657. extend Module.new {
  1658. protected
  1659. define_method(:inherited) do |klass|
  1660. list << "protected"
  1661. super(klass)
  1662. end
  1663. }
  1664. extend Module.new {
  1665. define_method(:inherited) do |klass|
  1666. list << "public"
  1667. super(klass)
  1668. end
  1669. }
  1670. }
  1671. assert_nothing_raised(NoMethodError, Bug6891) {Class.new(x)}
  1672. assert_equal(['public', 'protected'], list)
  1673. end
  1674. def test_invalid_attr
  1675. %W[
  1676. foo?
  1677. @foo
  1678. @@foo
  1679. $foo
  1680. \u3042$
  1681. ].each do |name|
  1682. assert_raise_with_message(NameError, /#{Regexp.quote(quote(name))}/) do
  1683. Module.new { attr_accessor name.to_sym }
  1684. end
  1685. end
  1686. end
  1687. private def quote(name)
  1688. encoding = Encoding.default_internal || Encoding.default_external
  1689. (name.encoding == encoding || name.ascii_only?) ? name : name.inspect
  1690. end
  1691. class AttrTest
  1692. class << self
  1693. attr_accessor :cattr
  1694. end
  1695. attr_accessor :iattr
  1696. d

Large files files are truncated, but you can click here to view the full file