diff --git a/Changelog.md b/Changelog.md index f5e8db94..c1df6b4a 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ # v0.6.1 2014-08-16 -* Add rescue else body promotion/concatenation mutation +* Add rescue resbody body concat-promotion mutation +* Add rescue else body concat-promotion mutation #245 # v0.6.0 2014-08-11 diff --git a/config/flay.yml b/config/flay.yml index e520e95a..faf9491b 100644 --- a/config/flay.yml +++ b/config/flay.yml @@ -1,3 +1,3 @@ --- threshold: 18 -total_score: 1076 +total_score: 1085 diff --git a/lib/mutant/ast/meta.rb b/lib/mutant/ast/meta.rb index 62845988..423e49fa 100644 --- a/lib/mutant/ast/meta.rb +++ b/lib/mutant/ast/meta.rb @@ -17,14 +17,42 @@ module Mutant REGISTRY.fetch(node.type, Generic).new(node) end - # Generic metadata for send nodes + # Mixin to define meta nodes + class Node < Module + include Concord.new(:type) + + CONCORD = Concord.new(:node) + + # Hook called when module gets included + # + # @param [Class, Module] host + # + # @return [undefined] + # + # @api private + # + def included(host) + REGISTRY[type] = host + host.class_eval do + include CONCORD, NamedChildren + end + end + + end # Node + + # Metadata for resbody nods + class Resbody + include Node.new(:resbody) + + children :captures, :assignment, :body + end # Resbody + + # Metadata for send nodes class Send - include Concord.new(:node), NamedChildren + include Node.new(:send) children :receiver, :selector - REGISTRY[:send] = self - INDEX_ASSIGNMENT_SELECTOR = :[]= ATTRIBUTE_ASSIGNMENT_SELECTOR_SUFFIX = '='.freeze diff --git a/lib/mutant/meta.rb b/lib/mutant/meta.rb index b2f709d4..dd882487 100644 --- a/lib/mutant/meta.rb +++ b/lib/mutant/meta.rb @@ -16,7 +16,8 @@ module Mutant # @api private # def self.add(&block) - ALL << DSL.run(block) + file = caller.first.split(':in', 2).first + ALL << DSL.run(file, block) end Pathname.glob(Pathname.new(__FILE__).parent.parent.parent.join('meta', '**/*.rb')) diff --git a/lib/mutant/meta/example.rb b/lib/mutant/meta/example.rb index 84dd1f44..d1737b8a 100644 --- a/lib/mutant/meta/example.rb +++ b/lib/mutant/meta/example.rb @@ -1,7 +1,7 @@ module Mutant module Meta class Example - include Adamantium, Concord::Public.new(:node, :mutations) + include Adamantium, Concord::Public.new(:file, :node, :mutations) # Return a verification instance # @@ -97,7 +97,7 @@ module Mutant def mutation_report original_node = example.node [ - 'Original-AST:', + "#{example.file}, Original-AST:", original_node.inspect, 'Original-Source:', example.source, diff --git a/lib/mutant/meta/example/dsl.rb b/lib/mutant/meta/example/dsl.rb index fef11f31..63935e17 100644 --- a/lib/mutant/meta/example/dsl.rb +++ b/lib/mutant/meta/example/dsl.rb @@ -12,8 +12,8 @@ module Mutant # # @api private # - def self.run(block) - instance = new + def self.run(file, block) + instance = new(file) instance.instance_eval(&block) instance.example end @@ -24,7 +24,8 @@ module Mutant # # @api private # - def initialize + def initialize(file) + @file = file @source = nil @expected = [] end @@ -40,7 +41,7 @@ module Mutant # def example raise 'source not defined' unless @source - Example.new(@source, @expected) + Example.new(@file, @source, @expected) end private diff --git a/lib/mutant/mutator/node/rescue.rb b/lib/mutant/mutator/node/rescue.rb index 1fd93ef3..812e1a56 100644 --- a/lib/mutant/mutator/node/rescue.rb +++ b/lib/mutant/mutator/node/rescue.rb @@ -34,8 +34,11 @@ module Mutant # def mutate_rescue_bodies children_indices(RESCUE_INDICES).each do |index| - next unless children.at(index) + rescue_body = children.at(index) + next unless rescue_body mutate_child(index) + resbody_body = AST::Meta.for(rescue_body).body + emit_concat(resbody_body) if resbody_body end end @@ -48,8 +51,11 @@ module Mutant # @api private # def emit_concat(child) - raise unless body - emit(s(:begin, body, child)) + if body + emit(s(:begin, body, child)) + else + emit(child) + end end # Emit body mutations diff --git a/meta/def.rb b/meta/def.rb index 080718d9..5ad550b4 100644 --- a/meta/def.rb +++ b/meta/def.rb @@ -14,7 +14,7 @@ Mutant::Meta::Example.add do mutation 'def foo; self; rescue; end' mutation 'def foo; end' - # Promote rescue body + # Promote rescue resbody bodies mutation 'def foo; foo; end' end @@ -29,10 +29,13 @@ Mutant::Meta::Example.add do mutation 'def a; foo; rescue; bar; else; nil; end' mutation 'def a; foo; rescue; bar; else; self; end' + # Promote and concat rescue resbody bodies + mutation 'def a; foo; bar; end' + # Promote and concat else body mutation 'def a; foo; baz; end' - # Promote body + # Promote rescue body mutation 'def a; foo; end' # Empty body diff --git a/meta/rescue.rb b/meta/rescue.rb index 3bc79204..5514b2f6 100644 --- a/meta/rescue.rb +++ b/meta/rescue.rb @@ -9,6 +9,8 @@ Mutant::Meta::Example.add do mutation 'begin; rescue ExceptionA, self => error; true; end' mutation 'begin; rescue ExceptionA, ExceptionB => error; false; end' mutation 'begin; rescue ExceptionA, ExceptionB => error; nil; end' + mutation 'begin; true; end' + end Mutant::Meta::Example.add do @@ -19,6 +21,7 @@ Mutant::Meta::Example.add do mutation 'begin; rescue SomeException => error; false; end' mutation 'begin; rescue SomeException => error; nil; end' mutation 'begin; rescue self => error; true; end' + mutation 'begin; true; end' end Mutant::Meta::Example.add do @@ -28,6 +31,7 @@ Mutant::Meta::Example.add do mutation 'begin; rescue => error; false; end' mutation 'begin; rescue => error; nil; end' mutation 'begin; rescue; true; end' + mutation 'begin; true; end' end Mutant::Meta::Example.add do @@ -36,4 +40,5 @@ Mutant::Meta::Example.add do singleton_mutations mutation 'begin; rescue; false; end' mutation 'begin; rescue; nil; end' + mutation 'begin; true end' end