Sha256: 02c998ec6997b5c6625e85fa8f157180cb4f1ecf16841287aae83994caf6991c

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

module Riot
  class Situation; end

  class Context
    # The protein
    attr_reader :description, :assertions, :situation
    def initialize(description, reporter, parent=nil)
      @description, @reporter = description, reporter
      @assertions = []
      @parent = parent
      @situation = Situation.new
      bootstrap(@situation)
    end

    def bootstrap(a_situation)
      @parent.bootstrap(a_situation) if @parent # Walk it out
      induce_local_setup(a_situation)
    end

    # something smelly between setup() and bootstrap()
    def setup(&block)
      @setup = block
      make_situation_topical(induce_local_setup(situation))
    end

    # DSLee stuff
    def context(description, &block) Riot.context(description, @reporter, self, &block); end
    def asserts(description, &block) new_assertion("asserts #{description}", &block); end
    def should(description, &block) new_assertion("should #{description}", &block); end

    # In conclusion
    def report
       # we should just be passing assertions to the reporter and building better descriptions earlier
      assertions.each do |assertion|
        if assertion.passed?
          @reporter.passed
        else
          result = assertion.result.contextualize(self)
          @reporter.send( (assertion.errored? ? :errored : :failed), result)
        end
      end
    end

    def to_s; @to_s ||= [@parent.to_s, @description].join(' ').strip; end
  private
    def new_assertion(description, &block)
      (assertions << Assertion.new(description, @situation, &block)).last
    end

    def induce_local_setup(a_situation)
      a_situation.instance_eval(&@setup) if @setup
    end

    def make_situation_topical(topic)
      situation.instance_variable_set(:@topic, topic)
      situation.instance_eval { def topic; @topic; end }
    end
  end # Context
end # Riot

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
riot-0.9.1 lib/riot/context.rb
riot-0.9.0 lib/riot/context.rb