Class: Lemon::TestMethod

Inherits:
TestCase show all
Defined in:
lib/lemon/test_method.rb

Overview

The TestMethod class is a special TestCase that requires a particular target method be tested.

Direct Known Subclasses

TestClassMethod

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Attributes inherited from TestCase

#advice, #context, #label, #setup, #skip, #target, #tests

Instance Method Summary collapse

Methods inherited from TestCase

#domain, #domain_class, #each, #evaluate, #scope, #size, #skip!, #skip?, #tags

Constructor Details

#initialize(settings = {}, &block) ⇒ TestMethod

New unit test.



14
15
16
17
# File 'lib/lemon/test_method.rb', line 14

def initialize(settings={}, &block)
  @tested   = false
  super(settings)
end

Instance Attribute Details

#testedObject

Used to make sure the the method has been tested, or not.



37
38
39
# File 'lib/lemon/test_method.rb', line 37

def tested
  @tested
end

Instance Method Details

#class_method?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/lemon/test_method.rb', line 131

def class_method?
  false
end

#nameObject

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.



44
45
46
# File 'lib/lemon/test_method.rb', line 44

def name
  "##{target}"
end

#raise_pending(procedure) ⇒ Object



113
114
115
116
117
118
119
# 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) { ... } ⇒ Object

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.

Parameters:

  • test (TestProc)

    The test procedure instance to run.

Yields:

  • The procedure for running the test.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#target_classObject

The target class.



124
125
126
# File 'lib/lemon/test_method.rb', line 124

def target_class
  @target_class ||= context.target
end

#to_sObject

Returns the prefixed method name.



54
55
56
# File 'lib/lemon/test_method.rb', line 54

def to_s
  "##{target}"
end

#typeObject

Description of the type of test case.



30
31
32
# File 'lib/lemon/test_method.rb', line 30

def type
  'Method'
end

#unitObject

Returns the fully qulaified name of the target method. This is the standard interface used by Ruby Test.



62
63
64
# File 'lib/lemon/test_method.rb', line 62

def unit
  "#{context}##{target}"
end

#validate_settingsObject

Validate that a context and target method have been supplied.



22
23
24
25
# 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