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 /// Opt-in. `a ?? b` becomes `a`: the fallback of a nullish coalescing
77 /// expression is removed.
78 NullishFallback,
79}
80
81impl Operator {
82 /// The operators of a run that names no operator.
83 pub const DEFAULT: [Self; 13] = [
84 Self::ArithmeticOperator,
85 Self::RelationalOperator,
86 Self::LogicalOperator,
87 Self::NegateCondition,
88 Self::UnaryOperator,
89 Self::UpdateOperator,
90 Self::RemovedCall,
91 Self::ReturnValue,
92 Self::BooleanLiteral,
93 Self::StringLiteral,
94 Self::OptionalChaining,
95 Self::RegexLiteral,
96 Self::RemovedChainLink,
97 ];
98
99 /// The operators that run only when a run names them.
100 pub const OPT_IN: [Self; 10] = [
101 Self::BlockStatement,
102 Self::ArrayLiteral,
103 Self::ObjectLiteral,
104 Self::MethodExpression,
105 Self::SwitchCase,
106 Self::CallExpression,
107 Self::ThrowStatement,
108 Self::RegexConstructor,
109 Self::RegexAssertion,
110 Self::NullishFallback,
111 ];
112
113 /// Every operator, in a stable order: the default set, then the opt-in
114 /// set.
115 pub const ALL: [Self; 23] = [
116 Self::ArithmeticOperator,
117 Self::RelationalOperator,
118 Self::LogicalOperator,
119 Self::NegateCondition,
120 Self::UnaryOperator,
121 Self::UpdateOperator,
122 Self::RemovedCall,
123 Self::ReturnValue,
124 Self::BooleanLiteral,
125 Self::StringLiteral,
126 Self::OptionalChaining,
127 Self::RegexLiteral,
128 Self::RemovedChainLink,
129 Self::BlockStatement,
130 Self::ArrayLiteral,
131 Self::ObjectLiteral,
132 Self::MethodExpression,
133 Self::SwitchCase,
134 Self::CallExpression,
135 Self::ThrowStatement,
136 Self::RegexConstructor,
137 Self::RegexAssertion,
138 Self::NullishFallback,
139 ];
140
141 /// True for an operator of [`Operator::DEFAULT`].
142 pub fn is_default(self) -> bool {
143 Self::DEFAULT.contains(&self)
144 }
145
146 /// The stable kebab-case name of the operator.
147 pub const fn as_str(self) -> &'static str {
148 match self {
149 Self::ArithmeticOperator => "arithmetic-operator",
150 Self::RelationalOperator => "relational-operator",
151 Self::LogicalOperator => "logical-operator",
152 Self::NegateCondition => "negate-condition",
153 Self::UnaryOperator => "unary-operator",
154 Self::UpdateOperator => "update-operator",
155 Self::RemovedCall => "removed-call",
156 Self::ReturnValue => "return-value",
157 Self::BooleanLiteral => "boolean-literal",
158 Self::StringLiteral => "string-literal",
159 Self::OptionalChaining => "optional-chaining",
160 Self::RegexLiteral => "regex-literal",
161 Self::RemovedChainLink => "removed-chain-link",
162 Self::BlockStatement => "block-statement",
163 Self::ArrayLiteral => "array-literal",
164 Self::ObjectLiteral => "object-literal",
165 Self::MethodExpression => "method-expression",
166 Self::SwitchCase => "switch-case",
167 Self::CallExpression => "call-expression",
168 Self::ThrowStatement => "throw-statement",
169 Self::RegexConstructor => "regex-constructor",
170 Self::RegexAssertion => "regex-assertion",
171 Self::NullishFallback => "nullish-fallback",
172 }
173 }
174
175 /// Parses a kebab-case operator name.
176 pub fn from_name(name: &str) -> Option<Self> {
177 Self::ALL
178 .into_iter()
179 .find(|operator| operator.as_str() == name)
180 }
181}
182
183impl std::fmt::Display for Operator {
184 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185 f.write_str(self.as_str())
186 }
187}