module Minitest
  class TestSweetReporter < Reporter
    attr_accessor :results, :suite_start, :suite_end

    def self.options
      {}
    end

    def self.enable!(options = {})
      self.options.merge!(options)
    end

    def initialize(io = STDOUT, options = self.class.options)
      @suite_start = Minitest.clock_time
      @results = []
      super
    end
    
    def record(result)
      results << result
    end
    
    def report
      @suite_end = Minitest.clock_time
      TestSweet::Client.report({
        suite_start: suite_start,
        suite_end: suite_end,
        system_stats: TestSweet::SystemStats.basic,
        payload_version: "minitest_v1",
        results: results.map do |r|
          file, line = r.source_location
          {
            name:            r.name,
            allocations:     r.allocations,
            time:            r.time,
            klass:           r.klass,
            failures:        r.failures,
            line:            line,
            file:            file,
            assertions:      r.assertions,
            result:          r.result_code
          }
        end
      })
    end

  end
end