Created
January 22, 2010 21:46
-
-
Save gstark/284163 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
require 'test/unit' | |
module GetterSetter | |
def getter_setter(attribute) | |
instance_variable = "@#{instance_variable}" | |
define_method("#{attribute}=") do |value| | |
instance_variable_set(instance_variable,value) | |
end | |
define_method(attribute) do | |
instance_variable_get(instance_variable) | |
end | |
end | |
end | |
class Thing | |
extend GetterSetter | |
getter_setter :foo | |
end | |
class GetterSetterTest < Test::Unit::TestCase | |
def test_thing_should_have_foo_setter | |
a = Thing.new | |
assert_nothing_raised do | |
a.foo = 42 | |
end | |
end | |
def test_thing_should_have_a_foo_getter | |
a = Thing.new | |
a.foo = 42 | |
assert a.foo == 42 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment