Skip to content

Commit

Permalink
Class method support
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/heckle/dev/": change = 2904]
  • Loading branch information
kevinclark committed Jan 14, 2007
1 parent 88afdf2 commit 273e59b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 27 deletions.
5 changes: 4 additions & 1 deletion History.txt
@@ -1,7 +1,10 @@
== svn == svn
* 1 minor enhancement * 1 minor enhancement
* -b allows heckling of branches only * -b allows heckling of branches only

* 2 major enhancement
* Timeout for tests set dynamically and overridable with -T
* Class method support with "self.method_name"

== 1.1.1 / 2006-12-20 == 1.1.1 / 2006-12-20


* 3 bug fixes: * 3 bug fixes:
Expand Down
57 changes: 35 additions & 22 deletions lib/heckle.rb
Expand Up @@ -10,7 +10,7 @@ def to_class
end end


class Heckle < SexpProcessor class Heckle < SexpProcessor
VERSION = '1.1.1' VERSION = '1.1.2'
MUTATABLE_NODES = [:if, :lit, :str, :true, :false, :while, :until] MUTATABLE_NODES = [:if, :lit, :str, :true, :false, :while, :until]
WINDOZE = RUBY_PLATFORM =~ /mswin/ WINDOZE = RUBY_PLATFORM =~ /mswin/
NULL_PATH = WINDOZE ? 'NUL:' : '/dev/null' NULL_PATH = WINDOZE ? 'NUL:' : '/dev/null'
Expand Down Expand Up @@ -38,28 +38,32 @@ def self.guess_timeout?


def initialize(klass_name=nil, method_name=nil, reporter = Reporter.new) def initialize(klass_name=nil, method_name=nil, reporter = Reporter.new)
super() super()


@klass_name, @method_name = klass_name, method_name.intern @klass_name = klass_name
@klass = @method = nil @method_name = method_name.intern if method_name

@klass = klass_name.to_class

@method = nil
@reporter = reporter @reporter = reporter

self.strict = false self.strict = false
self.auto_shift_type = true self.auto_shift_type = true
self.expected = Array self.expected = Array

@mutatees = Hash.new @mutatees = Hash.new
@mutation_count = Hash.new @mutation_count = Hash.new
@node_count = Hash.new @node_count = Hash.new
@count = 0 @count = 0

MUTATABLE_NODES.each {|type| @mutatees[type] = [] } MUTATABLE_NODES.each {|type| @mutatees[type] = [] }

@failures = [] @failures = []

@mutated = false @mutated = false

grab_mutatees grab_mutatees

@original_tree = current_tree.deep_clone @original_tree = current_tree.deep_clone
@original_mutatees = mutatees.deep_clone @original_mutatees = mutatees.deep_clone
end end
Expand Down Expand Up @@ -127,13 +131,19 @@ def heckle(exp)
raise e raise e
end end
@reporter.replacing(klass_name, method_name, src) if @@debug @reporter.replacing(klass_name, method_name, src) if @@debug
klass = klass_name.to_class
clean_name = method_name.to_s.gsub(/self\./, '')

self.count += 1 self.count += 1
new_name = "#{method_name}_#{count}"


klass.send :undef_method, new_name rescue nil new_name = "#{clean_name}_#{count}"
klass.send :alias_method, new_name, method_name
klass.class_eval(src) aliasing_class = (method_name.to_s =~ /self\./) ? (class << @klass; self end) : @klass

aliasing_class.send :undef_method, new_name rescue nil
aliasing_class.send :alias_method, new_name, clean_name

@klass.class_eval(src)
end end


############################################################ ############################################################
Expand Down Expand Up @@ -259,13 +269,16 @@ def reset_tree
return unless original_tree != current_tree return unless original_tree != current_tree
@mutated = false @mutated = false


klass = klass_name.to_class

self.count += 1 self.count += 1
new_name = "#{method_name}_#{count}"
klass.send :undef_method, new_name rescue nil clean_name = method_name.to_s.gsub(/self\./, '')
klass.send :alias_method, new_name, method_name new_name = "#{clean_name}_#{count}"
klass.send :alias_method, method_name, "#{method_name}_1"
aliasing_class = (method_name.to_s =~ /self\./) ? (class << @klass; self end) : @klass

aliasing_class.send :undef_method, new_name rescue nil
aliasing_class.send :alias_method, new_name, clean_name
aliasing_class.send :alias_method, clean_name, "#{clean_name}_1"
end end


def reset_mutatees def reset_mutatees
Expand Down
13 changes: 9 additions & 4 deletions lib/test_unit_heckler.rb
Expand Up @@ -20,8 +20,9 @@ def self.load_test_files
def self.validate(klass_name, method_name = nil) def self.validate(klass_name, method_name = nil)
load_test_files load_test_files
klass = klass_name.to_class klass = klass_name.to_class

initial_time = Time.now initial_time = Time.now

unless self.new(klass_name).tests_pass? then unless self.new(klass_name).tests_pass? then
abort "Initial run of tests failed... fix and run heckle again" abort "Initial run of tests failed... fix and run heckle again"
end end
Expand All @@ -35,9 +36,13 @@ def self.validate(klass_name, method_name = nil)
end end


puts "Initial tests pass. Let's rumble." puts "Initial tests pass. Let's rumble."

self.timeout = adjusted_timeout
methods = method_name ? Array(method_name) : klass.instance_methods(false)

puts "Initial tests pass. Let's rumble."

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

methods.each do |method_name| methods.each do |method_name|
self.new(klass_name, method_name).validate self.new(klass_name, method_name).validate
end end
Expand Down
4 changes: 4 additions & 0 deletions sample/lib/heckled.rb
Expand Up @@ -5,6 +5,10 @@ def initialize
@names = [] @names = []
end end


def self.is_a_klass_method?
true
end

def uses_while def uses_while
i = 1 i = 1
while i < 10 while i < 10
Expand Down
4 changes: 4 additions & 0 deletions sample/test/test_heckled.rb
Expand Up @@ -20,4 +20,8 @@ def test_uses_strings
def test_uses_infinite_loop def test_uses_infinite_loop
@heckled.uses_infinite_loop? @heckled.uses_infinite_loop?
end end

def test_is_a_klass_method
assert_equal true, Heckled.is_a_klass_method?
end
end end
4 changes: 4 additions & 0 deletions test/fixtures/heckled.rb
Expand Up @@ -95,6 +95,10 @@ def uses_ranges


def uses_nothing def uses_nothing
end end

def self.is_a_klass_method?
true
end


private private


Expand Down
34 changes: 34 additions & 0 deletions test/test_heckle.rb
Expand Up @@ -385,3 +385,37 @@ def test_flips_until_to_while
assert_equal expected, @heckler.current_tree assert_equal expected, @heckler.current_tree
end end
end end

class TestHeckleClassMethod < Test::Unit::TestCase
def setup
@heckler = TestHeckler.new("Heckled", "self.is_a_klass_method?")
end

def teardown
@heckler.reset
end

def test_default_structure
expected = [:defn, :"self.is_a_klass_method?",
[:scope,
[:block,
[:args],
[:true]]]]
assert_equal expected, @heckler.current_tree
end

def test_heckle_class_methods
expected = [:defn, :"self.is_a_klass_method?",
[:scope,
[:block,
[:args],
[:false]]]]
@heckler.process(@heckler.current_tree)
assert_equal expected, @heckler.current_tree
end
end





0 comments on commit 273e59b

Please sign in to comment.