Add example verificatin to guard against emitting invalid AST

This commit is contained in:
Markus Schirp 2018-12-01 22:44:39 +00:00
parent 9219e3b018
commit 0a7c7ec2ba
2 changed files with 45 additions and 1 deletions

View file

@ -11,7 +11,7 @@ module Mutant
# #
# @return [Boolean] # @return [Boolean]
def success? def success?
[missing, unexpected, no_diffs].all?(&:empty?) [missing, unexpected, no_diffs, invalid_syntax].all?(&:empty?)
end end
# Error report # Error report
@ -26,6 +26,7 @@ module Mutant
'original_source' => example.source, 'original_source' => example.source,
'missing' => format_mutations(missing), 'missing' => format_mutations(missing),
'unexpected' => format_mutations(unexpected), 'unexpected' => format_mutations(unexpected),
'invalid_syntax' => format_mutations(invalid_syntax),
'no_diff' => no_diff_report 'no_diff' => no_diff_report
) )
end end
@ -52,6 +53,17 @@ module Mutant
end end
memoize :missing memoize :missing
# Mutations that generated invalid syntax
#
# @return [Enumerable<Mutation>]
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 # Mutations with no diff to original
# #
# @return [Enumerable<Mutation>] # @return [Enumerable<Mutation>]

View file

@ -75,6 +75,7 @@ RSpec.describe Mutant::Meta::Example::Verification do
- node: s(:nil) - node: s(:nil)
source: nil source: nil
unexpected: [] unexpected: []
invalid_syntax: []
no_diff: [] no_diff: []
REPORT REPORT
end end
@ -95,6 +96,7 @@ RSpec.describe Mutant::Meta::Example::Verification do
source: 'false' source: 'false'
- node: s(:nil) - node: s(:nil)
source: nil source: nil
invalid_syntax: []
no_diff: [] no_diff: []
REPORT REPORT
end end
@ -112,11 +114,41 @@ RSpec.describe Mutant::Meta::Example::Verification do
original_source: 'true' original_source: 'true'
missing: [] missing: []
unexpected: [] unexpected: []
invalid_syntax: []
no_diff: no_diff:
- node: s(:true) - node: s(:true)
source: 'true' source: 'true'
REPORT REPORT
end end
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
end end