Class: Riot::Situation

Inherits:
Object show all
Defined in:
lib/riot/situation.rb

Overview

A Situation is virtually a stack frame for a single context run. The intent is for all blocks to be evaluated against an instance of a Situation. Given the following superfluous context:

context "Foo" do # block-1

  setup do # block-2
    {:bar => "baz"}
  end

  asserts "its hash" do # block-3
    topic
  end.equals do # block-4
    {:bar => "baz"}
  end

end # Foo

In this example, block-1 will be evaluated against a Context instance. Whereas block-2, block-3, and block-4 will all be evaluated against the same Situation instance. Situation instances (situations) are bound to a single context run; they are not shared across context runs, regardless of their position in the test tree structure.

What is gained from doing it this way is:

  • variables, methods, etc. set in one situation do not contaminate any others

  • variables, methods, etc. defined during a context run do not stick with the context itself

  • which means that testing state is independent of the test definitions themselves

Direct Known Subclasses

RR::Situation

Instance Method Summary collapse

Instance Method Details

#evaluate(&block) ⇒ Object

Anonymously evaluates any block given to it against the current instance of self. This is how assertion and assertion macro blocks are evaluated, for instance.

Parameters:

  • &block (lambda)

    the block to evaluate against self

Returns:

  • (Object)

    whatever the block would have returned



60
61
62
# File 'lib/riot/situation.rb', line 60

def evaluate(&block)
  self.instance_eval(&block)
end

#helper(name, &block) ⇒ Object

This is where a defined helper is born. A method is defined against the current instance of self. This method will not be defined on any other instances of Situation created afterwards.

Parameters:

  • name (Symbol, String)

    the name of the helper being defined

  • &block (lambda)

    the code to execute whennever the helper is called



50
51
52
# File 'lib/riot/situation.rb', line 50

def helper(name, &block)
  (class << self; self; end).send(:define_method, name, &block)
end

#setup(&block) ⇒ Object

This is where a setup block is actually evaluated and the topic tracked.

Parameters:

  • &block (lambda)

    a setup block

Returns:

  • (Object)

    the current topic value



41
42
43
# File 'lib/riot/situation.rb', line 41

def setup(&block)
  @_topic = self.instance_eval(&block)
end

#topicObject

Returns the currrently tracked value of topic

Returns:

  • (Object)

    whatever the topic is currently set to



33
34
35
# File 'lib/riot/situation.rb', line 33

def topic
  @_topic ||= nil
end