Module: RSpec::Given::InstanceExtensions

Included in:
Spec::ExampleGroup
Defined in:
lib/rspec/given/extensions.rb

Overview

Provide run-time methods to support RSpec/Given infrastructure. All the methods in this module are considered private and implementation-specific.

Instance Method Summary collapse

Instance Method Details

#_rg_check_andsObject

:nodoc:



86
87
88
89
90
91
92
# File 'lib/rspec/given/extensions.rb', line 86

def _rg_check_ands  # :nodoc:
  return if self.class._rgc_context_info[:and_ran]
  self.class._rgc_and_blocks.each do |block|
    _rg_evaluate("And", block)
  end
  self.class._rgc_context_info[:and_ran] = true
end

#_rg_check_invariantsObject

Check all the invariants in the current and surrounding describe/context blocks, starting with the outermost context.



78
79
80
81
82
83
84
# File 'lib/rspec/given/extensions.rb', line 78

def _rg_check_invariants  # :nodoc:
  _rg_contexts.each do |context|
    context._rgc_invariants.each do |block|
      _rg_evaluate("Invariant", block)
    end
  end
end

#_rg_contextsObject

List of containing contexts in order from outermost to innermost.



23
24
25
# File 'lib/rspec/given/extensions.rb', line 23

def _rg_contexts          # :nodoc:
  _rg_inner_contexts.reverse
end

#_rg_establish_givensObject

Establish all the Given preconditions the current and surrounding describe/context blocks, starting with the outermost context.



66
67
68
69
70
71
72
73
74
# File 'lib/rspec/given/extensions.rb', line 66

def _rg_establish_givens  # :nodoc:
  return if defined?(@_rg_ran) && @_rg_ran
  @_rg_ran = true
  _rg_contexts.each do |context|
    context._rgc_givens.each do |block|
      instance_eval(&block)
    end
  end
end

#_rg_evaluate(clause_type, block) ⇒ Object

Evaluate a Then, And, or Invariant assertion.



103
104
105
106
107
108
109
110
# File 'lib/rspec/given/extensions.rb', line 103

def _rg_evaluate(clause_type, block)   # :nodoc:
  RSpec::Given.matcher_called = false
  passed = instance_eval(&block)
  if ! passed && _rg_na_configured? && ! RSpec::Given.matcher_called
    nassert = NaturalAssertion.new(clause_type, block, self, self.class._rgc_lines)
    RSpec::Given.fail_with nassert.message if _rg_need_na_message?(nassert)
  end
end

#_rg_info(keyword) ⇒ Object

Return the context information for keyword from the innermost defining context.



29
30
31
32
33
34
35
36
37
# File 'lib/rspec/given/extensions.rb', line 29

def _rg_info(keyword)     # :nodoc:
  _rg_inner_contexts.each do |context|
    h = context._rgc_context_info
    if h.has_key?(keyword)
      return h[keyword]
    end
  end
  nil
end

#_rg_inner_contextsObject

List of containing contexts in order from innermost to outermost.



15
16
17
18
19
# File 'lib/rspec/given/extensions.rb', line 15

def _rg_inner_contexts    # :nodoc:
  self.class.ancestors.select { |context|
    context.respond_to?(:_rgc_givens)
  }
end

#_rg_na_configured?Boolean

Return the configuration value for natural assertions.

If natural assertions are not configured in the contexts, use the global configuration value.

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/rspec/given/extensions.rb', line 58

def _rg_na_configured?    # :nodoc:
  info_value = _rg_info(:natural_assertions_enabled)
  info_value.nil? ? RSpec::Given.natural_assertions_enabled? : info_value
end

#_rg_need_na_message?(nassert) ⇒ Boolean

Should a natural assertion failure message be generated?

A natural assertion failure message is generated if the assertion has non-empty content that doesn’t use rspec assertions. The configuration options for natural assertions are checked and applied accordingly.

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/rspec/given/extensions.rb', line 46

def _rg_need_na_message?(nassert) # :nodoc:
  return false unless nassert.has_content?
  use_na = _rg_na_configured?
  return true if use_na == :always
  return false if !RSpec::Given::MONKEY && nassert.using_rspec_assertion?
  use_na
end

#_rg_then(&block) ⇒ Object

Implement the run-time semantics of the Then clause.



95
96
97
98
99
100
# File 'lib/rspec/given/extensions.rb', line 95

def _rg_then(&block)      # :nodoc:
  _rg_establish_givens
  _rg_check_invariants
  _rg_evaluate("Then", block)
  _rg_check_ands
end