/
verification.rb
106 lines (94 loc) · 2.8 KB
/
verification.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true
module Mutant
module Meta
class Example
# Example verification
class Verification
include Adamantium::Flat, Concord.new(:example, :mutations)
# Test if mutation was verified successfully
#
# @return [Boolean]
def success?
[missing, unexpected, no_diffs, invalid_syntax].all?(&:empty?)
end
memoize :success?
# Error report
#
# @return [String]
def error_report
fail 'no error report on successful validation' if success?
YAML.dump(
'file' => example.file,
'original_ast' => example.node.inspect,
'original_source' => example.source,
'missing' => format_mutations(missing),
'unexpected' => format_mutations(unexpected),
'invalid_syntax' => format_mutations(invalid_syntax),
'no_diff' => no_diff_report
)
end
memoize :error_report
private
# Unexpected mutations
#
# @return [Array<Mutation>]
def unexpected
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/SuppressedException
end
end
memoize :invalid_syntax
# Mutations with no diff to original
#
# @return [Enumerable<Mutation>]
def no_diffs
mutations.select { |mutation| mutation.source.eql?(example.source) }
end
memoize :no_diffs
# Mutation report
#
# @param [Array<Mutation>] mutations
#
# @return [Array<Hash>]
def format_mutations(mutations)
mutations.map do |mutation|
{
'node' => mutation.node.inspect,
'source' => mutation.source
}
end
end
# No diff mutation report
#
# @return [Array, nil]
def no_diff_report
no_diffs.map do |mutation|
{
'node' => mutation.node.inspect,
'source' => mutation.source
}
end
end
end # Verification
end # Example
end # Meta
end # Mutant