Socket
Socket
Sign inDemoInstall

weapon-regex

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weapon-regex

weapon-regex


Version published
Weekly downloads
80K
decreased by-0.72%
Maintainers
1
Weekly downloads
 
Created
Source

Weapon regeX

Mutation testing badge Build Status GitHub Pages

Weapon regeX Logo

With Weapon regeX you can mutate regular expressions which can be used in mutation testing. The generated regular expressions cover edge cases and typos. Weapon regeX is available for both Javascript and Scala. The Javascript version of the library is generated from Scala using ScalaJS.

The current supported versions for Scala are: 2.12.12 and 2.13.3.

Getting started

Scala

Add Weapon regeX to your build.sbt.

"io.stryker-mutator" %% "weapon-regex" % "0.1.2"

Mutate!

import weaponregex.WeaponRegeX
import scala.util.{Success, Failure}

WeaponRegeX.mutate("^abc(d+|[xyz])$") match {
  case Success(mutants) => mutants map (_.pattern) map println
  case Failure(e)       => print(e.getMessage)
}

Javascript

Install Weapon regeX with npm.

npm install weapon-regex

Mutate!

const wrx = require('weapon-regex');

let mutants = wrx.mutate("^abc(d+|[xyz])$");

mutants.forEach(mutant => {
  console.log(mutant.pattern);
});

Try it!

API

Scala

The mutate function has the following signature:

//import scala.util.{Try, Success, Failure}

def mutate(
      pattern: String,
      mutators: Seq[TokenMutator] = BuiltinMutators.all,
      mutationLevels: Seq[Int] = null
  ): Try[Seq[Mutant]]

With the mutators argument you can give a select list of mutators that should be used in the mutation process. If omitted, all builtin mutators will be used. This list will be filtered depending on the mutationLevels argument.

A list of mutationLevels can also be passed to the function. The mutators will be filtered based on the levels in the list. If omitted, no filtering takes place.

This function will return a Success of of a sequence of Mutant if can be parsed, a Failure otherwise.

Javascript

The mutate function can be called with an options object to control which mutators should be used in the mutation process:

const wrx = require('weapon-regex');

let mutants = wrx.mutate("^abc(d+|[xyz])$",{
  mutators: Array.from(wrx.mutators.values()),
  mutationLevels: [1, 2, 3]
});

Both options can be omitted, and have the same functionality as the options described in the Scala API section. You can get a map of mutators from the mutators attribute of the library. It is a map from string (mutator name) to a mutator object.

This function will return a JavaScript Array of Mutant if can be parsed, or throw an exception otherwise.

Supported mutators

All the supported mutators and at which mutation level they appear are shown in the table below.

Name123
BOLRemoval
EOLRemoval
BOL2BOI
EOL2EOI
CharClassNegation
CharClassChildRemoval
CharClassAnyChar
CharClassRangeModification
PredefCharClassNegation
PredefCharClassNullification
PredefCharClassAnyChar
QuantifierRemoval
QuantifierNChange
QuantifierNOrMoreModification
QuantifierNOrMoreChange
QuantifierNMModification
QuantifierShortModification
QuantifierShortChange
QuantifierReluctantAddition
GroupToNCGroup

Boundary Mutators

BOLRemoval

It removes the beginning of line character "^"

OriginalMutated
^abcabc

Back to table 🔝

EOLRemoval

It removes the end of line character "$"

OriginalMutated
abc$abc

Back to table 🔝

BOL2BOI

It changes the beginning of line character "^" to a beginning of input character "\A"

OriginalMutated
^abc\Aabc

Back to table 🔝

EOL2EOI

It changes the end of line character "^" to a end of input character "\z"

OriginalMutated
abc$abc\z

Back to table 🔝

Character class mutators

CharClassNegation

It flips the sign of a character class.

OriginalMutated
[abc][^abc]
[^abc][abc]

Back to table 🔝

CharClassChildRemoval

It removes a child of a character class.

OriginalMutated
[abc][bc]
[abc][ac]
[abc][ab]

Back to table 🔝

CharClassAnyChar

It changes a character class to a character class which matches any character.

OriginalMutated
[abc][\w\W]

Back to table 🔝

CharClassRangeModification

It changes the high and low of a range by one in both directions if possible.

OriginalMutated
[b-y][a-y]
[b-y][c-y]
[b-y][b-z]
[b-y][b-x]

Back to table 🔝

Predefined character class mutators

PredefCharClassNegation

It flips the sign of a predefined character class. All the predefined character classes are shown in the table below.

OriginalMutated
\d\D
\D\d
\s\S
\S\s
\w\W
\W\w

Back to table 🔝

PredefCharClassNullification

It removes the backslash from a predefined character class such as "\w".

OriginalMutated
\dd
\DD
\ss
\SS
\ww
\WW

Back to table 🔝

PredefCharClassAnyChar

It changes a predefined character class to a character class containing the predefined one and its negation.

OriginalMutated
\d[\d\D]
\D[\D\d]
\s[\s\S]
\S[\S\s]
\w[\w\W]
\W[\W\w]

Back to table 🔝

Quantifier mutators

QuantifierRemoval

It removes a quantifier. This is done for all possible quantifiers, even ranges, and the reluctant and possessive variants.

OriginalMutated
abc?abc
abc*abc
abc+abc
abc{1,3}abc
abc??abc
abc*?abc
abc+?abc
abc{1,3}?abc
abc?+abc
abc*+abc
abc++abc
abc{1,3}+abc

Back to table 🔝

QuantifierNChange

It changes the fixed amount quantifier to a couple range variants.

OriginalMutated
abc{9}abc{0,9}
abc{9}abc{9,}

Back to table 🔝

QuantifierNOrMoreModification

It changes the n to infinity range quantifier to a couple variants where the low of the range is incremented an decremented by one.

OriginalMutated
abc{9,}abc{8,}
abc{9,}abc{10,}

Back to table 🔝

QuantifierNOrMoreChange

It turns an N or more range quantifier into a fixed number quantifier.

OriginalMutated
abc{9,}abc{9}

Back to table 🔝

QuantifierNMModification

It alters the N to M range quantifier by decrementing or incrementing the high and low of the range by one.

OriginalMutated
abc{3,9}abc{2,9}
abc{3,9}abc{4,9}
abc{3,9}abc{3,8}
abc{3,9}abc{3,10}

Back to table 🔝

QuantifierShortModification

It treats the shorthand quantifiers (?, *, +) as their corresponding range quantifier variant ({0,1}, {0,}, {1,}), and applies the same mutations as mentioned in the mutators above.

OriginalMutated
abc?abc{1,1}
abc?abc{0,0}
abc?abc{0,2}
abc*abc{1,}
abc+abc{0,}
abc+abc{2,}

Back to table 🔝

QuantifierShortChange

It changes the shorthand quantifiers * and + to their fixed range quantifier variant.

OriginalMutated
abc*abc{0}
abc+abc{1}

Back to table 🔝

QuantifierReluctantAddition

It changes greedy quantifiers to reluctant quantifiers.

OriginalMutated
abc?abc??
abc*abc*?
abc+abc+?
abc{9}abc{9}?
abc{9,}abc{9,}?
abc{9,13}abc{9,13}?

Back to table 🔝

Group mutators

GroupToNCGroup

It changes a normal group to a non-capturing group.

OriginalMutated
(abc)(?:abc)

Back to table 🔝

FAQs

Package last updated on 18 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc