-
-
Save bmabey/131701 to your computer and use it in GitHub Desktop.
macro example and variations
This file contains hidden or 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
describe Customer, "last billing address" do | |
before do | |
@customer = Customer.generate! | |
@customer = Address.new | |
end | |
it "should be settable and gettable" do | |
@customer.last_billing_address = @account_address | |
@customer.last_billing_address.should == @account_address | |
end | |
... | |
end | |
describe Customer, "last delivery address" do | |
before do | |
@customer = Customer.generate! | |
@account_address = Address.new | |
end | |
it "should be settable and gettable" do | |
@customer.last_delivery_address = @account_address | |
@customer.last_delivery_address.should == @account_address | |
end | |
end |
This file contains hidden or 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 Customer | |
attr_accessor :last_delivery_address, :last_billing_address | |
end | |
module AccessorMacro | |
def it_should_have_an_accessor_for(*attributes) | |
attributes.each do |attribute| | |
describe attribute do | |
it "should be settable and gettable" do | |
reader = attribute.gsub(' ','_') | |
writer = "#{reader}=" | |
@customer.send(writer,'some value') | |
@customer.send(reader).should == 'some value' | |
end | |
end | |
end | |
end | |
end | |
describe Customer do | |
before do | |
@customer = Customer.new | |
end | |
extend AccessorMacro | |
it_should_have_an_accessor_for('last billing address', 'last delivery address') | |
end |
This file contains hidden or 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 Customer | |
attr_accessor :last_delivery_address, :last_billing_address | |
end | |
module AccessorMacro | |
def it_should_have_an_accessor_for(*attributes) | |
attributes.each do |attribute| | |
it "has an accesor for #{attribute}" do | |
reader = attribute.to_s.gsub(' ','_') | |
writer = "#{reader}=" | |
@customer.send(writer,'some value') | |
@customer.send(reader).should == 'some value' | |
end | |
end | |
end | |
alias :it_has_accessors_for :it_should_have_an_accessor_for | |
end | |
describe Customer do | |
before do | |
@customer = Customer.new | |
end | |
extend AccessorMacro | |
it_has_accessors_for :last_billing_address, :last_delivery_address | |
end |
This file contains hidden or 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 Customer | |
attr_accessor :last_delivery_address, :last_billing_address | |
end | |
module AccessorMacro | |
def it_should_have_an_accessor_for(*attributes) | |
attributes.each do |attribute| | |
it "has an accesor for #{attribute}" do | |
reader = attribute.to_s.gsub(' ','_') | |
writer = "#{reader}=" | |
described_object = instance_variable_get("@#{self.class.described_type.to_s.downcase}") || self.class.described_type.new | |
described_object.send(writer,'some value') | |
described_object.send(reader).should == 'some value' | |
end | |
end | |
end | |
alias :it_has_accessors_for :it_should_have_an_accessor_for | |
end | |
describe Customer do | |
before do | |
@customer = Customer.new | |
end | |
extend AccessorMacro | |
it_has_accessors_for :last_billing_address, :last_delivery_address | |
end |
This file contains hidden or 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 Customer | |
attr_accessor :last_delivery_address, :last_billing_address | |
end | |
module AccessorMacro | |
def it_should_have_an_accessor_for(*attributes) | |
attributes.each do |attribute| | |
it "has an accesor for #{attribute}" do | |
reader = attribute.to_s.gsub(' ','_') | |
writer = "#{reader}=" | |
subject.send(writer,'some value') | |
subject.send(reader).should == 'some value' | |
end | |
end | |
end | |
alias :it_has_accessors_for :it_should_have_an_accessor_for | |
end | |
describe Customer do | |
# This isn't really needed, but it shows how you would define a subject with your current setup. | |
#before do | |
#@customer = Customer.new | |
#end | |
#subject { @customer } | |
extend AccessorMacro | |
it_has_accessors_for :last_billing_address, :last_delivery_address | |
end |
This file contains hidden or 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 Customer | |
attr_accessor :last_delivery_address, :last_billing_address | |
end | |
module AccessorMacro | |
def it_should_have_an_accessor_for(*attributes) | |
attributes.each do |attribute| | |
class_eval(<<-END_OF_MACRO, __FILE__, __LINE__ + 1) | |
it "has an accesor for #{attribute}" do | |
subject.#{attribute}= 'some value' | |
subject.#{attribute}.should == 'some value' | |
end | |
END_OF_MACRO | |
end | |
end | |
alias :it_has_accessors_for :it_should_have_an_accessor_for | |
end | |
describe Customer do | |
extend AccessorMacro | |
it_has_accessors_for :last_billing_address, :last_delivery_address | |
end |
This file contains hidden or 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 Customer | |
attr_accessor :last_delivery_address, :last_billing_address | |
end | |
module AccessorMacro | |
def it_should_have_an_accessor_for(*attributes) | |
source_filename, source_line = caller[0].split | |
attributes.each do |attribute| | |
class_eval(<<-END_OF_MACRO, source_filename, source_line.to_i) | |
it "has an accesor for #{attribute}" do | |
subject.#{attribute}= 'some value' | |
subject.#{attribute}.should == 'some value' | |
end | |
END_OF_MACRO | |
end | |
end | |
alias :it_has_accessors_for :it_should_have_an_accessor_for | |
end | |
describe Customer do | |
extend AccessorMacro | |
it_has_accessors_for :last_billing_address, :last_delivery_address | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment