class Grntest::Reporters::BaseReporter

Constants

COLOR_NAMES
LONG_ELAPSED_TIME

Public Class Methods

new(tester) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 21
def initialize(tester)
  @tester = tester
  @term_width = guess_term_width
  @output = @tester.output
  @mutex = Mutex.new
  reset_current_column
end

Private Instance Methods

available_colors() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 400
def available_colors
  case ENV["COLORTERM"]
  when "gnome-terminal"
    256
  else
    case ENV["TERM"]
    when /-256color\z/
      256
    else
      8
    end
  end
end
colorize(message, result_or_status) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 271
def colorize(message, result_or_status)
  return message unless @tester.use_color?
  if result_or_status.is_a?(Symbol)
    status = result_or_status
  else
    status = result_status(result_or_status)
  end
  case status
  when :success
    "%s%s%s" % [success_color, message, reset_color]
  when :failure
    "%s%s%s" % [failure_color, message, reset_color]
  when :leaked
    "%s%s%s" % [leaked_color, message, reset_color]
  when :omitted
    "%s%s%s" % [omitted_color, message, reset_color]
  when :not_checked
    "%s%s%s" % [not_checked_color, message, reset_color]
  else
    message
  end
end
columns() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 53
def columns
  [
    # label,      format value
    ["tests/sec", lambda {|result| "%9.2f" % throughput(result)}],
    ["   tests",  lambda {|result| "%8d"   % result.n_tests}],
    ["  passes",  lambda {|result| "%8d"   % result.n_passed_tests}],
    ["failures",  lambda {|result| "%8d"   % result.n_failed_tests}],
    ["  leaked",  lambda {|result| "%8d"   % result.n_leaked_tests}],
    [" omitted",  lambda {|result| "%8d"   % result.n_omitted_tests}],
    ["!checked",  lambda {|result| "%8d"   % result.n_not_checked_tests}],
  ]
end
create_temporary_file(key, content) { |file| ... } click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 211
def create_temporary_file(key, content)
  file = Tempfile.new("groonga-test-#{key}")
  file.print(content)
  file.close
  yield(file)
end
elapsed_time_status(elapsed_time) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 175
def elapsed_time_status(elapsed_time)
  if long_elapsed_time?(elapsed_time)
    :failure
  else
    :not_checked
  end
end
escape_sequence(*commands) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 367
def escape_sequence(*commands)
  sequence = []
  commands.each do |command|
    case command
    when :reset
      sequence << "0"
    when :bold
      sequence << "1"
    when :italic
      sequence << "3"
    when :underline
      sequence << "4"
    when Hash
      foreground_p = !command[:background]
      if available_colors == 256
        sequence << (foreground_p ? "38" : "48")
        sequence << "5"
        sequence << pack_256_color(*command[:color_256])
      else
        color_parameter = foreground_p ? 3 : 4
        color_parameter += 6 if command[:intensity]
        color = COLOR_NAMES.index(command[:color])
        sequence << "#{color_parameter}#{color}"
      end
    end
  end
  "\e[#{sequence.join(';')}m"
end
failure_color() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 307
def failure_color
  escape_sequence({
                    :color => :red,
                    :color_256 => [3, 0, 0],
                    :background => true,
                  },
                  {
                    :color => :white,
                    :color_256 => [5, 5, 5],
                    :bold => true,
                  })
end
guess_term_width() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 218
def guess_term_width
  Integer(guess_term_width_from_env || guess_term_width_from_stty || 79)
rescue ArgumentError
  0
end
guess_term_width_from_env() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 224
def guess_term_width_from_env
  ENV["COLUMNS"] || ENV["TERM_WIDTH"]
end
guess_term_width_from_stty() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 228
def guess_term_width_from_stty
  return nil unless STDIN.tty?

  case tty_info
  when /(\d+) columns/
    $1
  when /columns (\d+)/
    $1
  else
    nil
  end
end
increment_current_column(message) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 203
def increment_current_column(message)
  @current_column += string_width(message.to_s)
end
justify(message, width) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 183
def justify(message, width)
  return " " * width if message.nil?
  return message.ljust(width) if message.bytesize <= width
  half_width = width / 2.0
  elision_mark = "..."
  left = message[0, half_width.ceil - elision_mark.size]
  right = message[(message.size - half_width.floor)..-1]
  "#{left}#{elision_mark}#{right}"
end
leaked_color() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 320
def leaked_color
  escape_sequence({
                    :color => :magenta,
                    :color_256 => [3, 0, 3],
                    :background => true,
                  },
                  {
                    :color => :white,
                    :color_256 => [5, 5, 5],
                    :bold => true,
                  })
end
long_elapsed_time?(elapsed_time) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 171
def long_elapsed_time?(elapsed_time)
  elapsed_time >= LONG_ELAPSED_TIME
end
not_checked_color() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 346
def not_checked_color
  escape_sequence({
                    :color => :cyan,
                    :color_256 => [0, 1, 1],
                    :background => true,
                  },
                  {
                    :color => :white,
                    :color_256 => [5, 5, 5],
                    :bold => true,
                  })
end
omitted_color() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 333
def omitted_color
  escape_sequence({
                    :color => :blue,
                    :color_256 => [0, 0, 1],
                    :background => true,
                  },
                  {
                    :color => :white,
                    :color_256 => [5, 5, 5],
                    :bold => true,
                  })
end
pack_256_color(red, green, blue) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 396
def pack_256_color(red, green, blue)
  red * 36 + green * 6 + blue + 16
end
print(message) click to toggle source
puts(*messages) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 198
def puts(*messages)
  reset_current_column
  @output.puts(*messages)
end
report_actual(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 95
def report_actual(result)
  report_marker(result)
  puts(result.actual)
  report_marker(result)
end
report_diff(expected, actual) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 105
def report_diff(expected, actual)
  if @tester.diff == "internal"
    run_diff_reporter(expected, actual)
  else
    run_diff_command(expected, actual)
  end
end
report_failure(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 89
def report_failure(result)
  report_marker(result)
  report_diff(result.expected, result.actual)
  report_marker(result)
end
report_full_test_name(worker) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 136
def report_full_test_name(worker)
  report_worker_id(worker) unless single_worker?
  print("#{worker.suite_name}/#{worker.test_name}")
end
report_marker(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 101
def report_marker(result)
  puts(colorize("=" * @term_width, result))
end
report_right_message(message) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 149
def report_right_message(message)
  message_width = string_width(message)
  rest_width = @term_width - @current_column
  if rest_width > message_width
    print(" " * (rest_width - message_width))
  end
  puts(message)
end
report_summary(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 44
def report_summary(result)
  puts(statistics_header)
  puts(colorize(statistics(result), result))
  pass_ratio = result.pass_ratio
  elapsed_time = result.real_elapsed_time
  summary = "%.4g%% passed in %.4fs." % [pass_ratio, elapsed_time]
  puts(colorize(summary, result))
end
report_test(worker, result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 141
def report_test(worker, result)
  report_marker(result)
  report_worker_id(worker) unless single_worker?
  puts(worker.suite_name)
  print("  #{worker.test_name}")
  report_test_result(result, worker.status)
end
report_test_result(result, label) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 158
def report_test_result(result, label)
  report_right_message(test_result_message(result, label))
end
report_worker_id(worker) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 131
def report_worker_id(worker)
  @worker_id_width ||= (Math.log10(@tester.n_workers) + 1).floor
  print("[%*d] " % [@worker_id_width, worker.id])
end
reset_color() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 359
def reset_color
  escape_sequence(:reset)
end
reset_current_column() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 207
def reset_current_column
  @current_column = 0
end
result_status(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 253
def result_status(result)
  if result.respond_to?(:status)
    result.status
  else
    if result.n_failed_tests > 0
      :failure
    elsif result.n_leaked_tests > 0
      :leaked
    elsif result.n_omitted_tests > 0
      :omitted
    elsif result.n_not_checked_tests > 0
      :not_checked
    else
      :success
    end
  end
end
run_diff_command(expected, actual) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 113
def run_diff_command(expected, actual)
  create_temporary_file("expected", expected) do |expected_file|
    create_temporary_file("actual", actual) do |actual_file|
      diff_options = @tester.diff_options.dup
      diff_options += [
        "--label", "(expected)", expected_file.path,
        "--label", "(actual)", actual_file.path,
      ]
      system(@tester.diff, *diff_options)
    end
  end
end
run_diff_reporter(expected, actual) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 126
def run_diff_reporter(expected, actual)
  reporter = DiffReporter.new(expected, actual)
  reporter.report
end
single_worker?() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 40
def single_worker?
  @tester.n_workers == 1
end
statistics(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 73
def statistics(result)
  items = columns.collect do |label, format_value|
    format_value.call(result)
  end
  "  " + items.join(" | ") + " |"
end
statistics_header() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 66
def statistics_header
  labels = columns.collect do |label, format_value|
    label
  end
  "  " + labels.join(" | ") + " |"
end
string_width(string) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 249
def string_width(string)
  string.gsub(/\e\[[0-9;]+m/, "").size
end
success_color() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 294
def success_color
  escape_sequence({
                    :color => :green,
                    :color_256 => [0, 3, 0],
                    :background => true,
                  },
                  {
                    :color => :white,
                    :color_256 => [5, 5, 5],
                    :bold => true,
                  })
end
synchronize() { || ... } click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 30
def synchronize
  if single_worker?
    yield
  else
    @mutex.synchronize do
      yield
    end
  end
end
test_result_message(result, label) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 162
def test_result_message(result, label)
  elapsed_time = result.real_elapsed_time
  formatted_elapsed_time = "%.4fs" % elapsed_time
  formatted_elapsed_time = colorize(formatted_elapsed_time,
                                    elapsed_time_status(elapsed_time))
  " #{formatted_elapsed_time} [#{colorize(label, result)}]"
end
throughput(result) click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 80
def throughput(result)
  if result.real_elapsed_time.zero?
    tests_per_second = 0
  else
    tests_per_second = result.n_tests / result.real_elapsed_time
  end
  tests_per_second
end
tty_info() click to toggle source
# File lib/grntest/reporters/base-reporter.rb, line 241
def tty_info
  begin
    `stty -a`
  rescue SystemCallError
    nil
  end
end