class Spectre::Runner

Public Class Methods

current() click to toggle source
# File lib/spectre.rb, line 121
def self.current
  @@current
end

Public Instance Methods

run(specs) click to toggle source
# File lib/spectre.rb, line 125
def run specs
  runs = []

  specs.group_by { |x| x.subject }.each do |subject, spec_group|
    Logger.log_subject subject do
      spec_group.group_by { |x| x.context }.each do |context, specs|
        Logger.log_context(context) do
          runs.concat run_context(context, specs)
        end
      end
    end
  end

  runs
end

Private Instance Methods

run_blocks(name, context, blocks) click to toggle source
# File lib/spectre.rb, line 175
def run_blocks name, context, blocks
  ctx = SpecContext.new context.__subject, name
  spec = Spec.new name, context.__subject, name, [], nil, nil, ctx, nil

  run_info = RunInfo.new spec

  @@current = run_info

  run_info.started = Time.now

  Logger.log_context ctx do
    begin
      blocks.each do |block|
        block.call
      end

      run_info.finished = Time.now

    rescue ExpectationFailure => e
      run_info.failure = e

    rescue Exception => e
      run_info.error = e
      Logger.log_error spec, e

    end
  end

  run_info.finished = Time.now

  @@current = nil

  run_info
end
run_context(context, specs) click to toggle source
# File lib/spectre.rb, line 143
def run_context context, specs
  runs = []

  if context.__setup_blocks.count > 0
    setup_run = run_blocks('setup', context, context.__setup_blocks)
    runs << setup_run
    return runs if setup_run.error or setup_run.failure
  end

  begin
    specs.each do |spec|
      if spec.data.any?
        spec.data.each do |data|
          Logger.log_spec(spec, data) do
            runs << run_spec(spec, data)
          end
        end
      else
        Logger.log_spec(spec) do
          runs << run_spec(spec)
        end
      end
    end
  ensure
    if context.__teardown_blocks.count > 0
      runs << run_blocks('teardown', context, context.__teardown_blocks)
    end
  end

  runs
end
run_spec(spec, data=nil) click to toggle source
# File lib/spectre.rb, line 210
def run_spec spec, data=nil
  run_info = RunInfo.new(spec, data)

  @@current = run_info

  run_info.started = Time.now

  begin
    if spec.context.__before_blocks.count > 0
      before_ctx = SpecContext.new(spec.subject, 'before')

      Logger.log_context before_ctx do
        spec.context.__before_blocks.each do |block|
          block.call(data)
        end
      end
    end

    spec.block.call(data)

  rescue ExpectationFailure => e
    run_info.failure = e

  rescue Interrupt
    run_info.skipped = true
    Logger.log_skipped spec

  rescue Exception => e
    run_info.error = e
    Logger.log_error spec, e

  ensure
    if spec.context.__after_blocks.count > 0
      after_ctx = SpecContext.new(spec.subject, 'after')

      Logger.log_context after_ctx do
        begin
          spec.context.__after_blocks.each do |block|
            block.call
          end

          run_info.finished = Time.now

        rescue ExpectationFailure => e
          run_info.failure = e

        rescue Exception => e
          run_info.error = e
          Logger.log_error spec, e

        end
      end
    end
  end

  run_info.finished = Time.now

  @@current = nil

  run_info
end