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.

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Attributes inherited from TestCase

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

Instance Method Summary collapse

Methods inherited from TestCase

#each, #evaluate, #scope, #scope_class, #size, #skip!, #skip?

Constructor Details

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

New unit test.

Parameters:

  • settings (Hash) (defaults to: {})

    a customizable set of options

Options Hash (settings):

  • :function (Boolean)

    Is the target method a class method, or not.



16
17
18
19
20
# File 'lib/lemon/test_method.rb', line 16

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

Instance Attribute Details

#testedObject

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



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

def tested
  @tested
end

Instance Method Details

#function?Boolean Also known as: class_method?

Is this method a class method?

Returns:

  • (Boolean)


41
42
43
# File 'lib/lemon/test_method.rb', line 41

def function?
  @function
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.



51
52
53
# File 'lib/lemon/test_method.rb', line 51

def name
  function? ? "::#{target}" : "##{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.



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
109
110
# File 'lib/lemon/test_method.rb', line 80

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

If the target method is a class method, then the target class is the meta-class, otherwise just the class itself.



123
124
125
126
127
128
129
130
131
# File 'lib/lemon/test_method.rb', line 123

def target_class
  @target_class ||= (
    if function? 
      (class << context.target; self; end)
    else
      context.target
    end
  )
end

#to_sObject

Returns the prefixed method name.



59
60
61
# File 'lib/lemon/test_method.rb', line 59

def to_s
  function? ? "::#{target}" : "##{target}"
end

#typeObject

Type is either ‘Method` or `Function` (a function is a class method).



29
30
31
32
33
34
35
# File 'lib/lemon/test_method.rb', line 29

def type
  if function?
    'Function'
  else
    'Method'
  end
end

#unitObject

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



65
66
67
# File 'lib/lemon/test_method.rb', line 65

def unit
  function? ? "#{context}.#{target}" : "#{context}##{target}"
end

#validate_settingsObject

Validate that a context and target method have been supplied.



23
24
25
26
# File 'lib/lemon/test_method.rb', line 23

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