module InPlaceTesting def self.included(klass) old_method_added = klass.method(:method_added) new_method_added = lambda do |m| @@current_method = m old_method_added.call m end # Next line inspired by http://utils.ning.com/ruby/dbc.rb by Brian McCallister and Martin Traverso (class << klass; self; end).send :define_method, :method_added, new_method_added class << klass def given(*args) self.new.send @@current_method, *args end def method_should(message, params) unless params[:return] == params[:when] puts "\"#{@@current_method}\" fails to #{message} (returns #{params[:when]} instead of #{params[:return]})" end end end end end class DumbClass include InPlaceTesting def add(x,y) x + y end method_should "add 5 and 4", :return => 9, :when => given(5, 4) def subtract(x,y) x - y end method_should "subtract 5 from 14", :return => 9, :when => given(14,5) def increment(x) x + 1 end method_should "increment 12", :return => 13, :when => given(12) end
You need to create an account or log in to post comments to this site.