Class: Lemon::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/lemon/test_case.rb

Overview

Test Case serves as the base class for Lemon’s specialized test case classes.

Direct Known Subclasses

TestMethod, TestModule

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

A test case target is a class or module.

Parameters:

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

    The settings used to define the test case.

Options Hash (settings):

  • :context (TestCase)

    Parent test case.

  • :target (Module, Class, Symbol)

    The testcase’s target.

  • :label (String)

    Breif description of testcase. (NOTE: this might not be used)

  • :setup (TestSetup)

    Test setup.

  • :skip (Boolean)

    If runner should skip test.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lemon/test_case.rb', line 58

def initialize(settings={}, &block)
  @context = settings[:context]
  @target  = settings[:target]
  @label   = settings[:label]
  @setup   = settings[:setup]
  @skip    = settings[:skip]
  @tags    = settings[:tags]

  @advice  = @context ? @context.advice.dup : TestAdvice.new

  @tests   = []
  @domain  = domain_class.new(self)

  validate_settings

  evaluate(&block)
end

Instance Attribute Details

#adviceObject (readonly)

Advice are labeled procedures, such as before and after advice.



28
29
30
# File 'lib/lemon/test_case.rb', line 28

def advice
  @advice
end

#contextObject (readonly)

The parent context in which this case resides.



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

def context
  @context
end

#labelObject (readonly)

Brief description of the test case.



18
19
20
# File 'lib/lemon/test_case.rb', line 18

def label
  @label
end

#setupObject (readonly)

The setup and teardown advice.



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

def setup
  @setup
end

#skipObject (readonly)

Skip execution of test case?



34
35
36
# File 'lib/lemon/test_case.rb', line 34

def skip
  @skip
end

#targetObject (readonly)

Target component.



21
22
23
# File 'lib/lemon/test_case.rb', line 21

def target
  @target
end

#testsObject (readonly)

List of tests and sub-contexts.



31
32
33
# File 'lib/lemon/test_case.rb', line 31

def tests
  @tests
end

Instance Method Details

#domainObject



84
85
86
# File 'lib/lemon/test_case.rb', line 84

def domain
  @domain
end

#domain_classObject

Get the domain class dynamically so that each subclass of TestCase will retrieve it’s own.



180
181
182
# File 'lib/lemon/test_case.rb', line 180

def domain_class
  self.class.const_get(:DSL)
end

#each(&block) ⇒ Object

Iterate over each test and subcase.



98
99
100
# File 'lib/lemon/test_case.rb', line 98

def each(&block)
  tests.each(&block)
end

#evaluate(&block) ⇒ Object



91
92
93
# File 'lib/lemon/test_case.rb', line 91

def evaluate(&block)
  @domain.module_eval(&block)
end

#run(test, &block) ⇒ Object

Run test in the context of this case.

Parameters:

  • test (TestProc)

    The test unit to run.



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/lemon/test_case.rb', line 151

def run(test, &block)
  advice[:before].each do |matches, block|
    if matches.all?{ |match| test.match?(match) }
      scope.instance_exec(test, &block) #block.call(unit)
    end
  end

  block.call

  advice[:after].each do |matches, block|
    if matches.all?{ |match| test.match?(match) }
      scope.instance_exec(test, &block) #block.call(unit)
    end
  end
end

#scopeScope

Module for evaluating test case script.

Returns:

  • (Scope)

    evaluation scope



172
173
174
# File 'lib/lemon/test_case.rb', line 172

def scope
  @scope ||= TestScope.new(self)
end

#sizeObject

Number of tests and subcases.



105
106
107
# File 'lib/lemon/test_case.rb', line 105

def size
  tests.size
end

#skip!(reason = true) ⇒ Object



134
135
136
# File 'lib/lemon/test_case.rb', line 134

def skip!(reason=true)
  @skip = reason
end

#skip?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/lemon/test_case.rb', line 127

def skip?
  @skip
end

#tagsObject



141
142
143
# File 'lib/lemon/test_case.rb', line 141

def tags
  @tags
end

#to_sObject



120
121
122
# File 'lib/lemon/test_case.rb', line 120

def to_s
  @label.to_s
end

#typeObject

Subclasses of TestCase can override this to describe the type of test case they define.



113
114
115
# File 'lib/lemon/test_case.rb', line 113

def type
  'Test Case'
end

#validate_settingsObject

Subclasses can override this methof to validate settings. It is run just before evaluation of scope block.



80
81
# File 'lib/lemon/test_case.rb', line 80

def validate_settings
end