require "core/spec_helper" describe "Module" do after do remove_constants %w( A X Y Z InheritableAccessorForModule InheritableAccessorForClass InheritableAccessor InheritableAccessorBase InheritableAccessorA InheritableAccessorB ) end it "Namespace" do class A class B class C end end end File.namespace.should == nil A::B::C.namespace.should == A::B end # class AnonymousClass # class << self # def anonymous_name # self.name # end # end # end it "name" do class A class B class C end end end A::B::C.name.should == "A::B::C" end it "each_ancestor" do class X; end class Y < X; end class Z < Y; end list = [] Z.each_ancestor{|a| list << a} list.should include Y list.should include X list.should_not include Z list.should_not include Object list.should_not include Kernel list = [] Z.each_ancestor(true){|a| list << a} list.should include Y list.should include X list.should_not include Z list.should include Object list.should include Kernel end it "each_namespace" do class A class B class C end end end list = [] A::B::C.each_namespace{|n| list << n} list.should == [A::B, A] end it "is?" do File.is?(IO).should be_true end class WrapMethod def method_name value 10*value end end it "wrap_method" do WrapMethod.wrap_method :method_name do |old, value| send(old, value)*value end WrapMethod.new.method_name(10).should == 1000 end it "escape_method" do Object.escape_method("><=?") end it "inheritable_accessor" do module InheritableAccessorForModule module ClassMethods inheritable_accessor :callbacks, [:set_user] inheritable_accessor :layout, "for_module" end end class InheritableAccessorForClass class << self inheritable_accessor :callbacks2, [] inheritable_accessor :layout2, "for_class" end callbacks2 << :set_user end class InheritableAccessor < InheritableAccessorForClass inherit InheritableAccessorForModule callbacks << :set_model callbacks2 << :set_model self.layout2 = "anoter_layout" end InheritableAccessorForClass.callbacks2.should == [:set_user] InheritableAccessor.callbacks.should == [:set_user, :set_model] InheritableAccessor.callbacks2.should == [:set_user, :set_model] InheritableAccessorForClass.layout2.should == "for_class" InheritableAccessor.layout2.should == "anoter_layout" InheritableAccessor.layout.should == "for_module" end it "inheritable_accessor (from error)" do module InheritableAccessorBase module ClassMethods inheritable_accessor :callbacks, [] end end class InheritableAccessorA inherit InheritableAccessorBase callbacks << :a end class InheritableAccessorB inherit InheritableAccessorBase callbacks << :b end InheritableAccessorA.callbacks.should == [:a] InheritableAccessorB.callbacks.should == [:b] end end