Created
October 10, 2015 21:36
-
-
Save lukeledet/6cf41ae3368eec53dff2 to your computer and use it in GitHub Desktop.
Forgiver class and tests
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
class Forgiver < BasicObject | |
attr_reader :arg | |
def initialize arg | |
@arg = arg | |
end | |
def method_missing m, *args, &block | |
arg.respond_to?(m) ? ::Forgiver.new(arg.send(m, *args, &block)) : self | |
end | |
end |
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
require 'minitest/autorun' | |
require './forgiver' | |
class ForgiverTest < Minitest::Test | |
def setup | |
@object = Forgiver.new("string") | |
end | |
def test_simple_return | |
assert @object.upcase === "STRING" | |
end | |
def test_single_invalid_method | |
# This should return the original argument | |
assert @object.sup === "string" | |
end | |
def test_invalid_method_after_valid_method | |
assert @object.upcase.noop === "STRING" | |
end | |
def test_mix_of_valid_and_invalid_methods | |
assert @object.noop.noop.upcase.noop.downcase === "string" | |
end | |
def test_many_invalid_methods | |
assert @object.sup.dog.hi === "string" | |
end | |
def test_many_valid_methods | |
assert @object.upcase.chop.length.to_s === "5" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment