comparison-operator
total >= 50 total > 50 This mutator moves the boundary of a comparison, as if the code were off by one:
total >= 50 becomes total > 50. Its other mutants swap the operator, make
the result true or false, or move a number by one.
Tests catch these mutants with values at the boundary and right next to it, such as totals of 49, 50 and 51.
Each comparison gets two or three mutants:
| Code | Mutants |
|---|---|
price |
price <= budget, price != budget, false |
price |
price < budget, price == budget, true |
price |
price >= budget, price != budget, false |
price |
price > budget, price == budget, true |
price |
price <= oldPrice, price >= oldPrice, false |
price |
price < oldPrice, price > oldPrice, true |
|
true, false |
|
true, false |
|
true, false |
Mootant swaps ===, !==, == and != only when one side is known to be a
number, such as a number parameter or a numeric literal, and the other side is
not known to be anything else. Otherwise they get true and false.
Mootant doesn’t make the other swaps, such as price > budget for
price < budget: for numbers other than NaN, the tests that catch these
mutants also catch them.
When only one side of <, <=, > or >= is a number written in the code,
such as 50 or -1, Mootant moves it by one in place of true or false:
| Code | Mutant |
|---|---|
price < |
price < 4 |
weight <= |
weight <= 21 |
quantity > |
quantity > 11 |
total >= |
total >= 49 |
skus.indexOf(sku) > |
skus.indexOf(sku) > 0 |
A named constant doesn’t count: quantity > LIMIT gets false.
Mootant skips some of these changes in common code, such as i →
< items.lengthi != items.length in a loop condition, items.length →
> 0items.length != 0, and true or false for a comparison inside && or ||.
See Skipped changes.
Test the comparison at its boundary and right next to it on both sides. Take this function:
export function shippingCost(total: number): number {
return total >= 50 ? 0 : 5
return total > 50 ? 0 : 5
}The mutant total → >= 50total > 50 survives a test that uses a total
of 100. A test at the boundary catches it:
it('ships orders of exactly 50 for free', () => {
expect(shippingCost(50)).toBe(0)
})With the mutant, a total of 50 costs 5.
Values that catch the other mutants:
total→>=50total == 50: a total above 50, such as 51.total >=→50total >= 49: a total equal to the new number, 49.price < budget: a price below the budget, one equal to it and one above it.tier === 'gold',inandinstanceof: one true case and one false case.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'comparison-operator': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line comparison-operator: sampling
let sampled = Math.random() < 0.01When this mutator is off, or its mutants on a line are ignored, most changes
that other mutators leave to it come back, such as → 5051 in total >= 50
and true and false for return total >= 50 from
return-value. A change that this mutator would make
too stays skipped, such as → 5051 in total > 50.
This mutator has no options.
Mootant leaves out these changes. npx mootant list src --skipped shows them
with the category and the rule; Skipped changes
explains the categories.
| Change | Reason |
|---|---|
price < budget→false | Side of && or ||, left to logical-operatorownedO3 |
index <= last→true | Makes a loop condition always truetimeoutloop-running |
items.length > 0→items.length != 0 | Same result, assuming a lower limit, such as 0 for a lengthequivalentlower-bound |
typeof price === 'number'→true | Type hints show the comparison already has this resultequivalentproven-comparison |
packed >= quantity→packed == quantity in done = … | May keep a loop running forevertimeoutloop-advance |
start <= now→start == now | Compares two objects, such as dates, by identitynoisereference-equality |
price > minPrice→price >= minPrice | Same result for equal values in a > b ? a : bequivalentmin-max-idiom |
i !== boxes→i < boxes | Ends the loop at the same pointequivalentone-way-counter |
a.price > b.price→false | method-call removes the sort() call insteadownedO12 |
stock >= target→true in if (…) break | boolean-literal or condition makes the loop never runownedO13 |
The timeouts setting, on in strict, brings
back loop-running, loop-advance and one-way-counter. The other changes
stay skipped.
A switch matches its case labels without a comparison operator, so this
mutator leaves it alone. number-literal and
string-literal change the labels with their
caseTests option, on in strict.