Sha256: 5a47d9c1a99de094f52bd711a0ebf86a21e8c0fe9cb80c71b920936f0d7a79e0

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

module Riot
  RootContext = Struct.new(:setups, :teardowns)
  class Context
    attr_reader :description
    def initialize(description, parent=RootContext.new([],[]), &definition)
      @parent = parent
      @description = description
      @contexts, @setups, @assertions, @teardowns = [], [], [], []
      self.instance_eval(&definition)
    end

    def setups; @parent.setups + @setups; end
    def teardowns; @parent.teardowns + @teardowns; end
    
    def setup(&definition) (@setups << Setup.new(&definition)).last; end
    def teardown(&definition) (@teardowns << Setup.new(&definition)).last; end

    def asserts(what, &definition) new_assertion("asserts", what, &definition); end
    def should(what, &definition) new_assertion("should", what, &definition); end
    def asserts_topic; asserts("topic") { topic }; end

    def context(description, &definition)
      @contexts << Context.new("#{@description} #{description}", self, &definition)
    end
    
    def run(reporter)
      reporter.describe_context(self) unless @assertions.empty?
      local_run(reporter, Situation.new)
      run_sub_contexts(reporter)
      reporter
    end
  private
    def local_run(reporter, situation)
      (setups + @assertions + teardowns).each do |runnable|
        reporter.report(runnable.to_s, runnable.run(situation))
      end
    end

    def run_sub_contexts(reporter) @contexts.each { |ctx| ctx.run(reporter) }; end

    def new_assertion(scope, what, &definition)
      (@assertions << Assertion.new("#{scope} #{what}", &definition)).last
    end
  end # Context
end # Riot

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
riot-0.10.5 lib/riot/context.rb