Skip to content

Commit

Permalink
Add method body -> super mutation
Browse files Browse the repository at this point in the history
- Mutates method bodies to `super` to catch cases where a method has
  been needlessly re-implemented and the parent class provides the
  equivalent behavior.
- Closes mbj#154
- Closes mbj#673
  • Loading branch information
dgollahon committed Jun 27, 2018
1 parent 3eb5527 commit 325e6b6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/mutant/mutator/node/define.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def dispatch
emit_optarg_body_assignments
emit_restarg_body_mutation
emit_body(N_RAISE)
emit_body(N_ZSUPER)
emit_body(nil)
emit_body_mutations if body
end
Expand Down
17 changes: 16 additions & 1 deletion meta/def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
source 'def foo; end'

mutation 'def foo; raise; end'
mutation 'def foo; super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -14,6 +15,7 @@
mutation 'def foo(a); nil; end'
mutation 'def foo(*b); nil; end'
mutation 'def foo(a, *b); b = []; nil; end'
mutation 'def foo(a, *b); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -25,6 +27,7 @@
mutation 'def foo(a, *); raise; end'
mutation 'def foo(a); nil; end'
mutation 'def foo(*); nil; end'
mutation 'def foo(a, *); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -34,6 +37,7 @@
mutation 'def foo; nil; rescue; end'
mutation 'def foo; self; rescue; end'
mutation 'def foo; end'
mutation 'def foo; super; end'

# Promote rescue resbody bodies
mutation 'def foo; foo; end'
Expand Down Expand Up @@ -65,6 +69,8 @@
# Failing body
mutation 'def a; raise; end'

# Superclass implementation
mutation 'def a; super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -85,6 +91,7 @@

mutation 'def foo; raise; end'

mutation 'def foo; super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -103,7 +110,7 @@

# Mutation of body
mutation 'def foo(a, b); raise; end'

mutation 'def foo(a, b); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -120,13 +127,15 @@
mutation 'def foo(b = nil); true; end'
mutation 'def foo(a, _b = nil); true; end'
mutation 'def foo(a, b); true; end'
mutation 'def foo(a, b = nil); super; end'
end

Mutant::Meta::Example.add :def do
source 'def foo(_unused); end'

mutation 'def foo(_unused); raise; end'
mutation 'def foo; end'
mutation 'def foo(_unused); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -137,6 +146,7 @@
mutation 'def foo(_unused = true); raise; end'
mutation 'def foo(_unused); end'
mutation 'def foo; end'
mutation 'def foo(_unused = true); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -158,6 +168,7 @@
mutation 'def foo(a = 0, b = 0); a = 0; end'
mutation 'def foo(a = 0, b = 0); b = 0; end'
mutation 'def foo(a = 0, b = 0); raise; end'
mutation 'def foo(a = 0, b = 0); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -170,6 +181,7 @@
mutation 'def foo(_a = true); end'
mutation 'def foo(a = true); raise; end'
mutation 'def foo(a = true); a = true; end'
mutation 'def foo(a = true); super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -189,6 +201,8 @@
mutation 'def self.foo; end'

mutation 'def self.foo; raise; end'

mutation 'def self.foo; super; end'
end

Mutant::Meta::Example.add :def do
Expand All @@ -208,4 +222,5 @@

# Mutation of body
mutation 'def self.foo(a, b); raise; end'
mutation 'def self.foo(a, b); super; end'
end
1 change: 1 addition & 0 deletions meta/kwarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

mutation 'def foo; end'
mutation 'def foo(bar:); raise; end'
mutation 'def foo(bar:); super; end'
mutation 'def foo(_bar:); end'
end
1 change: 1 addition & 0 deletions meta/kwoptarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mutation 'def foo; end'
mutation 'def foo(bar: baz); raise; end'
mutation 'def foo(bar: baz); super; end'
mutation 'def foo(bar: nil); end'
mutation 'def foo(bar: self); end'
mutation 'def foo(bar:); end'
Expand Down
3 changes: 3 additions & 0 deletions meta/rescue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@

# Failing body
mutation 'def a; raise; end'

# Superclass implementation
mutation 'def a; super; end'
end

Mutant::Meta::Example.add :rescue do
Expand Down
1 change: 1 addition & 0 deletions meta/restarg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
mutation 'def foo; end'
mutation 'def foo(*bar); bar = []; end'
mutation 'def foo(*bar); raise; end'
mutation 'def foo(*bar); super; end'
end
5 changes: 5 additions & 0 deletions spec/unit/mutant/subject/method/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def foo; end
s(:begin,
s(:def, :foo, s(:args), s(:send, nil, :raise)), s(:send, nil, :memoize, s(:args, s(:sym, :foo))))
),
Mutant::Mutation::Evil.new(
object,
s(:begin,
s(:def, :foo, s(:args), s(:zsuper)), s(:send, nil, :memoize, s(:args, s(:sym, :foo))))
),
Mutant::Mutation::Evil.new(
object,
s(:begin,
Expand Down

0 comments on commit 325e6b6

Please sign in to comment.