Module: Given::InstanceExtensions

Defined in:
lib/given/extensions.rb

Overview

Provide run-time instance methods to support Given infrastructure. All the methods in this module are considered private and implementation-specific, and should not be directly called by the application developer.

By convention, these private instance specific methods are prefixed with gvn to avoid name collisions with application methods defined in a spec.

(Note that private class methods are prefixed with Gvn and private instance methods are prefixed with gvn).

Instance Method Summary collapse

Instance Method Details

#_gvn_block_passed?(block) ⇒ Boolean

Determine of the natural assertion pass/fail status of the block

Returns:

  • (Boolean)


107
108
109
110
111
# File 'lib/given/extensions.rb', line 107

def _gvn_block_passed?(block) # :nodoc:
  passed = instance_eval(&block)
  passed = passed.to_bool if passed.respond_to?(:to_bool)
  passed
end

#_gvn_check_andsObject

:nodoc:



90
91
92
93
94
95
96
# File 'lib/given/extensions.rb', line 90

def _gvn_check_ands  # :nodoc:
  return if self.class._Gvn_context_info[:and_ran]
  self.class._Gvn_and_blocks.each do |block|
    _gvn_evaluate("And", block)
  end
  self.class._Gvn_context_info[:and_ran] = true
end

#_gvn_check_invariantsObject

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



82
83
84
85
86
87
88
# File 'lib/given/extensions.rb', line 82

def _gvn_check_invariants  # :nodoc:
  _gvn_contexts.each do |context|
    context._Gvn_invariants.each do |block|
      _gvn_evaluate("Invariant", block)
    end
  end
end

#_gvn_contextsObject

List of containing contexts in order from outermost to innermost.



31
32
33
# File 'lib/given/extensions.rb', line 31

def _gvn_contexts          # :nodoc:
  _gvn_inner_contexts.reverse
end

#_gvn_establish_givensObject

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



70
71
72
73
74
75
76
77
78
# File 'lib/given/extensions.rb', line 70

def _gvn_establish_givens  # :nodoc:
  return if defined?(@_gvn_ran) && @_gvn_ran
  @_gvn_ran = true
  _gvn_contexts.each do |context|
    context._Gvn_givens.each do |block|
      instance_eval(&block)
    end
  end
end

#_gvn_evaluate(clause_type, block) ⇒ Object

Evaluate a Then, And, or Invariant assertion.



114
115
116
117
118
119
120
# File 'lib/given/extensions.rb', line 114

def _gvn_evaluate(clause_type, block)   # :nodoc:
  Given.start_evaluation
  passed = _gvn_block_passed?(block)
  if ! Given.explicit_assertions? && _gvn_na_configured?
    _gvn_naturally_assert(clause_type, block, passed)
  end
end

#_gvn_info(keyword) ⇒ Object

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



37
38
39
40
41
42
43
44
45
# File 'lib/given/extensions.rb', line 37

def _gvn_info(keyword)     # :nodoc:
  _gvn_inner_contexts.each do |context|
    h = context._Gvn_context_info
    if h.has_key?(keyword)
      return h[keyword]
    end
  end
  nil
end

#_gvn_inner_contextsObject

List of containing contexts in order from innermost to outermost.



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

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

#_gvn_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)


62
63
64
65
# File 'lib/given/extensions.rb', line 62

def _gvn_na_configured?    # :nodoc:
  info_value = _gvn_info(:natural_assertions_enabled)
  info_value.nil? ? Given.natural_assertions_enabled? : info_value
end

#_gvn_naturally_assert(clause_type, block, passed) ⇒ Object

Naturally assert the block (based on passed).



123
124
125
126
127
128
129
# File 'lib/given/extensions.rb', line 123

def _gvn_naturally_assert(clause_type, block, passed)
  Given.count_assertion
  unless passed
    nassert = NaturalAssertion.new(clause_type, block, self, self.class._Gvn_lines)
    Given.fail_with nassert.message if _gvn_need_na_message?(nassert)
  end
end

#_gvn_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. The configuration options for natural assertions are checked and applied accordingly.

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/given/extensions.rb', line 53

def _gvn_need_na_message?(nassert) # :nodoc:
  return false unless nassert.has_content?
  _gvn_na_configured?
end

#_gvn_then(&block) ⇒ Object

Implement the run-time semantics of the Then clause.



99
100
101
102
103
104
# File 'lib/given/extensions.rb', line 99

def _gvn_then(&block)      # :nodoc:
  _gvn_establish_givens
  _gvn_check_invariants
  _gvn_evaluate("Then", block)
  _gvn_check_ands
end