Add character type mutations

Mutations:

  - `/\b/` -> `/\B/` (closes #577)
  - `/\B/` -> `/\b/` (closes #578)
  - `/\d/` -> `/\D/` (closes #579)
  - `/\D/` -> `/\d/` (closes #580)
  - `/\s/` -> `/\S/` (closes #583)
  - `/\S/` -> `/\s/` (closes #584)
  - `/\w/` -> `/\W/` (closes #585)
  - `/\W/` -> `/\w/` (closes #586)
This commit is contained in:
John Backus 2016-05-23 22:16:00 -07:00
parent 3e2d3c2ec0
commit 11a130ed43
No known key found for this signature in database
GPG key ID: 9A91898D0B0B2FBE
5 changed files with 57 additions and 1 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 16
total_score: 1290
total_score: 1317

View file

@ -91,6 +91,7 @@ require 'mutant/mutator/util/symbol'
require 'mutant/mutator/node'
require 'mutant/mutator/node/generic'
require 'mutant/mutator/node/regexp'
require 'mutant/mutator/node/regexp/character_type'
require 'mutant/mutator/node/literal'
require 'mutant/mutator/node/literal/boolean'
require 'mutant/mutator/node/literal/range'

View file

@ -39,6 +39,14 @@ module Mutant
unsupported_regexp_nodes = AST::Types::REGEXP.to_a - %i[
regexp_root_expression
regexp_bol_anchor
regexp_word_type
regexp_nonword_type
regexp_digit_type
regexp_nondigit_type
regexp_space_type
regexp_nonspace_type
regexp_word_boundary_anchor
regexp_nonword_boundary_anchor
]
# These nodes still need a dedicated mutator,

View file

@ -0,0 +1,28 @@
module Mutant
class Mutator
class Node
module Regexp
# Character type mutator
class CharacterType < Node
map = {
regexp_word_type: :regexp_nonword_type,
regexp_digit_type: :regexp_nondigit_type,
regexp_space_type: :regexp_nonspace_type,
regexp_word_boundary_anchor: :regexp_nonword_boundary_anchor
}
MAP = IceNine.deep_freeze(map.merge(map.invert))
handle(*MAP.keys)
# Mutate to invert character type
#
# @return [undefined]
def dispatch
emit(s(MAP.fetch(node.type)))
end
end # CharacterType
end # Regexp
end # Node
end # Mutator
end # Mutant

View file

@ -0,0 +1,19 @@
mutations = {
[:regexp_word_type, '/\w/'] => [:regexp_nonword_type, '/\W/'],
[:regexp_digit_type, '/\d/'] => [:regexp_nondigit_type, '/\D/'],
[:regexp_space_type, '/\s/'] => [:regexp_nonspace_type, '/\S/'],
[:regexp_word_boundary_anchor, '/\b/'] => [:regexp_nonword_boundary_anchor, '/\B/']
}
mutations = mutations.merge(mutations.invert)
mutations.each do |(source_type, source_mutation), (_, regexp_mutation)|
Mutant::Meta::Example.add source_type do
source(source_mutation)
singleton_mutations
regexp_mutations
mutation(regexp_mutation)
end
end