From bfd41e6948ad50a540b94f7d97646221be618f8c Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 1 Dec 2018 22:44:37 +0000 Subject: [PATCH 1/3] Refactor example verification duplication --- lib/mutant/meta/example/verification.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mutant/meta/example/verification.rb b/lib/mutant/meta/example/verification.rb index c3bf4637..e59c6b5e 100644 --- a/lib/mutant/meta/example/verification.rb +++ b/lib/mutant/meta/example/verification.rb @@ -11,7 +11,7 @@ module Mutant # # @return [Boolean] def success? - missing.empty? && unexpected.empty? && no_diffs.empty? + [missing, unexpected, no_diffs].all?(&:empty?) end # Error report From 9219e3b018dd81a06d5b687df9818fee1fb5d76e Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 1 Dec 2018 22:44:38 +0000 Subject: [PATCH 2/3] Refactor example verification to reduce redundant unparsing --- lib/mutant/meta/example/verification.rb | 34 ++++++++++++++----------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/mutant/meta/example/verification.rb b/lib/mutant/meta/example/verification.rb index e59c6b5e..fd19723a 100644 --- a/lib/mutant/meta/example/verification.rb +++ b/lib/mutant/meta/example/verification.rb @@ -34,12 +34,24 @@ module Mutant # Unexpected mutations # - # @return [Array] + # @return [Array] def unexpected - mutations.map(&:node) - example.expected + mutations.reject do |mutation| + example.expected.include?(mutation.node) + end end memoize :unexpected + # Missing mutations + # + # @return [Array] + def missing + (example.expected - mutations.map(&:node)).map do |node| + Mutation::Evil.new(self, node) + end + end + memoize :missing + # Mutations with no diff to original # # @return [Enumerable] @@ -50,14 +62,14 @@ module Mutant # Mutation report # - # @param [Array] nodes + # @param [Array] mutations # # @return [Array] - def format_mutations(nodes) - nodes.map do |node| + def format_mutations(mutations) + mutations.map do |mutation| { - 'node' => node.inspect, - 'source' => Unparser.unparse(node) + 'node' => mutation.node.inspect, + 'source' => mutation.source } end end @@ -74,14 +86,6 @@ module Mutant end end - # Missing mutations - # - # @return [Array] - def missing - example.expected - mutations.map(&:node) - end - memoize :missing - end # Verification end # Example end # Meta From 0a7c7ec2ba2b237c608a3369a6132468aae5db24 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 1 Dec 2018 22:44:39 +0000 Subject: [PATCH 3/3] Add example verificatin to guard against emitting invalid AST --- lib/mutant/meta/example/verification.rb | 14 +++++++- .../mutant/meta/example/verification_spec.rb | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/mutant/meta/example/verification.rb b/lib/mutant/meta/example/verification.rb index fd19723a..5b8b3773 100644 --- a/lib/mutant/meta/example/verification.rb +++ b/lib/mutant/meta/example/verification.rb @@ -11,7 +11,7 @@ module Mutant # # @return [Boolean] def success? - [missing, unexpected, no_diffs].all?(&:empty?) + [missing, unexpected, no_diffs, invalid_syntax].all?(&:empty?) end # Error report @@ -26,6 +26,7 @@ module Mutant 'original_source' => example.source, 'missing' => format_mutations(missing), 'unexpected' => format_mutations(unexpected), + 'invalid_syntax' => format_mutations(invalid_syntax), 'no_diff' => no_diff_report ) end @@ -52,6 +53,17 @@ module Mutant end memoize :missing + # Mutations that generated invalid syntax + # + # @return [Enumerable] + def invalid_syntax + mutations.reject do |mutation| + ::Parser::CurrentRuby.parse(mutation.source) + rescue ::Parser::SyntaxError # rubocop:disable Lint/HandleExceptions + end + end + memoize :invalid_syntax + # Mutations with no diff to original # # @return [Enumerable] diff --git a/spec/unit/mutant/meta/example/verification_spec.rb b/spec/unit/mutant/meta/example/verification_spec.rb index ea3e9410..d38809f7 100644 --- a/spec/unit/mutant/meta/example/verification_spec.rb +++ b/spec/unit/mutant/meta/example/verification_spec.rb @@ -75,6 +75,7 @@ RSpec.describe Mutant::Meta::Example::Verification do - node: s(:nil) source: nil unexpected: [] + invalid_syntax: [] no_diff: [] REPORT end @@ -95,6 +96,7 @@ RSpec.describe Mutant::Meta::Example::Verification do source: 'false' - node: s(:nil) source: nil + invalid_syntax: [] no_diff: [] REPORT end @@ -112,11 +114,41 @@ RSpec.describe Mutant::Meta::Example::Verification do original_source: 'true' missing: [] unexpected: [] + invalid_syntax: [] no_diff: - node: s(:true) source: 'true' REPORT end end + + context 'when the generated node is invalid syntax after unparsed' do + let(:invalid_node) do + s(:op_asgn, s(:send, s(:self), :at, s(:int, 1)), :+, s(:int, 1)) + end + + let(:expected_nodes) { [invalid_node] } + let(:generated_nodes) { [invalid_node] } + + specify do + should eql(<<~'REPORT') + --- + file: foo.rb + original_ast: s(:true) + original_source: 'true' + missing: [] + unexpected: [] + invalid_syntax: + - node: |- + s(:op_asgn, + s(:send, + s(:self), :at, + s(:int, 1)), :+, + s(:int, 1)) + source: self.at(1) += 1 + no_diff: [] + REPORT + end + end end end