Class: ZombieTestChaser

Inherits:
Chaser
  • Object
show all
Defined in:
lib/zombie-chaser/zombie_test_chaser.rb

Constant Summary collapse

VERSION =

This should be used, but isn’t, in Rakefile.

'0.1.0'
@@test_pattern =
'test/test_*.rb'
@@tests_loaded =
false
@@world =
nil

Constants inherited from Chaser

Chaser::NULL_PATH, Chaser::WINDOZE

Instance Attribute Summary

Attributes inherited from Chaser

#klass, #klass_name, #method, #method_name, #old_method

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chaser

#aliasing_class, #calculate_proxy_method_name, #clean_method_name, debug, debug=, guess_timeout?, #interface_puts, #modify_class_method, #modify_instance_method, #modify_method, #mutate_value, #rand_number, #rand_range, #rand_string, #rand_symbol, #record_passing_mutation, #run_tests, #silence_stream, #tests_pass?, timeout=, #unmodify_class_method, #unmodify_instance_method, #unmodify_method, #validate

Constructor Details

#initialize(klass_name = nil, method_name = nil) ⇒ ZombieTestChaser

Returns a new instance of ZombieTestChaser.



134
135
136
137
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 134

def initialize(klass_name=nil, method_name=nil)
  self.class.create_world unless @@tests_loaded
  super(klass_name, method_name, Reporter.new(self.class.world.interface))
end

Class Method Details

.create_worldObject



26
27
28
29
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 26

def self.create_world
  @@tests_loaded = true
  @@world = World.new_using_test_unit_handler(@@test_pattern)
end

.current_class_names(exclude_list) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 35

def self.current_class_names(exclude_list)
  result = []
  ObjectSpace.each_object(Class) do |klass|
    next if klass.to_s.include?("Class:0x")
    next unless klass.ancestors.all? {|ancestor| (ancestor.to_s.split(/::/) & exclude_list).empty?}
    result << klass.to_s
  end
  result
end

.test_pattern=(value) ⇒ Object



22
23
24
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 22

def self.test_pattern=(value)
  @@test_pattern = value
end

.validate(klass_name = nil, method_name = nil, force = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 45

def self.validate(klass_name = nil, method_name = nil, force = false)
  pre_existing_class_names = self.current_class_names([]) unless klass_name
  create_world

  if klass_name
    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 =~ /self\./
        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).map{|sym| sym.to_s}.include? method_name
      end
    end
  end

  initial_time = Time.now

  chaser = self.new(klass_name)

  all_good = nil

  chaser.while_world_running do

    passed = chaser.human_survives?

    unless force or passed then
      abort "Initial run of tests failed... fix and run chaser 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

    chaser.interface_puts "Timeout set to #{adjusted_timeout} seconds."

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

    counts = Hash.new(0)

    klass_names = klass_name ? Array(klass_name) : self.current_class_names(["Test"]) - pre_existing_class_names
    klass_names.each do |block_klass_name|
      block_klass = block_klass_name.to_class

      methods = method_name ? Array(method_name) : block_klass.instance_methods(false) + block_klass.singleton_methods(false).collect {|meth| "self.#{meth}"}

      methods.sort_by{|x| x.to_s}.each do |block_method_name|
        result = self.new(block_klass_name, block_method_name).validate
        counts[result] += 1
      end
    end
    all_good = counts[false] == 0

    chaser.interface_puts "Chaser Results:",
                          "",
                          "Passed    : %3d" % counts[true],
                          "Failed    : %3d" % counts[false],
                          ""

    if all_good then
      chaser.interface_puts "All chasing was thwarted! YAY!!!"
    else
      chaser.interface_puts "Improve the tests and try again."
    end

  end
  all_good
end

.worldObject



31
32
33
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 31

def self.world
  @@world
end

Instance Method Details

#human_survives?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 122

def human_survives?
  self.class.world.run_human
end

#while_world_runningObject



130
131
132
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 130

def while_world_running
  self.class.world.while_world_running{yield}
end

#zombie_survives?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/zombie-chaser/zombie_test_chaser.rb', line 126

def zombie_survives?
  self.class.world.run_next_zombie
end