Created
January 22, 2010 20:44
-
-
Save gstark/284123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a method on a class that will duplicate an instance, | |
# remove a method given as an argument and return the new methodless object | |
require 'test/unit' | |
module MetaClassFetcher | |
def metaclass | |
class <<self | |
self | |
end | |
end | |
end | |
class MyClass | |
def stripping(method_name) | |
other = self.dup | |
other.extend(MetaClassFetcher) | |
other.metaclass.send(:undef_method,method_name.to_sym) | |
return other | |
end | |
def clothes | |
"undies" | |
end | |
end | |
class ThingyTest < Test::Unit::TestCase | |
def setup | |
@receiver = MyClass.new | |
@stripped = @receiver.stripping("clothes") | |
end | |
def test_stripping_will_duplicate_the_receiving_object | |
assert @stripped.kind_of?(MyClass) && (@stripped.object_id != @receiver.object_id) | |
end | |
def test_stripping_will_not_return_nil | |
assert [email protected]? | |
end | |
def test_stripping_leaves_method_on_receiver | |
assert @receiver.respond_to?(:clothes) | |
end | |
def test_stripping_can_remove_clothes | |
assert [email protected]_to?(:clothes) | |
end | |
def test_stripping_does_not_affect_newly_created_instances | |
new_instance = MyClass.new | |
assert new_instance.respond_to?(:clothes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment