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.



157
158
159
# File 'lib/given/extensions.rb', line 157

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

#_Gvn_context_infoObject

Context information ofr the current describe/context block.



162
163
164
# File 'lib/given/extensions.rb', line 162

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

#_Gvn_givensObject

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



145
146
147
# File 'lib/given/extensions.rb', line 145

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

#_Gvn_invariantsObject

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



151
152
153
# File 'lib/given/extensions.rb', line 151

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

#_Gvn_linesObject

Line extractor for the context.



167
168
169
# File 'lib/given/extensions.rb', line 167

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.



173
174
175
# File 'lib/given/extensions.rb', line 173

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

#_Gvn_when_actions(block) ⇒ Object

Normal When clause actions.



222
223
224
225
226
227
# File 'lib/given/extensions.rb', line 222

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.



231
232
233
234
235
236
237
238
239
# File 'lib/given/extensions.rb', line 231

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.



266
267
268
269
# File 'lib/given/extensions.rb', line 266

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 ... }


187
188
189
190
191
192
193
# File 'lib/given/extensions.rb', line 187

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 ... }


202
203
204
205
# File 'lib/given/extensions.rb', line 202

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.



261
262
263
# File 'lib/given/extensions.rb', line 261

def Invariant(&block)
  _Gvn_invariants << block
end

#Then(opts = {}, &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 ... }


250
251
252
253
254
255
256
257
# File 'lib/given/extensions.rb', line 250

def Then(opts={}, &block)
  on_eval = opts.fetch(:on_eval, "_gvn_then")
  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 #{on_eval}(&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.



272
273
274
275
# File 'lib/given/extensions.rb', line 272

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 ... }


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

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