Class: Pork::Stat

Inherits:
Struct
  • Object
show all
Defined in:
lib/pork/stat.rb

Constant Summary collapse

Imp =
Module.new do
  def initialize io=$stdout, st=Time.now, mu=Mutex.new,
                 t=0, a=0, s=0, f=0, e=0, x=[]
    super
  end
  def incr_assertions; mutex.synchronize{ self.assertions += 1 }; end
  def incr_tests     ; mutex.synchronize{ self.tests      += 1 }; end
  def incr_skips     ; mutex.synchronize{ self.skips      += 1 }; end
  def add_failure err
    mutex.synchronize do
      self.failures += 1
      exceptions << err
    end
  end
  def add_error err
    mutex.synchronize do
      self.errors += 1
      exceptions << err
    end
  end
  def case_pass    msg='.'; io.print msg; end
  def case_skip    msg='s'; io.print msg; end
  def case_failed  msg='F'; io.print msg; end
  def case_errored msg='E'; io.print msg; end
  def passed?; exceptions.size == 0                        ; end
  def numbers; [tests, assertions, failures, errors, skips]; end
  def time_spent; Time.now - start; end
  def report
    io.puts
    io.puts report_exceptions
    io.printf("\nFinished in %s seconds.\n", time_spent)
    io.printf("%s tests, %s assertions, %s failures, %s errors, %s skips\n",
              *numbers)
  end
  def merge stat
    self.class.new(io, start, mutex,
      *to_a.drop(3).zip(stat.to_a.drop(3)).map{ |(a, b)| a + b })
  end

  private
  def report_exceptions
    exceptions.reverse_each.map do |(err, msg, source_location)|
      "\n  #{show_command(source_location)}"   \
      "\n  #{show_backtrace(err)}" \
      "\n#{message(msg)}"          \
      "\n#{show_exception(err)}"
    end
  end

  def show_command source_location
    "Replicate this test with:\n#{command(source_location)}"
  end

  def command source_location
    "#{env(source_location)} #{Gem.ruby} -S #{$0} #{ARGV.join(' ')}"
  end

  def show_backtrace err
    backtrace(err).join("\n  ")
  end

  def backtrace err
    if $VERBOSE
      err.backtrace
    else
      strip(err.backtrace.reject{ |l| l =~ %r{/lib/pork(/\w+)*\.rb:\d+} })
    end
  end

  def message msg
    msg
  end

  def show_exception err
    "#{err.class}: #{err.message}"
  end

  def strip bt
    strip_home(strip_cwd(bt))
  end

  def strip_home bt
    bt.map{ |path| path.sub(ENV['HOME'], '~') }
  end

  def strip_cwd bt
    bt.map{ |path| path.sub("#{Dir.pwd}/", '') }
  end

  def env source_location
    "env #{pork(source_location)} #{pork_mode} #{pork_seed}"
  end

  def pork source_location
    file, line = source_location
    "PORK_TEST='#{strip([file]).join}:#{line}'"
  end

  def pork_mode
    "PORK_MODE=#{Pork.execute_mode}"
  end

  def pork_seed
    "PORK_SEED=#{Pork.seed}"
  end
end

Instance Attribute Summary collapse

Instance Attribute Details

#assertionsObject

Returns the value of attribute assertions

Returns:

  • (Object)

    the current value of assertions



5
6
7
# File 'lib/pork/stat.rb', line 5

def assertions
  @assertions
end

#errorsObject

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



5
6
7
# File 'lib/pork/stat.rb', line 5

def errors
  @errors
end

#exceptionsObject

Returns the value of attribute exceptions

Returns:

  • (Object)

    the current value of exceptions



5
6
7
# File 'lib/pork/stat.rb', line 5

def exceptions
  @exceptions
end

#failuresObject

Returns the value of attribute failures

Returns:

  • (Object)

    the current value of failures



5
6
7
# File 'lib/pork/stat.rb', line 5

def failures
  @failures
end

#ioObject

Returns the value of attribute io

Returns:

  • (Object)

    the current value of io



5
6
7
# File 'lib/pork/stat.rb', line 5

def io
  @io
end

#mutexObject

Returns the value of attribute mutex

Returns:

  • (Object)

    the current value of mutex



5
6
7
# File 'lib/pork/stat.rb', line 5

def mutex
  @mutex
end

#skipsObject

Returns the value of attribute skips

Returns:

  • (Object)

    the current value of skips



5
6
7
# File 'lib/pork/stat.rb', line 5

def skips
  @skips
end

#startObject

Returns the value of attribute start

Returns:

  • (Object)

    the current value of start



5
6
7
# File 'lib/pork/stat.rb', line 5

def start
  @start
end

#testsObject

Returns the value of attribute tests

Returns:

  • (Object)

    the current value of tests



5
6
7
# File 'lib/pork/stat.rb', line 5

def tests
  @tests
end