Merge pull request #791 from mbj/improve/example-verification
Improve example verification
This commit is contained in:
commit
148d632c20
2 changed files with 64 additions and 16 deletions
|
@ -11,7 +11,7 @@ module Mutant
|
|||
#
|
||||
# @return [Boolean]
|
||||
def success?
|
||||
missing.empty? && unexpected.empty? && no_diffs.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
|
||||
|
@ -34,12 +35,35 @@ module Mutant
|
|||
|
||||
# Unexpected mutations
|
||||
#
|
||||
# @return [Array<Parser::AST::Node>]
|
||||
# @return [Array<Mutation>]
|
||||
def unexpected
|
||||
mutations.map(&:node) - example.expected
|
||||
mutations.reject do |mutation|
|
||||
example.expected.include?(mutation.node)
|
||||
end
|
||||
end
|
||||
memoize :unexpected
|
||||
|
||||
# Missing mutations
|
||||
#
|
||||
# @return [Array<Mutation>]
|
||||
def missing
|
||||
(example.expected - mutations.map(&:node)).map do |node|
|
||||
Mutation::Evil.new(self, node)
|
||||
end
|
||||
end
|
||||
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
|
||||
#
|
||||
# @return [Enumerable<Mutation>]
|
||||
|
@ -50,14 +74,14 @@ module Mutant
|
|||
|
||||
# Mutation report
|
||||
#
|
||||
# @param [Array<Parser::AST::Node>] nodes
|
||||
# @param [Array<Mutation>] mutations
|
||||
#
|
||||
# @return [Array<Hash>]
|
||||
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 +98,6 @@ module Mutant
|
|||
end
|
||||
end
|
||||
|
||||
# Missing mutations
|
||||
#
|
||||
# @return [Array<Parser::AST::Node>]
|
||||
def missing
|
||||
example.expected - mutations.map(&:node)
|
||||
end
|
||||
memoize :missing
|
||||
|
||||
end # Verification
|
||||
end # Example
|
||||
end # Meta
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue