class Lemon::TestMethod
The TestMethod
class is a special TestCase
that requires a particular target method be tested.
Attributes
Used to make sure the the method has been tested, or not.
Public Class Methods
New unit test.
Lemon::TestCase::new
# File lib/lemon/test_method.rb, line 14 def initialize(settings={}, &block) @tested = false super(settings) end
Public Instance Methods
# File lib/lemon/test_method.rb, line 131 def class_method? false end
If class method, returns target method's name prefixed with double colons. If instance method, then returns target method's name prefixed with hash character.
# File lib/lemon/test_method.rb, line 44 def name "##{target}" end
# File lib/lemon/test_method.rb, line 113 def raise_pending(procedure) if RUBY_VERSION < '1.9' Kernel.eval %[raise NotImplementedError, "#{target} not tested"], procedure else Kernel.eval %[raise NotImplementedError, "#{target} not tested"], procedure.binding end end
Run test in the context of this case. Notice that run
for TestMethod
is more complex than a general TestCase
. This is to ensure that the target method is invoked during the course of the test.
@param [TestProc] test
The test procedure instance to run.
@yield
The procedure for running the test.
Lemon::TestCase#run
# File lib/lemon/test_method.rb, line 78 def run(test, &block) target = self.target raise_pending(test.procedure) unless test.procedure begin target_class.class_eval do alias_method "_lemon_#{target}", target define_method(target) do |*a,&b| test.tested = true __send__("_lemon_#{target}",*a,&b) end end rescue => error Kernel.eval <<-END, test.to_proc.binding raise #{error.class}, "#{target} not tested" END end begin super(test, &block) #block.call ensure target_class.class_eval %{ alias_method "#{target}", "_lemon_#{target}" } end raise_pending(test.procedure) unless test.tested end
The target class.
# File lib/lemon/test_method.rb, line 124 def target_class @target_class ||= context.target end
Returns the prefixed method name.
# File lib/lemon/test_method.rb, line 54 def to_s "##{target}" end
Description of the type of test case.
# File lib/lemon/test_method.rb, line 30 def type 'Method' end
Returns the fully qulaified name of the target method. This is the standard interface used by Ruby Test.
# File lib/lemon/test_method.rb, line 62 def unit "#{context}##{target}" end
Validate that a context and target method have been supplied.
# File lib/lemon/test_method.rb, line 22 def validate_settings raise "method test has no module or class context" unless @context raise "#{@target} is not a method name" unless Symbol === @target end