Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from makandra/avoid-global-blank-slate

Avoid using a global BlankSlate class
  • Loading branch information...
commit d1e9029e9eae09783a99114d7d23372d4883b265 2 parents 2a68ab1 + 5234c01
@raganwald authored
Showing with 25 additions and 7 deletions.
  1. +4 −4 lib/andand.rb
  2. +13 −3 test/andand_spec.rb
  3. +8 −0 test/test_helper.rb
View
8 lib/andand.rb
@@ -97,9 +97,9 @@ def dont (p = nil)
class Object
include AndAnd::ObjectGoodies
end
-
-unless Module.constants.map { |c| c.to_s }.include?('BlankSlate')
- if Module.constants.map { |c| c.to_s }.include?('BasicObject')
+
+unless defined?(::AndAnd::BlankSlate)
+ if defined?(::BasicObject)
module AndAnd
class BlankSlate < BasicObject
end
@@ -145,4 +145,4 @@ def method_missing(sym, *args, &block)
end
end
-end
+end
View
16 test/andand_spec.rb
@@ -26,7 +26,7 @@
=end
-require File.dirname(__FILE__) + '/test_helper.rb'
+require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')
require 'andand'
@@ -112,6 +112,16 @@ def to_proc
end
end
+describe 'AndAnd::BlankSlate' do
+ it 'should not respond to Object methods' do
+ nil.andand.to_s.should == nil
+ end
+
+ it 'should not be based on a global BlankSlate object' do
+ nil.andand.respond_to?(:method_on_global_blank_slate).should be_false
+ end
+end
+
class Foo
def frobbish
'fnord'
@@ -128,6 +138,6 @@ def frobbish
end.should_not raise_error
lambda do
foo.andand.hsibborf
- end.should raise_error
+ end.should raise_error(NoMethodError)
end
-end
+end
View
8 test/test_helper.rb
@@ -1,2 +1,10 @@
+class BlankSlate
+ # A global BlankSlate object. This is bad, because it inherits from Object
+ # and offers more methods than AndAnd::BlankSlate should offer.
+ # For example, ActiveSupport creates a global BlankSlate class.
+ def method_on_global_blank_slate
+ end
+end
+
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/andand'
Please sign in to comment.
Something went wrong with that request. Please try again.