bitwise-operator
flags & mask flags | mask This mutator swaps a bitwise or shift operator, as if the code used the wrong
one: flags & mask becomes flags | mask. When | has the number 0 on its
right, it drops | 0 instead, as if the code didn’t cut off the fraction:
cents | 0 becomes cents.
Tests catch these mutants with values for which the two versions differ, such as flags that lack a bit of the mask.
Each operator becomes one other operator:
| Code | Mutant |
|---|---|
options |
options | EXPRESS |
options |
options & EXPRESS |
options |
options | GIFT_WRAP |
1 |
1 >> level |
cents |
cents << 1 |
hash |
hash >> 0 |
Mootant adds parentheses where the new operator would group the code
differently: options | mask ^ extra becomes options & (mask ^ extra).
When the right side of | is the number 0, Mootant drops | 0 instead:
(items / perBox) | 0 becomes (items / perBox), which keeps the fraction.
Check the result with values for which the two versions differ. Take this function:
export const GIFT_WRAP = 1
export const EXPRESS = 2
export function hasExpress(options: number): boolean {
return (options & EXPRESS) !== 0
return (options | EXPRESS) !== 0
}A test that checks only an order with express shipping does not catch
options → & EXPRESSoptions | EXPRESS: both versions return true for that
order. Check an order without express shipping:
it('returns false for an order without express shipping', () => {
expect(hasExpress(GIFT_WRAP)).toBe(false)
})With the mutant, hasExpress(GIFT_WRAP) returns true, as it does for every
order: options | EXPRESS is never 0.
The reverse swap, options → | EXPRESSoptions & EXPRESS, needs options other
than EXPRESS: 1 | 2 is 3, 1 & 2 is 0.
Values that tell the other changes apart:
options→^GIFT_WRAPoptions | GIFT_WRAP: options with gift wrap.1→<<level1 >> level: a level from 1 to 31.cents→>>1cents << 1: a whole number other than 0, such as 10.hash→>>>0hash >> 0: a negative whole number, such as -1.(items / perBox)→| 0(items / perBox): a division with a remainder.
Turn the mutator off in the configuration:
import { defineConfig } from 'mootant'
export default defineConfig({
mutators: {
'bitwise-operator': 'off',
},
})To ignore the mutants of this mutator on one line, put an ignore comment above it:
// mootant-ignore-next-line bitwise-operator: random id
let cartId = (Math.random() * 2 ** 32) >>> 0This 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 |
|---|---|
cents >> 0→cents << 0 | << or >> by 0, or ^ with 0, gives the same resultequivalentidentity-operand |
size << 1→size >> 1 | May keep a loop running forevertimeoutloop-advance |
cents | 0→cents in cents | 0 | 0 | Same code as another mutantduplicateduplicate |
options|mask→options&mask | Vue would read the new &… as an HTML character referenceinvalidtemplate-text |
The timeouts setting, on in strict, brings
back loop-advance. The other changes stay skipped.
Compound assignments, such as options |= mask, belong to
assignment-operator, and ~options to
unary-operator.