Module: Assert::Context::SetupDSL

Included in:
Assert::Context
Defined in:
lib/assert/context/setup_dsl.rb

Instance Method Summary collapse

Instance Method Details

#setup(scope_or_method_name = nil, &block) ⇒ Object Also known as: before

Add a setup block to run before each test or run the list of teardown blocks in given scope



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/assert/context/setup_dsl.rb', line 19

def setup(scope_or_method_name = nil, &block)
  is_method = scope_or_method_name.kind_of?(String) || scope_or_method_name.kind_of?(Symbol)
  if block_given? || is_method
    # arg is a block or method that needs to be stored as a setup
    self.setups << (block || scope_or_method_name)
  elsif !is_method
    # arg is an instance of this class (the scope for a test),
    # run the setups for this context in the scope
    scope = scope_or_method_name
    # setup parent...
    self.superclass.setup(scope) if self.superclass.respond_to?(:setup)
    # ... before child
    self.setups.each do |setup|
      setup.kind_of?(::Proc) ? scope.instance_eval(&setup) : scope.send(setup)
    end
  end
end

#setup_once(&block) ⇒ Object Also known as: before_once, startup



6
7
8
# File 'lib/assert/context/setup_dsl.rb', line 6

def setup_once(&block)
  self.suite.setup(&block)
end

#teardown(scope_or_method_name = nil, &block) ⇒ Object Also known as: after

Add a teardown block to run after each test or run the list of teardown blocks in given scope



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/assert/context/setup_dsl.rb', line 39

def teardown(scope_or_method_name = nil, &block)
  is_method = scope_or_method_name.kind_of?(String) || scope_or_method_name.kind_of?(Symbol)
  if block_given? || is_method
    # arg is a block or method that needs to be stored as a teardown
    self.teardowns << (block || scope_or_method_name)
  elsif !is_method
    # arg is an instance of this class (the scope for a test),
    # run the setups for this context in the scope
    scope = scope_or_method_name
    # teardown child...
    self.teardowns.each do |teardown|
      teardown.kind_of?(::Proc) ? scope.instance_eval(&teardown) : scope.send(teardown)
    end
    # ... before parent
    self.superclass.teardown(scope) if self.superclass.respond_to?(:teardown)
  end
end

#teardown_once(&block) ⇒ Object Also known as: after_once, shutdown



12
13
14
# File 'lib/assert/context/setup_dsl.rb', line 12

def teardown_once(&block)
  self.suite.teardown(&block)
end