Module: Given::ClassExtensions

Defined in:
lib/given/extensions.rb

Overview

Provide run-time class methods to support Given infrastructure. Methods that begin with Gvn are considered private and implementation specific, and should not be directly called by appliation code. Other methods without the Gvn prefix are public and intended for use by the application developer.

(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_and_blocksObject

List of the and blocks directly in the current describe/context block.



149
150
151
# File 'lib/given/extensions.rb', line 149

def _Gvn_and_blocks         # :nodoc:
  @_Gvn_and_blocks ||= []
end

#_Gvn_context_infoObject

Context information ofr the current describe/context block.



154
155
156
# File 'lib/given/extensions.rb', line 154

def _Gvn_context_info       # :nodoc:
  @_Gvn_context_info ||= {}
end

#_Gvn_givensObject

List of all givens directly in the current describe/context block.



137
138
139
# File 'lib/given/extensions.rb', line 137

def _Gvn_givens            # :nodoc:
  @_Gvn_givens ||= []
end

#_Gvn_invariantsObject

List of all invariants directly in the current describe/context block.



143
144
145
# File 'lib/given/extensions.rb', line 143

def _Gvn_invariants        # :nodoc:
  @_Gvn_invariants ||= []
end

#_Gvn_linesObject

Line extractor for the context.



159
160
161
# File 'lib/given/extensions.rb', line 159

def _Gvn_lines              # :nodoc:
  @_Gvn_lines ||= LineExtractor.new
end

#_Gvn_trigger_given(name) ⇒ Object

Trigger the evaluation of a Given! block by referencing its name.



165
166
167
# File 'lib/given/extensions.rb', line 165

def _Gvn_trigger_given(name) # :nodoc:
  Proc.new { send(name) }
end

#_Gvn_when_actions(block) ⇒ Object

Normal When clause actions.



214
215
216
217
218
219
# File 'lib/given/extensions.rb', line 214

def _Gvn_when_actions(block)        # :nodoc:
  _Gvn_before do
    _gvn_establish_givens
    instance_eval(&block)
  end
end

#_Gvn_when_actions_with_capture(name, block) ⇒ Object

Normal When clause actions except that exceptions are captured in a Failure object.



223
224
225
226
227
228
229
230
231
# File 'lib/given/extensions.rb', line 223

def _Gvn_when_actions_with_capture(name, block) # :nodoc:
  let(name) do
    Failure.capture(Given.pending_error) do
      _gvn_establish_givens
      instance_eval(&block)
    end
  end
  _Gvn_before do __send__(name) end
end

#And(&block) ⇒ Object

Provide an assertion that shares setup with a peer Then command.



257
258
259
260
# File 'lib/given/extensions.rb', line 257

def And(&block)
  fail "And defined without a Then" unless _Gvn_context_info[:then_defined]
  _Gvn_and_blocks << block
end

#Given(*args, &block) ⇒ Object

Declare a “given” of the current specification. If the given is named, the block will be lazily evaluated the first time the given is mentioned by name in the specification. If the given is unnamed, the block is evaluated for side effects every time the specification is executed.

:call-seq:

Given(:name) { ... code ... }
Given { ... code ... }


179
180
181
182
183
184
185
# File 'lib/given/extensions.rb', line 179

def Given(*args, &block)
  if args.first.is_a?(Symbol)
    let(args.first, &block)
  else
    _Gvn_givens << block
  end
end

#Given!(name, &block) ⇒ Object

Declare a named given of the current specification. Similar to the named version of the “Given” command, except that the block is always evaluated.

:call-seq:

Given!(:name) { ... code ... }


194
195
196
197
# File 'lib/given/extensions.rb', line 194

def Given!(name, &block)
  let(name, &block)
  _Gvn_givens << _Gvn_trigger_given(name)
end

#Invariant(&block) ⇒ Object

Establish an invariant that must be true for all Then blocks in the current (and nested) scopes.



252
253
254
# File 'lib/given/extensions.rb', line 252

def Invariant(&block)
  _Gvn_invariants << block
end

#Then(&block) ⇒ Object

Provide an assertion about the specification.

Then supplies an assertion that should be true after all the Given and When blocks have been run. All invariants in scope will be checked before the Then block is run.

:call-seq:

Then { ... assertion ... }


242
243
244
245
246
247
248
# File 'lib/given/extensions.rb', line 242

def Then(&block)
  file, line = Given.location_of(block)
  description = _Gvn_lines.line(file, line) unless Given.source_caching_disabled
  cmd = description ? "it(description)" : "specify"
  eval %{#{cmd} do _gvn_then(&block) end}, binding, file, line
  _Gvn_context_info[:then_defined] = true
end

#use_natural_assertions(enabled = true) ⇒ Object

Configure the use of natural assertions in this context.



263
264
265
266
# File 'lib/given/extensions.rb', line 263

def use_natural_assertions(enabled=true)
  Given.ok_to_use_natural_assertions(enabled)
  _Gvn_context_info[:natural_assertions_enabled] = enabled
end

#When(*args, &block) ⇒ Object

Declare the code that is under test.

:call-seq:

When(:named_result) { ... code_under_test ... }
When { ... code_under_test ... }


205
206
207
208
209
210
211
# File 'lib/given/extensions.rb', line 205

def When(*args, &block)
  if args.first.is_a?(Symbol)
    _Gvn_when_actions_with_capture(args.first, block)
  else
    _Gvn_when_actions(block)
  end
end