class TestUnitHeckler

Public Instance Methods

tests_pass?() click to toggle source
# File lib/test_unit_heckler.rb, line 107
def tests_pass?
  silence_stream do
    if @@focus and @method_name then
      name = normal_to_test @method_name.to_s
      ARGV.clear
      ARGV << "--name=/#{name}/"
    end
    result = Test::Unit::AutoRunner.run
    ARGV.clear
    result
  end
end

Public Class Methods

focus=(value) click to toggle source
# File lib/test_unit_heckler.rb, line 26
def self.focus=(value)
  @@focus = value
end
load_test_files() click to toggle source
# File lib/test_unit_heckler.rb, line 30
def self.load_test_files
  @@tests_loaded = true
  Dir.glob(@@test_pattern).each {|test| require test}
end
new(klass_name=nil, method_name=nil, nodes=Heckle::MUTATABLE_NODES) click to toggle source
# File lib/test_unit_heckler.rb, line 100
def initialize(klass_name=nil, method_name=nil, nodes=Heckle::MUTATABLE_NODES)
  super
  self.class.load_test_files unless @@tests_loaded
end
test_pattern=(value) click to toggle source
# File lib/test_unit_heckler.rb, line 22
def self.test_pattern=(value)
  @@test_pattern = value
end
validate(klass_name, method_name = nil, nodes = Heckle::MUTATABLE_NODES, force = false) click to toggle source
# File lib/test_unit_heckler.rb, line 35
def self.validate(klass_name, method_name = nil,
                  nodes = Heckle::MUTATABLE_NODES, force = false)
  load_test_files
  klass = klass_name.to_class

  # Does the method exist?
  klass_methods = klass.singleton_methods(false).collect {|meth| "self.#{meth}"}
  if method_name
    if method_name =~ %rself\./
      abort "Unknown method: #{klass_name}.#{method_name.gsub('self.', '')}" unless klass_methods.include? method_name
    else
      abort "Unknown method: #{klass_name}##{method_name}" unless klass.instance_methods(false).include? method_name
    end
  end

  initial_time = Time.now

  heckle = self.new(klass_name)

  passed = heckle.tests_pass?

  unless force or passed then
    abort "Initial run of tests failed... fix and run heckle again"
  end

  if self.guess_timeout? then
    running_time = Time.now - initial_time
    adjusted_timeout = (running_time * 2 < 5) ? 5 : (running_time * 2).ceil
    self.timeout = adjusted_timeout
  end

  puts "Timeout set to #{adjusted_timeout} seconds."

  if passed then
    puts "Initial tests pass. Let's rumble."
  else
    puts "Initial tests failed but you forced things. Let's rumble."
  end
  puts

  methods = method_name ? Array(method_name) : klass.instance_methods(false) + klass_methods

  counts = Hash.new(0)
  methods.sort.each do |method_name|
    result = self.new(klass_name, method_name, nodes).validate
    counts[result] += 1
  end
  all_good = counts[false] == 0

  puts "Heckle Results:"
  puts
  puts "Passed    : %3d" % counts[true]
  puts "Failed    : %3d" % counts[false]
  puts "Thick Skin: %3d" % counts[nil]
  puts

  if all_good then
    puts "All heckling was thwarted! YAY!!!"
  else
    puts "Improve the tests and try again."
  end

  all_good
end