-
Notifications
You must be signed in to change notification settings - Fork 241
/
TypescriptMutator.spec.ts
166 lines (146 loc) · 5.04 KB
/
TypescriptMutator.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { File } from '@stryker-mutator/api/core';
import { testInjector } from '@stryker-mutator/test-helpers';
import { expect } from 'chai';
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import NodeMutator, { NodeReplacement } from '../../src/mutator/NodeMutator';
import { MUTATORS_TOKEN, TypescriptMutator, typescriptMutatorFactory } from '../../src/TypescriptMutator';
class FunctionDeclarationMutator extends NodeMutator<ts.FunctionDeclaration> {
public name = 'FunctionDeclarationForTest';
public guard(node: ts.Node): node is ts.FunctionDeclaration {
return node.kind === ts.SyntaxKind.FunctionDeclaration;
}
protected identifyReplacements(node: ts.FunctionDeclaration): NodeReplacement[] {
return [
{ node, replacement: '// Function declaration removed' },
{ node, replacement: 'changedToOtherCall()' }
];
}
}
class SourceFileMutator extends NodeMutator<ts.SourceFile> {
public name = 'SourceFileForTest';
public guard(node: ts.Node): node is ts.SourceFile {
return node.kind === ts.SyntaxKind.SourceFile;
}
protected identifyReplacements(node: ts.SourceFile): NodeReplacement[] {
return [{ node, replacement: '"stryker was here"' }];
}
}
function createSut() {
return testInjector.injector
.provideValue(MUTATORS_TOKEN, [new SourceFileMutator(), new FunctionDeclarationMutator()])
.injectClass(TypescriptMutator);
}
describe('TypescriptMutator', () => {
let sut: TypescriptMutator;
it('should register all mutators by default', () => {
// Arrange
const expectedMutatorNames = fs
.readdirSync(path.resolve(__dirname, '..', '..', 'src', 'mutator'))
.filter(
mutatorFile =>
path.extname(mutatorFile) === '.ts' && !mutatorFile.endsWith('.d.ts') && mutatorFile !== 'NodeMutator.ts' && mutatorFile !== 'index.ts'
)
.map(fileName => fileName.substr(0, fileName.length - 'Mutator.ts'.length));
// Act
const actualMutators = testInjector.injector.injectFunction(typescriptMutatorFactory).mutators.map(m => m.name);
// Assert
expect(expectedMutatorNames).length.greaterThan(2); // sanity check
expectedMutatorNames.forEach(mutatorName => expect(actualMutators).includes(mutatorName));
});
describe('using 2 mutators', () => {
let file1: File;
let file2: File;
beforeEach(() => {
sut = createSut();
file1 = new File(
'file1.ts',
`function add(n...: number[]) {
return n.sum();
}
const a = add(1, 3, 4, 5);`
);
file2 = new File(
'file2.ts',
`function subtract(n...: numbers[]){
return n[0] - n.slice(1).sum();
}
const b = subtract(10, 3, 4);`
);
});
it('should deliver 6 mutants', () => {
const mutants = sut.mutate([file1, file2]);
expect(mutants.filter(mutant => mutant.mutatorName === 'SourceFileForTest')).lengthOf(2);
expect(mutants.filter(mutant => mutant.mutatorName === 'FunctionDeclarationForTest')).lengthOf(4);
});
describe('declaration nodes', () => {
beforeEach(() => {
sut = createSut();
});
it('should not skip a node when it does not have a declare modifier', () => {
// Arrange
file1 = new File('file1.ts', 'function mutated(a: number, b: number);');
// Act
const mutants = sut.mutate([file1]);
// Assert
expect(mutants).to.deep.equal([
{
fileName: 'file1.ts',
mutatorName: 'SourceFileForTest',
range: [0, 39],
replacement: '"stryker was here"'
},
{
fileName: 'file1.ts',
mutatorName: 'FunctionDeclarationForTest',
range: [0, 39],
replacement: '// Function declaration removed'
},
{
fileName: 'file1.ts',
mutatorName: 'FunctionDeclarationForTest',
range: [0, 39],
replacement: 'changedToOtherCall()'
}
]);
});
it('should skip a node when it has a declare modifier', () => {
// Arrange
file1 = new File('file1.ts', 'declare function notMutated(a: number, b: number);');
// Act
const mutants = sut.mutate([file1]);
// Assert
expect(mutants).to.deep.equal([
{
fileName: 'file1.ts',
mutatorName: 'SourceFileForTest',
range: [0, 50],
replacement: '"stryker was here"'
}
]);
});
it('should skip a node when it is an interface', () => {
// Arrange
file1 = new File(
'file1.ts',
`interface Hello {
value: string;
sortable?: true;
}`
);
// Act
const mutants = sut.mutate([file1]);
// Assert
expect(mutants).to.deep.equal([
{
fileName: 'file1.ts',
mutatorName: 'SourceFileForTest',
range: [0, 85],
replacement: '"stryker was here"'
}
]);
});
});
});
});