Skip to main content

fallow_mutation/
operator.rs

1//! Mutation operators and their stable kebab-case names.
2
3use serde::Serialize;
4
5/// One mutation operator.
6///
7/// The default set ([`Operator::DEFAULT`]) comes from selective-mutation
8/// research: it keeps the operators that give most of the test-strength
9/// signal and drops redundant ones. The other operators of [`Operator::ALL`]
10/// are opt-in: they make more mutants, many of them equivalent or killed by
11/// any test, so they run only when a run names them.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
13#[serde(rename_all = "kebab-case")]
14pub enum Operator {
15    /// `+` and `-` swap, `*` and `/` swap, `%` becomes `*`. The compound
16    /// assignments `+=` and `-=`, `*=` and `/=`, `<<=` and `>>=`, `&=` and
17    /// `|=` swap, and `%=` becomes `*=`.
18    ArithmeticOperator,
19    /// The non-redundant relational subset (Kaminski), and the constant
20    /// result of the comparison.
21    RelationalOperator,
22    /// `&&` and `||` swap, `??` becomes `&&`. `&&=` and `||=` swap, `??=`
23    /// becomes `&&=`.
24    LogicalOperator,
25    /// A branch or loop test becomes `true` and `false`, and a boolean
26    /// operand of `&&` or `||` becomes the constant that does not decide
27    /// the whole expression.
28    NegateCondition,
29    /// `!x`, `-x` and `~x` become `x`. `-x` and `+x` swap.
30    UnaryOperator,
31    /// `++` and `--` swap.
32    UpdateOperator,
33    /// A call statement becomes a no-op.
34    RemovedCall,
35    /// `return expr` and the concise body of `x => expr` become `undefined`.
36    ReturnValue,
37    /// `true` and `false` swap.
38    BooleanLiteral,
39    /// A non-empty string or template becomes empty. An empty one becomes
40    /// `"fallow"` or `` `fallow` ``.
41    StringLiteral,
42    /// `a?.b` becomes `a.b`, `a?.()` becomes `a()`.
43    OptionalChaining,
44    /// One token of a regular expression literal changes: an anchor or a
45    /// quantifier is removed, a `{n,m}` range becomes `{n}`, a character
46    /// class is negated, or a class escape gets the opposite case.
47    RegexLiteral,
48    /// One call link of a fluent chain in a module-level `const`
49    /// initializer is removed: `r.m(args)` becomes `r`, when `r` is a call.
50    RemovedChainLink,
51    /// Opt-in. The statements of a non-empty block or function body are
52    /// removed.
53    BlockStatement,
54    /// Opt-in. `[x]` becomes `[]`, and `[]` becomes `["fallow"]`.
55    ArrayLiteral,
56    /// Opt-in. A non-empty object literal becomes `{}`.
57    ObjectLiteral,
58    /// Opt-in. A string, array or date method call becomes its pair
59    /// (`startsWith` and `endsWith`), or the call is removed (`s.trim()`
60    /// becomes `s`).
61    MethodExpression,
62    /// Opt-in. The statements of a `case` or `default` clause are removed,
63    /// so the clause falls through.
64    SwitchCase,
65    /// Opt-in. A call in an expression position becomes `void 0`.
66    CallExpression,
67    /// Opt-in. A `throw new X(...)` statement is removed.
68    ThrowStatement,
69    /// Opt-in. The `regex-literal` and `regex-assertion` changes, applied
70    /// to the string pattern of `new RegExp("...")` or `RegExp("...")`.
71    RegexConstructor,
72    /// Opt-in. A lookaround of a regular expression literal is negated
73    /// (`(?=` and `(?!` swap, `(?<=` and `(?<!` swap), or `\b` and `\B`
74    /// swap.
75    RegexAssertion,
76}
77
78impl Operator {
79    /// The operators of a run that names no operator.
80    pub const DEFAULT: [Self; 13] = [
81        Self::ArithmeticOperator,
82        Self::RelationalOperator,
83        Self::LogicalOperator,
84        Self::NegateCondition,
85        Self::UnaryOperator,
86        Self::UpdateOperator,
87        Self::RemovedCall,
88        Self::ReturnValue,
89        Self::BooleanLiteral,
90        Self::StringLiteral,
91        Self::OptionalChaining,
92        Self::RegexLiteral,
93        Self::RemovedChainLink,
94    ];
95
96    /// The operators that run only when a run names them.
97    pub const OPT_IN: [Self; 9] = [
98        Self::BlockStatement,
99        Self::ArrayLiteral,
100        Self::ObjectLiteral,
101        Self::MethodExpression,
102        Self::SwitchCase,
103        Self::CallExpression,
104        Self::ThrowStatement,
105        Self::RegexConstructor,
106        Self::RegexAssertion,
107    ];
108
109    /// Every operator, in a stable order: the default set, then the opt-in
110    /// set.
111    pub const ALL: [Self; 22] = [
112        Self::ArithmeticOperator,
113        Self::RelationalOperator,
114        Self::LogicalOperator,
115        Self::NegateCondition,
116        Self::UnaryOperator,
117        Self::UpdateOperator,
118        Self::RemovedCall,
119        Self::ReturnValue,
120        Self::BooleanLiteral,
121        Self::StringLiteral,
122        Self::OptionalChaining,
123        Self::RegexLiteral,
124        Self::RemovedChainLink,
125        Self::BlockStatement,
126        Self::ArrayLiteral,
127        Self::ObjectLiteral,
128        Self::MethodExpression,
129        Self::SwitchCase,
130        Self::CallExpression,
131        Self::ThrowStatement,
132        Self::RegexConstructor,
133        Self::RegexAssertion,
134    ];
135
136    /// True for an operator of [`Operator::DEFAULT`].
137    pub fn is_default(self) -> bool {
138        Self::DEFAULT.contains(&self)
139    }
140
141    /// The stable kebab-case name of the operator.
142    pub const fn as_str(self) -> &'static str {
143        match self {
144            Self::ArithmeticOperator => "arithmetic-operator",
145            Self::RelationalOperator => "relational-operator",
146            Self::LogicalOperator => "logical-operator",
147            Self::NegateCondition => "negate-condition",
148            Self::UnaryOperator => "unary-operator",
149            Self::UpdateOperator => "update-operator",
150            Self::RemovedCall => "removed-call",
151            Self::ReturnValue => "return-value",
152            Self::BooleanLiteral => "boolean-literal",
153            Self::StringLiteral => "string-literal",
154            Self::OptionalChaining => "optional-chaining",
155            Self::RegexLiteral => "regex-literal",
156            Self::RemovedChainLink => "removed-chain-link",
157            Self::BlockStatement => "block-statement",
158            Self::ArrayLiteral => "array-literal",
159            Self::ObjectLiteral => "object-literal",
160            Self::MethodExpression => "method-expression",
161            Self::SwitchCase => "switch-case",
162            Self::CallExpression => "call-expression",
163            Self::ThrowStatement => "throw-statement",
164            Self::RegexConstructor => "regex-constructor",
165            Self::RegexAssertion => "regex-assertion",
166        }
167    }
168
169    /// Parses a kebab-case operator name.
170    pub fn from_name(name: &str) -> Option<Self> {
171        Self::ALL
172            .into_iter()
173            .find(|operator| operator.as_str() == name)
174    }
175}
176
177impl std::fmt::Display for Operator {
178    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179        f.write_str(self.as_str())
180    }
181}