Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save beccasaurus/291477 to your computer and use it in GitHub Desktop.
Save beccasaurus/291477 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'spec'
require 'extlib'
module SomeNamespace; end
describe 'Extlib#assert_kind_of' do
include Extlib::Assertions
it 'should handle constant reloading' do
class SomeNamespace::SomeClass; end
@instance = SomeNamespace::SomeClass.new
lambda { assert_kind_of 'test', @instance, SomeNamespace::SomeClass }.should_not raise_error
# reload SomeNamespace::SomeClass constant
lambda { SomeNamespace::SomeClass }.should_not raise_error
SomeNamespace.send(:remove_const, :SomeClass)
lambda { SomeNamespace::SomeClass }.should raise_error(NameError, 'uninitialized constant SomeNamespace::SomeClass')
class SomeNamespace::SomeClass; end
lambda { assert_kind_of 'test', @instance, SomeNamespace::SomeClass }.should_not raise_error
end
end
Extlib#assert_kind_of
- should handle constant reloading (FAILED - 1)
1)
'Extlib#assert_kind_of should handle constant reloading' FAILED
expected no Exception, got #<ArgumentError: +test+ should be SomeClass, but was SomeClass>
./extlib-assert_kind_of-does-not-like-class-reloading.rb:18:
module Extlib
module Assertions
# Allows for classes to be reloaded.
# In theory, we might only want to allow this while in development mode.
#
# As run the original assert_kind_of and, if an ArgumentError is raised,
# we double-check that none of the class names match.
#
# If they match, we return, assuming that, if the class names match,
# then the actual type is a match.
#
# If there are no class name matches, we raise the original exception.
def assert_kind_of_with_allow_class_name_matching(name, value, *klasses)
begin
assert_kind_of_without_allow_class_name_matching(name, value, *klasses)
rescue ArgumentError
klasses.each { |k| return if value.class.name == k.name }
raise # if we haven't returned, raise the original exception
end
end
alias_method_chain :assert_kind_of, :allow_class_name_matching
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment