Permalink
Browse files

Specs for Module#public_constant.

  • Loading branch information...
1 parent a8f9010 commit 3fe4e869d40e2dbceaefb0b5bce35a41de3d7020 @headius headius committed Dec 29, 2011
Showing with 103 additions and 0 deletions.
  1. +4 −0 core/module/fixtures/classes19.rb
  2. +99 −0 core/module/public_constant_spec.rb
View
4 core/module/fixtures/classes19.rb
@@ -66,6 +66,10 @@ def defined_from_subclass
defined? PRIVATE_CONSTANT_CLASS
end
end
+
+ def self.reset_private_constants
+ Object.send :private_constant, :PRIVATE_CONSTANT_IN_OBJECT
+ end
end
class Object
View
99 core/module/public_constant_spec.rb
@@ -0,0 +1,99 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+ruby_version_is "1.9.3" do
+ require File.expand_path('../fixtures/classes19', __FILE__)
+
+ describe "Module#public_constant" do
+ it "can only be passed constant names defined in the target (self) module" do
+ cls1 = Class.new
+ cls1.const_set :Foo, true
+ cls2 = Class.new(cls1)
+
+ lambda do
+ cls2.send :public_constant, :Foo
+ end.should raise_error(NameError)
+ end
+
+ not_compliant_on :jruby do
+ it "accepts multiple names but ignores names past the first" do
+ mod = Module.new
+ mod.const_set :Foo, true
+ mod.const_set :Bar, true
+
+ mod.send :private_constant, :Foo
+ mod.send :private_constant, :Bar
+
+ mod.send :public_constant, :Foo, :Bar
+
+ mod::Foo.should == true
+ lambda {mod::Bar}.should raise_error(NameError)
+ end
+ end
+
+ deviates_on :jruby do
+ it "accepts multiple names" do
+ mod = Module.new
+ mod.const_set :Foo, true
+ mod.const_set :Bar, true
+
+ mod.send :private_constant, :Foo
+ mod.send :private_constant, :Bar
+
+ mod.send :public_constant, :Foo, :Bar
+
+ mod::Foo.should == true
+ mod::Bar.should == true
+ end
+ end
+ end
+
+ describe "Module#public_constant marked constants" do
+ before :each do
+ @module = ModuleSpecs::PrivConstModule.dup
+ end
+
+ describe "in a module" do
+ it "can be accessed from outside the module" do
+ @module.send :public_constant, :PRIVATE_CONSTANT_MODULE
+ @module::PRIVATE_CONSTANT_MODULE.should == true
+ end
+
+ it "is defined? with A::B form" do
+ @module.send :public_constant, :PRIVATE_CONSTANT_MODULE
+ defined?(@module::PRIVATE_CONSTANT_MODULE).should == "constant"
+ end
+ end
+
+ describe "in a class" do
+ before :each do
+ @class = ModuleSpecs::PrivConstClass.dup
+ end
+
+ it "can be accessed from outside the class" do
+ @class.send :public_constant, :PRIVATE_CONSTANT_CLASS
+ @class::PRIVATE_CONSTANT_CLASS.should == true
+ end
+
+ it "is defined? with A::B form" do
+ @class.send :public_constant, :PRIVATE_CONSTANT_CLASS
+ defined?(@class::PRIVATE_CONSTANT_CLASS).should == "constant"
+ end
+ end
+
+ describe "in Object" do
+ after :each do
+ ModuleSpecs.reset_private_constants
+ end
+
+ it "can be accessed using ::Const form" do
+ Object.send :public_constant, :PRIVATE_CONSTANT_IN_OBJECT
+ ::PRIVATE_CONSTANT_IN_OBJECT.should == true
+ end
+
+ it "is defined? using ::Const form" do
+ Object.send :public_constant, :PRIVATE_CONSTANT_IN_OBJECT
+ defined?(::PRIVATE_CONSTANT_IN_OBJECT).should == "constant"
+ end
+ end
+ end
+end

0 comments on commit 3fe4e86

Please sign in to comment.