Add capture group -> non capture group mutation

- Mutates regexps like (foo) to (?:foo).
This commit is contained in:
Daniel Gollahon 2016-10-09 18:45:42 -07:00
parent a45313d2f2
commit 69bf42a51d
No known key found for this signature in database
GPG key ID: B561636FDB951129
6 changed files with 47 additions and 1 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 16
total_score: 1306
total_score: 1300

View file

@ -92,6 +92,7 @@ require 'mutant/mutator/node'
require 'mutant/mutator/node/generic'
require 'mutant/mutator/node/regexp'
require 'mutant/mutator/node/regexp/alternation_meta'
require 'mutant/mutator/node/regexp/capture_group'
require 'mutant/mutator/node/regexp/character_type'
require 'mutant/mutator/node/regexp/end_of_line_anchor'
require 'mutant/mutator/node/regexp/end_of_string_or_before_end_of_line_anchor'

View file

@ -39,6 +39,7 @@ module Mutant
unsupported_regexp_nodes = AST::Types::REGEXP.to_a - %i[
regexp_alternation_meta
regexp_bol_anchor
regexp_capture_group
regexp_digit_type
regexp_eol_anchor
regexp_eos_ob_eol_anchor

View file

@ -0,0 +1,26 @@
module Mutant
class Mutator
class Node
module Regexp
# Mutator for regexp capture groups, such as `/(foo)/`
class CaptureGroup < Node
handle(:regexp_capture_group)
children :group
# Emit mutations
#
# Replace `(captured_group)` with `(?:non_captured_group)`
#
# @return [undefined]
def dispatch
return unless group
emit(s(:regexp_passive_group, group))
emit_group_mutations
end
end # EndOfLineAnchor
end # Regexp
end # Node
end # Mutator
end # Mutant

View file

@ -7,4 +7,5 @@ Mutant::Meta::Example.add :regexp_alternation_meta do
mutation '/\A(foo|bar)\z/'
mutation '/\A(foo|baz)\z/'
mutation '/\A(bar|baz)\z/'
mutation '/\A(?:foo|bar|baz)\z/'
end

View file

@ -0,0 +1,17 @@
Mutant::Meta::Example.add :regexp_capture_group do
source '/()/'
singleton_mutations
regexp_mutations
end
Mutant::Meta::Example.add :regexp_capture_group do
source '/(foo|bar)/'
singleton_mutations
regexp_mutations
mutation '/(?:foo|bar)/'
mutation '/(foo)/'
mutation '/(bar)/'
end