# encoding: utf-8

module BareTest
  class Formatter
    class Vows < Formatter
      register 'baretest/formatter/vows'

      Symbols = {
        :success => '✓',
        :skipped => '-',
        :pending => '-',
        :failure => '✗',
        :error   => '✗'
      }

      Labels = {
        :success => "\e[1;32mOK\e[0m",
        :pending => "\e[1;96mPending\e[0m",
        :failure => "\e[1;31mBroken\e[0m",
        :error   => "\e[1;31mBroken\e[0m"
      }

      Colors = {
        :success => "\e[32m",
        :skipped => "\e[30m",
        :pending => "\e[36m",
        :failure => "\e[31m",
        :error   => "\e[31m",
        :normal  => "\e[0m"
      }

      def stylize status, string
        Colors[status] + string + Colors[:normal]
      end

      def start_suite suite
        defer do
          if suite.description
            puts indent(suite, -1) + suite.description
          end
        end
      end

      def end_test test, status, elapsed
        apply_deferred

        puts indent(test, -1) + Symbols[status.code] +
        ' ' + stylize(status.code, test.description)

        case status.code
          when :pending, :skipped, :failure
            puts indent(test) + '» ' + stylize(status.code, status.reason)
          when :error
            backtrace = backtrace(status).join("\n")

            puts indent(test) + '» ' + stylize(status.code, status.reason)
            puts indent(test) + '» ' + stylize(status.code, backtrace)
        end
      end
      
      def end_all status_collection, elapsed
        success, pending, skipped,
        failure, error = *status_collection.values_at(:success,
                                                      :pending,
                                                      :skipped,
                                                      :failure,
                                                      :error)

        status = Symbols[status_collection.code] +
        ' ' + Labels[status_collection.code]
        status += " » \e[1m#{success}\e[0m honored" if success > 0
        status += " • \e[1m#{pending}\e[0m pending" if pending > 0
        status += " • \e[1m#{skipped}\e[0m skipped" if skipped > 0
        status += " • \e[1m#{failure}\e[0m failed"  if failure > 0
        status += " • \e[1m#{error}\e[0m broken"    if error   > 0
        status += " \e[30m(#{elapsed}s)\e[0m"

        puts status
      end
    end
  end
end
