Skip to content

Commit

Permalink
Heckle :dasgn.
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/heckle/dev/": change = 3179]
  • Loading branch information
drbrain committed May 18, 2007
1 parent c3ee53e commit 4e182b2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 23 deletions.
3 changes: 2 additions & 1 deletion History.txt
Expand Up @@ -2,8 +2,9 @@

* N major enhancements:
* :call nodes are now heckled.
* :lasgn nodes are now heckled.
* :dasgn nodes are now heckled.
* :iasgn nodes are now heckled.
* :lasgn nodes are now heckled.
* N minor enhancements:
* Added focus mode to feel the Eye of Sauron.

Expand Down
47 changes: 25 additions & 22 deletions lib/heckle.rb
Expand Up @@ -25,6 +25,7 @@ class Heckle < SexpProcessor

MUTATABLE_NODES = [
:call,
:dasgn,
:false,
:if,
:lasgn,
Expand Down Expand Up @@ -260,6 +261,28 @@ def process_defn(exp)
reset_node_count
end

def mutate_asgn(node)
type = node.shift
var = node.shift
val = node.last

if val.first == :nil then
[type, var, [:lit, 42]]
else
[type, var, [:nil]]
end
end

def process_dasgn(exp)
mutate_node [:dasgn, exp.shift, process(exp.shift)]
end

##
# Replaces the value of the dasgn with nil if its some value, and 42 if its
# nil.

alias mutate_dasgn mutate_asgn

def process_iasgn(exp)
mutate_node [:iasgn, exp.shift, process(exp.shift)]
end
Expand All @@ -268,17 +291,7 @@ def process_iasgn(exp)
# Replaces the value of the iasgn with nil if its some value, and 42 if its
# nil.

def mutate_iasgn(node)
node.shift
var = node.shift
val = node.last

if val.first == :nil then
[:iasgn, var, [:lit, 42]]
else
[:iasgn, var, [:nil]]
end
end
alias mutate_iasgn mutate_asgn

def process_lasgn(exp)
mutate_node [:lasgn, exp.shift, process(exp.shift)]
Expand All @@ -288,17 +301,7 @@ def process_lasgn(exp)
# Replaces the value of the lasgn with nil if its some value, and 42 if its
# nil.

def mutate_lasgn(node)
node.shift
var = node.shift
val = node.last

if val.first == :nil then
[:lasgn, var, [:lit, 42]]
else
[:lasgn, var, [:nil]]
end
end
alias mutate_lasgn mutate_asgn

def process_lit(exp)
mutate_node [:lit, exp.shift]
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/heckled.rb
Expand Up @@ -9,6 +9,15 @@ def uses_call
some_func + some_other_func
end

def uses_dasgn
loop do |dvar|
loop do
dvar = 5
dvar = nil
end
end
end

def uses_iasgn
@ivar = 5
@ivar = nil
Expand Down
50 changes: 50 additions & 0 deletions test/test_heckle.rb
Expand Up @@ -103,6 +103,7 @@ def test_should_grab_mutatees_from_method
[:call, [:lvar, :i], :+, [:array, [:lit, 1]]],
[:call, [:str, "hi there"], :==,
[:array, [:str, "changeling"]]]],
:dasgn => [], # no dasgns here
:lasgn => [[:lasgn, :i, [:lit, 1]],
[:lasgn, :i, [:call, [:lvar, :i], :+, [:array, [:lit, 1]]]]],
:lit => [[:lit, 1], [:lit, 10], [:lit, 1]],
Expand Down Expand Up @@ -461,6 +462,55 @@ def test_heckle_class_methods
end
end

class TestHeckleDasgn < HeckleTestCase

def setup
@nodes = [:dasgn]
super
end

def test_dasgn_val
expected = [:defn, :uses_dasgn,
[:scope,
[:block,
[:args],
[:iter,
[:fcall, :loop],
[:dasgn_curr, :dvar],
[:iter,
[:fcall, :loop],
nil,
[:block,
[:dasgn, :dvar, [:nil]],
[:dasgn, :dvar, [:nil]]]]]]]]

@heckler.process(@heckler.current_tree)
assert_equal expected, @heckler.current_tree
end

def test_dasgn_nil
expected = [:defn, :uses_dasgn,
[:scope,
[:block,
[:args],
[:iter,
[:fcall, :loop],
[:dasgn_curr, :dvar],
[:iter,
[:fcall, :loop],
nil,
[:block,
[:dasgn, :dvar, [:lit, 5]],
[:dasgn, :dvar, [:lit, 42]]]]]]]]

@heckler.process(@heckler.current_tree)
@heckler.reset_tree
@heckler.process(@heckler.current_tree)
assert_equal expected, @heckler.current_tree
end

end

class TestHeckleIasgn < HeckleTestCase

def setup
Expand Down

0 comments on commit 4e182b2

Please sign in to comment.