Class: Lemon::TestCase::DSL

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

Instance Method Summary collapse

Methods inherited from Module

#namespace

Constructor Details

#initialize(testcase) ⇒ DSL

Returns a new instance of DSL.



197
198
199
200
201
202
203
204
205
# File 'lib/lemon/test_case.rb', line 197

def initialize(testcase) #, &code)
  @_testcase = testcase
  @_setup    = testcase.setup
  @_skip     = nil

  include testcase.context.domain if testcase.context

  #module_eval(&code)
end

Instance Method Details

#after(*matches, &procedure) ⇒ Object Also known as: After

Define a complex after procedure. The #before method allows before procedures to be defined that are triggered by a match against the unit’s target method name or aspect description. This allows groups of tests to be defined that share special teardown code.

Examples:

Method :puts do
  Test "standard output (@stdout)" do
    puts "Hello"
  end

  Before /@stdout/ do
    $stdout = StringIO.new
  end

  After /@stdout/ do
    $stdout = STDOUT
  end
end

Parameters:

  • matches (Array<Symbol,Regexp>)

    List of match critera that must all be matched to trigger the after procedure.



295
296
297
# File 'lib/lemon/test_case.rb', line 295

def after(*matches, &procedure)
  @_testcase.advice[:after][matches] = procedure
end

#before(*matches, &procedure) ⇒ Object Also known as: Before

Define a complex before procedure. The #before method allows before procedures to be defined that are triggered by a match against the unit’s target method name or aspect description. This allows groups of tests to be defined that share special setup code.

Examples:

Method :puts do
  Test "standard output (@stdout)" do
    puts "Hello"
  end

  Before /@stdout/ do
    $stdout = StringIO.new
  end

  After /@stdout/ do
    $stdout = STDOUT
  end
end

Parameters:

  • matches (Array<Symbol,Regexp>)

    List of match critera that must all be matched to trigger the before procedure.



264
265
266
# File 'lib/lemon/test_case.rb', line 264

def before(*matches, &procedure)
  @_testcase.advice[:before][matches] = procedure
end

#context(label, *tags, &block) ⇒ Object Also known as: Context

Create a subcase of module testcase.



306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/lemon/test_case.rb', line 306

def context(label, *tags, &block)
  return if @_omit

  @_testcase.tests << context_class.new(
    :context => @_testcase,
    :target  => @_testcase.target,
    :setup   => @_setup,
    :skip    => @_skip,
    :label   => label,
    :tags    => tags,
    &block
  )
end

#context_classObject

The class for which this is a DSL context.



190
191
192
# File 'lib/lemon/test_case.rb', line 190

def context_class
  TestCase
end

#omit(reason = true) ⇒ Object Also known as: Omit

Omitted tests are simply ignored and never instantiated let alone passed on to the test harness.

If a block is given then only tests defined with-in the block are skipped. If no block is given then all subsquent tests in the test case are skipped.

Examples:

omit do
  test do
    ...
  end
end


364
365
366
367
368
369
370
371
372
# File 'lib/lemon/test_case.rb', line 364

def omit(reason=true)
  if block_given?
    @_omit = reason
    yield
    @_omit = nil
  else
    @_omit = reason
  end
end

#setup(description = nil, &procedure) ⇒ Object Also known as: Setup, concern, Concern

Setup is used to set things up for each unit test. The setup procedure is run before each unit.

Parameters:

  • description (String) (defaults to: nil)

    A brief description of what the setup procedure sets-up.



214
215
216
217
218
# File 'lib/lemon/test_case.rb', line 214

def setup(description=nil, &procedure)
  if procedure
    @_setup = TestSetup.new(@test_case, description, &procedure)
  end
end

#skip(reason = true) ⇒ Object Also known as: Skip

Skip tests. Unlike omit, skipped tests are passed to the test harness, so they still can be included in reports, though they are not executed.

If a block is given then only tests defined with-in the block are skipped. If no block is given then all subsquent tests in the test case are skipped.

Examples:

skip "reason" do
  test do
    ...
  end
end

Parameters:

  • reason (String, Boolean) (defaults to: true)

    A description of the reason to skip the test, or simply a boolean value.



338
339
340
341
342
343
344
345
346
# File 'lib/lemon/test_case.rb', line 338

def skip(reason=true)
  if block_given?
    @_skip = reason
    yield
    @_skip = nil
  else
    @_skip = reason
  end
end

#teardown(&procedure) ⇒ Object Also known as: Teardown

Teardown procedure is used to clean-up after each unit test.



230
231
232
# File 'lib/lemon/test_case.rb', line 230

def teardown(&procedure)
  @_setup.teardown = procedure
end