class MiniTest::Check::Collector

Constants

Record

Public Class Methods

new() click to toggle source
# File lib/minitest-check.rb, line 119
def initialize
  # TODO: better storage mechanism
  # Creates a hash like:
  #
  #  {
  #   "stat_foo" => {
  #     "value_1" => Record.new(count, Set of contexts producing this value)
  #   }
  # }
  @store = Hash.new {|s, n|
    s[n] = Hash.new {|n, v|
      n[v] = Record.new
    }
  }
end

Public Instance Methods

report(io = STDOUT) click to toggle source
# File lib/minitest-check.rb, line 140
def report(io = STDOUT)
  return if @store.length == 0

  io.puts
  io.puts "Collected data for #{@store.length} probes during checks:"
  @store.each do |name, data|
    io.puts
    io.puts name
    io.puts
    draw_graph(
      data.map {|(value, record)| [value.to_s, record.count] },
      io
    )
  end
end
update(name, value, context = nil) click to toggle source
# File lib/minitest-check.rb, line 135
def update(name, value, context = nil)
  @store[name][value].count += 1
  @store[name][value].contexts << context
end

Private Instance Methods

draw_graph(pairs, io, max_width = 80) click to toggle source
# File lib/minitest-check.rb, line 157
def draw_graph(pairs, io, max_width = 80)
  if pairs
    data_groups = pairs.group_by {|d| d[1] == 1 ? :singles : :multis }
    if multis = data_groups[:multis]
      data = multis.to_a.sort {|a,b| b[1] <=> a[1]}
      largest_value_width = data.map {|d| d[0].length}.max
      largest_num = data[0][1]
      scale = if largest_num > 0
        (max_width - largest_value_width - 3).to_f / largest_num.to_f
      else
        0
      end

      data.each do |(value, num)|
        num_string = num.to_s
        num_length = num_string.length
        bar_length = (num.to_f * scale).to_i
        fill_length = bar_length - num_length
        if fill_length > 0
          io.puts "#{value.to_s.ljust(largest_value_width)} | #{num_string}#{'#' * fill_length}"
        else
          io.puts "#{value.to_s.ljust(largest_value_width)} | #{'#' * bar_length} (#{num_string})"
        end
      end
    end

    if singles = data_groups[:singles]
      io.puts
      io.puts "#{singles.length} singles: #{singles.map {|d| d[0].inspect}.join(", ")}"
    end
  end
end