Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Specs for Module#public_constant.

  • Loading branch information...
commit 3fe4e869d40e2dbceaefb0b5bce35a41de3d7020 1 parent a8f9010
Charles Oliver Nutter headius authored
4 core/module/fixtures/classes19.rb
@@ -66,6 +66,10 @@ def defined_from_subclass
66 66 defined? PRIVATE_CONSTANT_CLASS
67 67 end
68 68 end
  69 +
  70 + def self.reset_private_constants
  71 + Object.send :private_constant, :PRIVATE_CONSTANT_IN_OBJECT
  72 + end
69 73 end
70 74
71 75 class Object
99 core/module/public_constant_spec.rb
... ... @@ -0,0 +1,99 @@
  1 +require File.expand_path('../../../spec_helper', __FILE__)
  2 +
  3 +ruby_version_is "1.9.3" do
  4 + require File.expand_path('../fixtures/classes19', __FILE__)
  5 +
  6 + describe "Module#public_constant" do
  7 + it "can only be passed constant names defined in the target (self) module" do
  8 + cls1 = Class.new
  9 + cls1.const_set :Foo, true
  10 + cls2 = Class.new(cls1)
  11 +
  12 + lambda do
  13 + cls2.send :public_constant, :Foo
  14 + end.should raise_error(NameError)
  15 + end
  16 +
  17 + not_compliant_on :jruby do
  18 + it "accepts multiple names but ignores names past the first" do
  19 + mod = Module.new
  20 + mod.const_set :Foo, true
  21 + mod.const_set :Bar, true
  22 +
  23 + mod.send :private_constant, :Foo
  24 + mod.send :private_constant, :Bar
  25 +
  26 + mod.send :public_constant, :Foo, :Bar
  27 +
  28 + mod::Foo.should == true
  29 + lambda {mod::Bar}.should raise_error(NameError)
  30 + end
  31 + end
  32 +
  33 + deviates_on :jruby do
  34 + it "accepts multiple names" do
  35 + mod = Module.new
  36 + mod.const_set :Foo, true
  37 + mod.const_set :Bar, true
  38 +
  39 + mod.send :private_constant, :Foo
  40 + mod.send :private_constant, :Bar
  41 +
  42 + mod.send :public_constant, :Foo, :Bar
  43 +
  44 + mod::Foo.should == true
  45 + mod::Bar.should == true
  46 + end
  47 + end
  48 + end
  49 +
  50 + describe "Module#public_constant marked constants" do
  51 + before :each do
  52 + @module = ModuleSpecs::PrivConstModule.dup
  53 + end
  54 +
  55 + describe "in a module" do
  56 + it "can be accessed from outside the module" do
  57 + @module.send :public_constant, :PRIVATE_CONSTANT_MODULE
  58 + @module::PRIVATE_CONSTANT_MODULE.should == true
  59 + end
  60 +
  61 + it "is defined? with A::B form" do
  62 + @module.send :public_constant, :PRIVATE_CONSTANT_MODULE
  63 + defined?(@module::PRIVATE_CONSTANT_MODULE).should == "constant"
  64 + end
  65 + end
  66 +
  67 + describe "in a class" do
  68 + before :each do
  69 + @class = ModuleSpecs::PrivConstClass.dup
  70 + end
  71 +
  72 + it "can be accessed from outside the class" do
  73 + @class.send :public_constant, :PRIVATE_CONSTANT_CLASS
  74 + @class::PRIVATE_CONSTANT_CLASS.should == true
  75 + end
  76 +
  77 + it "is defined? with A::B form" do
  78 + @class.send :public_constant, :PRIVATE_CONSTANT_CLASS
  79 + defined?(@class::PRIVATE_CONSTANT_CLASS).should == "constant"
  80 + end
  81 + end
  82 +
  83 + describe "in Object" do
  84 + after :each do
  85 + ModuleSpecs.reset_private_constants
  86 + end
  87 +
  88 + it "can be accessed using ::Const form" do
  89 + Object.send :public_constant, :PRIVATE_CONSTANT_IN_OBJECT
  90 + ::PRIVATE_CONSTANT_IN_OBJECT.should == true
  91 + end
  92 +
  93 + it "is defined? using ::Const form" do
  94 + Object.send :public_constant, :PRIVATE_CONSTANT_IN_OBJECT
  95 + defined?(::PRIVATE_CONSTANT_IN_OBJECT).should == "constant"
  96 + end
  97 + end
  98 + end
  99 +end

0 comments on commit 3fe4e86

Please sign in to comment.
Something went wrong with that request. Please try again.