Changeset 439 for EcnlProtoTool/trunk/mruby-2.1.1/test/t/module.rb
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-2.1.1
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-2.1.1/test/t/module.rb
r331 r439 4 4 def labeled_module(name, &block) 5 5 Module.new do 6 singleton_class.class_eval do6 (class <<self; self end).class_eval do 7 7 define_method(:to_s) { name } 8 8 alias_method :inspect, :to_s … … 14 14 def labeled_class(name, supklass = Object, &block) 15 15 Class.new(supklass) do 16 singleton_class.class_eval do16 (class <<self; self end).class_eval do 17 17 define_method(:to_s) { name } 18 18 alias_method :inspect, :to_s … … 22 22 end 23 23 24 def assert_uninitialized_const(&block) 25 assert_raise_with_message_pattern(NameError, "uninitialized constant *", &block) 26 end 27 28 def assert_wrong_const_name(&block) 29 assert_raise_with_message_pattern(NameError, "wrong constant name *", &block) 30 end 31 24 32 assert('Module', '15.2.2') do 25 33 assert_equal Class, Module.class 26 34 end 27 35 36 assert('Module#alias_method', '15.2.2.4.8') do 37 cls = Class.new do 38 def foo 39 "FOO" 40 end 41 end 42 43 assert_same(cls, cls.alias_method(:bar, :foo)) 44 assert_equal("FOO", cls.new.bar) 45 end 46 28 47 # TODO not implemented ATM assert('Module.constants', '15.2.2.3.1') do 29 30 # TODO not implemented ATM assert('Module.nesting', '15.2.2.3.2') do31 48 32 49 assert('Module#ancestors', '15.2.2.4.9') do 33 50 class Test4ModuleAncestors 34 51 end 35 sc = Test4ModuleAncestors.singleton_class36 52 r = String.ancestors 37 53 … … 52 68 53 69 assert_equal Test4AppendFeatures2, Test4AppendFeatures2.const_get(:Const4AppendFeatures2) 70 assert_raise(FrozenError) { Module.new.append_features Class.new.freeze } 54 71 end 55 72 … … 202 219 end 203 220 end 204 r = Test4ClassEval.instance_methods205 206 221 assert_equal 11, Test4ClassEval.class_eval{ @a } 207 222 assert_equal 12, Test4ClassEval.class_eval{ @b } 208 assert_equal Array, r.class 209 assert_true r.include?(:method1) 210 end 211 212 assert('Module#class_variable_defined?', '15.2.2.4.16') do 213 class Test4ClassVariableDefined 214 @@cv = 99 215 end 216 217 assert_true Test4ClassVariableDefined.class_variable_defined?(:@@cv) 218 assert_false Test4ClassVariableDefined.class_variable_defined?(:@@noexisting) 219 end 220 221 assert('Module#class_variable_get', '15.2.2.4.17') do 222 class Test4ClassVariableGet 223 @@cv = 99 224 end 225 226 assert_equal 99, Test4ClassVariableGet.class_variable_get(:@@cv) 227 end 228 229 assert('Module#class_variable_set', '15.2.2.4.18') do 230 class Test4ClassVariableSet 231 @@foo = 100 232 def foo 233 @@foo 234 end 235 end 236 237 assert_true Test4ClassVariableSet.class_variable_set(:@@cv, 99) 238 assert_true Test4ClassVariableSet.class_variable_set(:@@foo, 101) 239 assert_true Test4ClassVariableSet.class_variables.include? :@@cv 240 assert_equal 99, Test4ClassVariableSet.class_variable_get(:@@cv) 241 assert_equal 101, Test4ClassVariableSet.new.foo 242 end 243 244 assert('Module#class_variables', '15.2.2.4.19') do 245 class Test4ClassVariables1 246 @@var1 = 1 247 end 248 class Test4ClassVariables2 < Test4ClassVariables1 249 @@var2 = 2 250 end 251 252 assert_equal [:@@var1], Test4ClassVariables1.class_variables 253 assert_equal [:@@var2, :@@var1], Test4ClassVariables2.class_variables 223 assert_equal true, Test4ClassEval.new.respond_to?(:method1) 254 224 end 255 225 … … 261 231 assert_true Test4ConstDefined.const_defined?(:Const4Test4ConstDefined) 262 232 assert_false Test4ConstDefined.const_defined?(:NotExisting) 233 assert_wrong_const_name{ Test4ConstDefined.const_defined?(:wrong_name) } 263 234 end 264 235 … … 269 240 270 241 assert_equal 42, Test4ConstGet.const_get(:Const4Test4ConstGet) 242 assert_equal 42, Test4ConstGet.const_get("Const4Test4ConstGet") 243 assert_equal 42, Object.const_get("Test4ConstGet::Const4Test4ConstGet") 244 245 assert_raise(TypeError){ Test4ConstGet.const_get(123) } 246 assert_uninitialized_const{ Test4ConstGet.const_get(:I_DO_NOT_EXIST) } 247 assert_uninitialized_const{ Test4ConstGet.const_get("I_DO_NOT_EXIST::ME_NEITHER") } 248 assert_wrong_const_name{ Test4ConstGet.const_get(:wrong_name) } 249 end 250 251 assert('Module#const_set', '15.2.2.4.23') do 252 module Test4ConstSet 253 Const4Test4ConstSet = 42 254 end 255 256 assert_equal 23, Test4ConstSet.const_set(:Const4Test4ConstSet, 23) 257 assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet) 258 ["", "wrongNAME", "Wrong-Name"].each do |n| 259 assert_wrong_const_name { Test4ConstSet.const_set(n, 1) } 260 end 261 end 262 263 assert('Module#remove_const', '15.2.2.4.40') do 264 module Test4RemoveConst 265 ExistingConst = 23 266 end 267 268 assert_equal 23, Test4RemoveConst.remove_const(:ExistingConst) 269 assert_false Test4RemoveConst.const_defined?(:ExistingConst) 270 assert_raise_with_message_pattern(NameError, "constant * not defined") do 271 Test4RemoveConst.remove_const(:NonExistingConst) 272 end 273 %i[x X!].each do |n| 274 assert_wrong_const_name { Test4RemoveConst.remove_const(n) } 275 end 276 assert_raise(FrozenError) { Test4RemoveConst.freeze.remove_const(:A) } 271 277 end 272 278 … … 281 287 end 282 288 283 assert('Module#const_get', '15.2.2.4.23') do 284 module Test4ConstSet 285 Const4Test4ConstSet = 42 286 end 287 288 assert_true Test4ConstSet.const_set(:Const4Test4ConstSet, 23) 289 assert_equal 23, Test4ConstSet.const_get(:Const4Test4ConstSet) 290 end 291 292 assert('Module#constants', '15.2.2.4.24') do 293 $n = [] 294 module TestA 295 C = 1 296 end 297 class TestB 298 include TestA 299 C2 = 1 300 $n = constants.sort 301 end 302 303 assert_equal [ :C ], TestA.constants 304 assert_equal [ :C, :C2 ], $n 289 assert('Module#extend_object', '15.2.2.4.25') do 290 cls = Class.new 291 mod = Module.new { def foo; end } 292 a = cls.new 293 b = cls.new 294 mod.extend_object(b) 295 assert_false a.respond_to?(:foo) 296 assert_true b.respond_to?(:foo) 297 assert_raise(FrozenError) { mod.extend_object(cls.new.freeze) } 298 assert_raise(FrozenError, TypeError) { mod.extend_object(1) } 305 299 end 306 300 … … 310 304 end 311 305 module Test4Include2 312 include Test4Include 306 @include_result = include Test4Include 307 class << self 308 attr_reader :include_result 309 end 313 310 end 314 311 315 312 assert_equal 42, Test4Include2.const_get(:Const4Include) 313 assert_equal Test4Include2, Test4Include2.include_result 314 assert_raise(FrozenError) { Module.new.freeze.include Test4Include } 316 315 end 317 316 … … 345 344 end 346 345 347 assert('Module#included_modules', '15.2.2.4.30') do348 module Test4includedModules349 end350 module Test4includedModules2351 include Test4includedModules352 end353 r = Test4includedModules2.included_modules354 355 assert_equal Array, r.class356 assert_true r.include?(Test4includedModules)357 end358 359 346 assert('Module#initialize', '15.2.2.4.31') do 360 347 assert_kind_of Module, Module.new 361 348 mod = Module.new { def hello; "hello"; end } 362 assert_equal [:hello], mod.instance_methods 349 cls = Class.new{include mod} 350 assert_true cls.new.respond_to?(:hello) 363 351 a = nil 364 352 mod = Module.new { |m| a = m } 365 353 assert_equal mod, a 366 end367 368 assert('Module#instance_methods', '15.2.2.4.33') do369 module Test4InstanceMethodsA370 def method1() end371 end372 class Test4InstanceMethodsB373 def method2() end374 end375 class Test4InstanceMethodsC < Test4InstanceMethodsB376 def method3() end377 end378 379 r = Test4InstanceMethodsC.instance_methods(true)380 381 assert_equal [:method1], Test4InstanceMethodsA.instance_methods382 assert_equal [:method2], Test4InstanceMethodsB.instance_methods(false)383 assert_equal [:method3], Test4InstanceMethodsC.instance_methods(false)384 assert_equal Array, r.class385 assert_true r.include?(:method3)386 assert_true r.include?(:method2)387 354 end 388 355 … … 410 377 end 411 378 412 413 379 assert('Module#module_eval', '15.2.2.4.35') do 414 380 module Test4ModuleEval … … 419 385 assert_equal 11, Test4ModuleEval.module_eval{ @a } 420 386 assert_equal 12, Test4ModuleEval.module_eval{ @b } 421 end422 423 assert('Module#remove_class_variable', '15.2.2.4.39') do424 class Test4RemoveClassVariable425 @@cv = 99426 end427 428 assert_equal 99, Test4RemoveClassVariable.remove_class_variable(:@@cv)429 assert_false Test4RemoveClassVariable.class_variables.include? :@@cv430 end431 432 assert('Module#remove_const', '15.2.2.4.40') do433 module Test4RemoveConst434 ExistingConst = 23435 end436 437 result = Test4RemoveConst.module_eval { remove_const :ExistingConst }438 439 name_error = false440 begin441 Test4RemoveConst.module_eval { remove_const :NonExistingConst }442 rescue NameError443 name_error = true444 end445 446 # Constant removed from Module447 assert_false Test4RemoveConst.const_defined? :ExistingConst448 # Return value of binding449 assert_equal 23, result450 # Name Error raised when Constant doesn't exist451 assert_true name_error452 end453 454 assert('Module#remove_method', '15.2.2.4.41') do455 module Test4RemoveMethod456 class Parent457 def hello458 end459 end460 461 class Child < Parent462 def hello463 end464 end465 end466 467 assert_true Test4RemoveMethod::Child.class_eval{ remove_method :hello }468 assert_true Test4RemoveMethod::Child.instance_methods.include? :hello469 assert_false Test4RemoveMethod::Child.instance_methods(false).include? :hello470 387 end 471 388 … … 490 407 assert_false Test4UndefMethod::Child.new.respond_to?(:hello) 491 408 assert_false Test4UndefMethod::GrandChild.new.respond_to?(:hello) 492 assert_false Test4UndefMethod::Child.instance_methods(false).include? :hello493 409 end 494 410 495 411 # Not ISO specified 412 413 assert('Module#dup') do 414 module TestModuleDup 415 @@cvar = :cvar 416 class << self 417 attr_accessor :cattr 418 def cmeth; :cmeth end 419 end 420 def cvar; @@cvar end 421 def imeth; :imeth end 422 self.cattr = :cattr 423 end 424 425 m = TestModuleDup.dup 426 o = Object.include(m).new 427 assert_equal(:cattr, m.cattr) 428 assert_equal(:cmeth, m.cmeth) 429 assert_equal(:cvar, o.cvar) 430 assert_equal(:imeth, o.imeth) 431 assert_match("#<Module:0x*>", m.to_s) 432 assert_not_predicate(m, :frozen?) 433 assert_not_predicate(TestModuleDup.freeze.dup, :frozen?) 434 end 496 435 497 436 assert('Module#define_method') do … … 507 446 end 508 447 448 assert 'Module#prepend_features' do 449 mod = Module.new { def m; :mod end } 450 cls = Class.new { def m; :cls end } 451 assert_equal :cls, cls.new.m 452 mod.prepend_features(cls) 453 assert_equal :mod, cls.new.m 454 assert_raise(FrozenError) { Module.new.prepend_features(Class.new.freeze) } 455 end 456 509 457 # @!group prepend 510 458 assert('Module#prepend') do … … 539 487 expected = [:M2,[:M3,[:C1,[:M4,[:M1,[:C0,[:M0],:C0],:M1],:M4],:C1],:M3],:M2] 540 488 assert_equal(expected, obj.m1) 489 end 490 491 assert('Module#prepend result') do 492 module TestPrepended; end 493 module TestPrependResult 494 @prepend_result = prepend TestPrepended 495 class << self 496 attr_reader :prepend_result 497 end 498 end 499 500 assert_equal TestPrependResult, TestPrependResult.prepend_result 541 501 end 542 502 … … 573 533 bug8357 = '[ruby-core:54742] [Bug #8357]' 574 534 assert_kind_of(b, c.new, bug8357) 575 end576 577 assert('Moduler#prepend + #instance_methods') do578 bug6655 = '[ruby-core:45915]'579 assert_equal(Object.instance_methods, Class.new {prepend Module.new}.instance_methods, bug6655)580 end581 582 assert 'Module#prepend + #singleton_methods' do583 o = Object.new584 o.singleton_class.class_eval {prepend Module.new}585 assert_equal([], o.singleton_methods)586 end587 588 assert 'Module#prepend + #remove_method' do589 c = Class.new do590 prepend Module.new { def foo; end }591 end592 assert_raise(NameError) do593 c.class_eval do594 remove_method(:foo)595 end596 end597 c.class_eval do598 def foo; end599 end600 removed = nil601 c.singleton_class.class_eval do602 define_method(:method_removed) {|id| removed = id}603 end604 assert_nothing_raised(NoMethodError, NameError, '[Bug #7843]') do605 c.class_eval do606 remove_method(:foo)607 end608 end609 assert_equal(:foo, removed)610 535 end 611 536 … … 650 575 end 651 576 652 assert 'Module#prepend #instance_methods(false)' do653 bug6660 = '[ruby-dev:45863]'654 assert_equal([:m1], Class.new{ prepend Module.new; def m1; end }.instance_methods(false), bug6660)655 assert_equal([:m1], Class.new(Class.new{def m2;end}){ prepend Module.new; def m1; end }.instance_methods(false), bug6660)656 end657 658 577 assert 'cyclic Module#prepend' do 659 578 bug7841 = '[ruby-core:52205] [Bug #7841]' … … 666 585 end 667 586 668 # these assertions will not run without a #assert_sep erately method587 # these assertions will not run without a #assert_separately method 669 588 #assert 'test_prepend_optmethod' do 670 589 # bug7983 = '[ruby-dev:47124] [Bug #7983]' … … 682 601 683 602 # mruby has no visibility control 684 assert 'Module#prepend visibility' do685 bug8005 = '[ruby-core:53106] [Bug #8005]'686 c = Class.new do687 prepend Module.new {}688 def foo() end689 protected :foo690 end691 a = c.new692 assert_true a.respond_to?(:foo), bug8005693 assert_nothing_raised(NoMethodError,bug8005) {a.send :foo}694 end603 # assert 'Module#prepend visibility' do 604 # bug8005 = '[ruby-core:53106] [Bug #8005]' 605 # c = Class.new do 606 # prepend Module.new {} 607 # def foo() end 608 # protected :foo 609 # end 610 # a = c.new 611 # assert_true a.respond_to?(:foo), bug8005 612 # assert_nothing_raised(bug8005) {a.send :foo} 613 # end 695 614 696 615 # mruby has no visibility control 697 assert 'Module#prepend inherited visibility' do 698 bug8238 = '[ruby-core:54105] [Bug #8238]' 699 module Test4PrependVisibilityInherited 700 class A 701 def foo() A; end 702 private :foo 703 end 704 class B < A 705 public :foo 706 prepend Module.new 707 end 708 end 709 assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}") 710 end 711 712 assert 'Module#prepend + #included_modules' do 713 bug8025 = '[ruby-core:53158] [Bug #8025]' 714 mixin = labeled_module("mixin") 715 c = labeled_module("c") {prepend mixin} 716 im = c.included_modules 717 assert_not_include(im, c, bug8025) 718 assert_include(im, mixin, bug8025) 719 c1 = labeled_class("c1") {prepend mixin} 720 c2 = labeled_class("c2", c1) 721 im = c2.included_modules 722 assert_not_include(im, c1, bug8025) 723 assert_not_include(im, c2, bug8025) 724 assert_include(im, mixin, bug8025) 725 end 726 727 assert 'Module#prepend super in alias' do 728 skip "super does not currently work in aliased methods" 729 bug7842 = '[Bug #7842]' 730 731 p = labeled_module("P") do 732 def m; "P"+super; end 733 end 734 735 a = labeled_class("A") do 736 def m; "A"; end 737 end 738 739 b = labeled_class("B", a) do 740 def m; "B"+super; end 741 alias m2 m 742 prepend p 743 alias m3 m 744 end 745 746 assert_nothing_raised do 747 assert_equal("BA", b.new.m2, bug7842) 748 end 749 750 assert_nothing_raised do 751 assert_equal("PBA", b.new.m3, bug7842) 752 end 753 end 616 # assert 'Module#prepend inherited visibility' do 617 # bug8238 = '[ruby-core:54105] [Bug #8238]' 618 # module Test4PrependVisibilityInherited 619 # class A 620 # def foo() A; end 621 # private :foo 622 # end 623 # class B < A 624 # public :foo 625 # prepend Module.new 626 # end 627 # end 628 # assert_equal(Test4PrependVisibilityInherited::A, Test4PrependVisibilityInherited::B.new.foo, "#{bug8238}") 629 # end 630 631 # assert 'Module#prepend super in alias' do 632 # skip "super does not currently work in aliased methods" 633 # bug7842 = '[Bug #7842]' 634 635 # p = labeled_module("P") do 636 # def m; "P"+super; end 637 # end 638 639 # a = labeled_class("A") do 640 # def m; "A"; end 641 # end 642 643 # b = labeled_class("B", a) do 644 # def m; "B"+super; end 645 # alias m2 m 646 # prepend p 647 # alias m3 m 648 # end 649 650 # assert_nothing_raised do 651 # assert_equal("BA", b.new.m2, bug7842) 652 # end 653 654 # assert_nothing_raised do 655 # assert_equal("PBA", b.new.m3, bug7842) 656 # end 657 # end 754 658 755 659 assert 'Module#prepend each class' do … … 774 678 end 775 679 776 # requires #assert_sep erately680 # requires #assert_separately 777 681 #assert 'Module#prepend call super' do 778 682 # assert_separately([], <<-'end;') #do … … 785 689 # end; 786 690 #end 691 692 assert 'Module#prepend to frozen class' do 693 assert_raise(FrozenError) { Class.new.freeze.prepend Module.new } 694 end 787 695 # @!endgroup prepend 788 696 789 697 assert('Module#to_s') do 790 module Test4to_sModules 791 end 792 793 assert_equal 'Test4to_sModules', Test4to_sModules.to_s 698 module Outer 699 class Inner; end 700 const_set :SetInner, Class.new 701 end 702 703 assert_equal 'Outer', Outer.to_s 704 assert_equal 'Outer::Inner', Outer::Inner.to_s 705 assert_equal 'Outer::SetInner', Outer::SetInner.to_s 706 707 outer = Module.new do 708 const_set :SetInner, Class.new 709 end 710 Object.const_set :SetOuter, outer 711 712 assert_equal 'SetOuter', SetOuter.to_s 713 assert_equal 'SetOuter::SetInner', SetOuter::SetInner.to_s 714 715 assert_match "#<Module:0x*>", Module.new.to_s 716 assert_match "#<Class:0x*>", Class.new.to_s 717 718 assert_equal "FrozenClassToS", (FrozenClassToS = Class.new.freeze).to_s 719 assert_equal "Outer::A", (Outer::A = Module.new.freeze).to_s 720 assert_match "#<Module:0x*>::A", (Module.new::A = Class.new.freeze).to_s 794 721 end 795 722 … … 819 746 end 820 747 821 C1.new822 C2.new748 assert_kind_of(M1, C1.new) 749 assert_kind_of(M1, C2.new) 823 750 end 824 751 … … 834 761 end 835 762 836 B.new.foo763 assert_true(B.new.foo) 837 764 end 838 765 … … 849 776 assert_raise(TypeError) { module 0::M1 end } 850 777 assert_raise(TypeError) { module []::M2 end } 778 end 779 780 assert('module to return the last value') do 781 m = module M; :m end 782 assert_equal(m, :m) 783 end 784 785 assert('module to return nil if body is empty') do 786 assert_nil(module M end) 851 787 end 852 788
Note:
See TracChangeset
for help on using the changeset viewer.