Sha256: 9410d6d0dd6c4b25f89a792c76050a0da42f8da3d0f409e45523b5956569761b

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

require "json"
require "teabag/result"

module Teabag
  class Runner

    attr_accessor :formatters
    attr_reader   :failure_count

    def initialize(suite_name = :default)
      @suite_name = suite_name
      @formatters = Teabag.configuration.formatters.split(/,\s+/).map{ |f| resolve_formatter(f).new }
      @failure_count = 0
    end

    def process(line)
      return if output_from(line)
      log line unless Teabag.configuration.suppress_log
    end

    private

    def resolve_formatter(formatter)
      Teabag::Formatters.const_get("#{formatter.to_s.camelize}Formatter")
    end

    def output_from(line)
      json = JSON.parse(line)
      return false unless json["_teabag"] && json["type"]
      result = Teabag::Result.build_from_json(@suite_name, json)
      notify_formatters result
      @failure_count += 1 if result.failing?
      return true
    rescue JSON::ParserError
      false
    end

    def notify_formatters(result)
      @formatters.each do |formatter|
        event = result.type
        formatter.send(event, result) if formatter.respond_to?(event)
      end
    end

    def log(msg)
      STDOUT.print msg
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
teabag-0.3.5 lib/teabag/runner.rb
teabag-0.3.4 lib/teabag/runner.rb
teabag-0.3.3 lib/teabag/runner.rb