-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(instrumenter): place mutants in if statements (#2481)
Place mutants in a statement with an `if` statement instead of a `switch` statement. Fixes #2469
- Loading branch information
Showing
15 changed files
with
284 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
module.exports.isEven = function(number) { | ||
// Note: Implemented with a case switch statement, in order to reproduce this issue: | ||
// https://github.com/stryker-mutator/stryker/issues/2469#issuecomment-690303849 | ||
let mod2 = number % 2; | ||
let isEven; | ||
switch(mod2) { | ||
case 0: { | ||
isEven = true; | ||
break; | ||
} | ||
case 1: { | ||
isEven = false; | ||
break; | ||
} | ||
} | ||
return isEven; | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { isEven } = require('../../src/IsEven'); | ||
const { expect } = require('chai'); | ||
|
||
describe('isEven', () => { | ||
|
||
it('should be false for 1', () => { | ||
expect(isEven(1)).false; | ||
}); | ||
|
||
it('should be true for 2', () => { | ||
expect(isEven(2)).true | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/instrumenter/src/mutant-placers/statement-mutant-placer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { types } from '@babel/core'; | ||
|
||
import { memberExpressionChain, createMutatedAst, mutationCoverageSequenceExpression, ID } from '../util/syntax-helpers'; | ||
|
||
import { MutantPlacer } from './mutant-placer'; | ||
|
||
/** | ||
* Mutant placer that places mutants in statements that allow it. | ||
* It uses an `if` statement to do so | ||
*/ | ||
const statementMutantPlacer: MutantPlacer = (path, mutants) => { | ||
if (path.isStatement()) { | ||
// First transform the mutated ast before we start to apply mutants. | ||
const appliedMutants = mutants.map((mutant) => ({ | ||
mutant, | ||
ast: createMutatedAst(path, mutant), | ||
})); | ||
|
||
// path.replaceWith( | ||
// types.blockStatement([ | ||
// types.switchStatement(memberExpressionChain(ID.GLOBAL, ID.ACTIVE_MUTANT), [ | ||
// ...appliedMutants.map(({ ast, mutant }) => types.switchCase(types.numericLiteral(mutant.id), [ast, types.breakStatement()])), | ||
// types.switchCase(null, [ | ||
// // Add mutation covering statement | ||
// types.expressionStatement(mutationCoverageSequenceExpression(mutants)), | ||
// path.node, | ||
// types.breakStatement(), | ||
// ]), | ||
// ]), | ||
// ]) | ||
// ); | ||
|
||
const instrumentedAst = appliedMutants.reduce( | ||
// Add if statements per mutant | ||
(prev: types.Statement, { ast, mutant }) => | ||
types.ifStatement( | ||
types.binaryExpression('===', memberExpressionChain(ID.GLOBAL, ID.ACTIVE_MUTANT), types.numericLiteral(mutant.id)), | ||
types.blockStatement([ast]), | ||
prev | ||
), | ||
path.isBlockStatement() | ||
? types.blockStatement([types.expressionStatement(mutationCoverageSequenceExpression(mutants)), ...path.node.body]) | ||
: types.blockStatement([types.expressionStatement(mutationCoverageSequenceExpression(mutants)), path.node]) | ||
); | ||
if (path.isBlockStatement()) { | ||
path.replaceWith(types.blockStatement([instrumentedAst])); | ||
} else { | ||
path.replaceWith(instrumentedAst); | ||
} | ||
|
||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
// Export it after initializing so `fn.name` is properly set | ||
export { statementMutantPlacer }; |
36 changes: 0 additions & 36 deletions
36
packages/instrumenter/src/mutant-placers/switch-case-mutant-placer.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.