Sha256: c79f75e16b021ef6549801534d7154353d57a20b5a02ec6bbf3cc45255473187

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Lopata
  module Observers
    class ConsoleOutputObserver < BaseObserver
      def finished(world)
        total = world.scenarios.length
        statuses = world.scenarios.map(&:status)
        counts = statuses.uniq.map do |status|
          colored("%d %s", status) % [statuses.count { |s| s == status }, status]
        end
        details = counts.empty? ? "" : "(%s)" % counts.join(', ')
        puts "#{total} scenario%s %s" % [total == 1 ? '' : 's', details]
      end

      def step_finished(step)
        @failed_steps << step if step.failed?
      end

      def scenario_started(scenario)
        @failed_steps = []
      end

      def scenario_finished(scenario)
        message = "#{scenario.title} #{bold(scenario.status.to_s.upcase)}"
        puts colored(message, scenario.status)

        @failed_steps.each do |step|
          if step.exception
            puts step.exception.message
            puts step.exception.backtrace.join("\n")
            puts
          end
        end
      end

      private

      def colored(text, status)
        case status
        when :failed then red(text)
        when :passed then green(text)
        else text
        end
      end

      def red(text)
        wrap(text, 31)
      end

      def green(text)
        wrap(text, 32)
      end

      def bold(text)
        wrap(text, 1)
      end

      def wrap(text, code)
        "\e[#{code}m#{text}\e[0m"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lopata-0.1.1 lib/lopata/observers/console_output_observer.rb
lopata-0.1.0 lib/lopata/observers/console_output_observer.rb