Permalink
Browse files

Fix stub_const/hide_const to work properly with a const like "::Foo".

It didn't work properly with names containing leading '::' before.

Fixes #200.
  • Loading branch information...
1 parent 148db3c commit 17daf44bc400de9cef6af8401e5ae7fde5243059 @myronmarston myronmarston committed Dec 3, 2012
Showing with 16 additions and 2 deletions.
  1. +8 −2 lib/rspec/mocks/mutate_const.rb
  2. +8 −0 spec/rspec/mocks/mutate_const_spec.rb
@@ -57,16 +57,22 @@ def constants_defined_on(mod)
end
def recursive_const_get(const_name)
- const_name.split('::').inject(Object) { |mod, name| get_const_defined_on(mod, name) }
+ normalize_const_name(const_name).split('::').inject(Object) do |mod, name|
+ get_const_defined_on(mod, name)
+ end
end
def recursive_const_defined?(const_name)
- const_name.split('::').inject([Object, '']) do |(mod, full_name), name|
+ normalize_const_name(const_name).split('::').inject([Object, '']) do |(mod, full_name), name|
yield(full_name, name) if block_given? && !mod.is_a?(Module)
return false unless const_defined_on?(mod, name)
[get_const_defined_on(mod, name), [mod, name].join('::')]
end
end
+
+ def normalize_const_name(const_name)
+ const_name.sub(/\A::/, '')
+ end
end
# Provides information about constants that may (or may not)
@@ -141,6 +141,10 @@ def change_const_value_to(value)
it_behaves_like "loaded constant hiding", "TestClass::Nested"
end
+ context 'for a loaded context prefixed with ::' do
+ it_behaves_like 'loaded constant hiding', "::TestClass"
+ end
+
context 'for an unloaded constant with nested name that matches a top-level constant' do
it_behaves_like "unloaded constant hiding", "TestClass::Hash"
@@ -291,6 +295,10 @@ def change_const_value_to(value)
it_behaves_like "loaded constant stubbing", "TestClass::Nested"
end
+ context 'for a loaded context prefixed with ::' do
+ it_behaves_like 'loaded constant stubbing', "::TestClass"
+ end
+
context 'for an unloaded constant with nested name that matches a top-level constant' do
it_behaves_like "unloaded constant stubbing", "TestClass::Hash"
end

0 comments on commit 17daf44

Please sign in to comment.