Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#23 restrict equalityoperator mutator #41

Merged
merged 10 commits into from
Nov 22, 2023
Prev Previous commit
Next Next commit
Add test for arithmetic operator
dvcopae committed Nov 15, 2023
commit 9f4f4e6b42225da7c4e74ddfd9ff26ea1b89de2f
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ const arithmeticOperatorReplacements = Object.freeze({
'+': { replacement: '-', mutatorName: '+To-' },
'-': { replacement: '+', mutatorName: '-To+' },
'*': { replacement: '/', mutatorName: '*To/' },
'/': { replacement: '/', mutatorName: '/To*' },
'/': { replacement: '*', mutatorName: '/To*' },
'%': { replacement: '*', mutatorName: '%To*' },
} as const);

@@ -28,6 +28,7 @@ export const arithmeticOperatorMutator: NodeMutator = {
};

function isInMutationLevel(node: types.BinaryExpression, level?: MutationLevel): boolean {
// No mutation level specified, so allow everything
if (level === undefined) {
return true;
}
13 changes: 12 additions & 1 deletion packages/instrumenter/test/helpers/expect-mutation.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ import { parse, ParserPlugin } from '@babel/parser';
import generator from '@babel/generator';
import { expect } from 'chai';

import { MutationLevel } from '@stryker-mutator/api/core';

import { NodeMutator } from '../../src/mutators/node-mutator.js';

const generate = generator.default;
@@ -36,6 +38,15 @@ const plugins = [
] as ParserPlugin[];

export function expectJSMutation(sut: NodeMutator, originalCode: string, ...expectedReplacements: string[]): void {
expectJSMutationWithLevel(sut, undefined, originalCode, ...expectedReplacements);
}

export function expectJSMutationWithLevel(
sut: NodeMutator,
level: MutationLevel | undefined,
originalCode: string,
...expectedReplacements: string[]
): void {
const sourceFileName = 'source.js';
const ast = parse(originalCode, {
sourceFilename: sourceFileName,
@@ -47,7 +58,7 @@ export function expectJSMutation(sut: NodeMutator, originalCode: string, ...expe

babel.traverse(ast, {
enter(path) {
for (const replacement of sut.mutate(path)) {
for (const replacement of sut.mutate(path, level)) {
const mutatedCode = generate(replacement).code;
const beforeMutatedCode = originalCode.substring(0, path.node.start ?? 0);
const afterMutatedCode = originalCode.substring(path.node.end ?? 0);
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { expect } from 'chai';

import { MutationLevel } from '@stryker-mutator/api/core';

import { arithmeticOperatorMutator as sut } from '../../../src/mutators/arithmetic-operator-mutator.js';
import { expectJSMutation } from '../../helpers/expect-mutation.js';
import { expectJSMutation, expectJSMutationWithLevel } from '../../helpers/expect-mutation.js';

const arithmeticLevel: MutationLevel = { name: 'ArithemticLevel', ArithmeticOperator: ['+To-', '-To+', '*To/'] };

describe(sut.name, () => {
it('should have name "ArithmeticOperator"', () => {
@@ -30,4 +34,15 @@ describe(sut.name, () => {

expectJSMutation(sut, '"a" + b + "c" + d + "e"');
});

it('should only mutate +, - and * from all possible mutators', () => {
expectJSMutationWithLevel(
sut,
arithmeticLevel,
'a + b; a - b; a * b; a % b; a / b; a % b',
'a - b; a - b; a * b; a % b; a / b; a % b', // mutates +
'a + b; a + b; a * b; a % b; a / b; a % b', // mutates -
'a + b; a - b; a / b; a % b; a / b; a % b', // mutates *
);
});
});