-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
incremental commit: using comments to suppress mutators
- Loading branch information
Showing
10 changed files
with
181 additions
and
19 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
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,79 @@ | ||
import R = require("ramda"); | ||
|
||
const PERTURB_ENABLE = "perturb-enable:"; | ||
const PERTURB_DISABLE = "perturb-disable:"; | ||
|
||
interface Operator { | ||
type: string; | ||
name: string; | ||
} | ||
|
||
interface Comment { | ||
type: string; | ||
value: string; | ||
range: number[]; | ||
} | ||
|
||
interface CommentedNode extends ESTree.Node { | ||
leadingComments?: Comment[]; | ||
trailingComments?: Comment[]; | ||
} | ||
|
||
function applyNodeComments (_node: ESTree.Node, disabledSet: Set<string>) { | ||
const node = <CommentedNode>_node; | ||
R.pipe( | ||
_getComments, | ||
R.chain(_extractOperators), | ||
R.forEach(_applyOperator(disabledSet)) | ||
)(node); | ||
} | ||
|
||
const _applyOperator = R.curry(function (set: Set<string>, op: Operator) { | ||
switch (op.type) { | ||
case "enable": { | ||
console.log("ENABLE", op.name) | ||
return set.add(op.name); | ||
} | ||
case "disable": { | ||
console.log("DISABLE", op.name); | ||
return set.delete(op.name); | ||
} | ||
} | ||
}); | ||
|
||
function _getComments (node: CommentedNode): Comment[] { | ||
return [].concat( | ||
node.leadingComments || [], | ||
node.trailingComments || [] | ||
); | ||
} | ||
|
||
function _extractOperators (c: Comment): Operator[] { | ||
const value = c.value.trim(); | ||
|
||
console.log("RAW VALUE", value); | ||
|
||
let type; | ||
if (value.startsWith(PERTURB_ENABLE)) { | ||
type = "enable"; | ||
} else if (value.startsWith(PERTURB_DISABLE)) { | ||
type = "disable"; | ||
} | ||
if (type == null) return null; | ||
|
||
return R.pipe( | ||
R.split(":"), | ||
R.prop("1"), | ||
R.split(","), | ||
R.filter(Boolean), | ||
R.map(R.trim), | ||
R.map(name => ({ type, name: String(name) })) | ||
)(value); | ||
} | ||
|
||
export = { | ||
_getComments, | ||
_extractOperators, | ||
_applyOperator, | ||
applyNodeComments, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import R = require("ramda"); | ||
import S = require("./_syntax"); | ||
import dropItem = require("../util/drop-item"); | ||
import dropEachOfProp = require("../util/drop-each-of-prop"); | ||
|
||
// drops each argument to a function/method call in turn | ||
// input: `fn(a, b, c)` | ||
// output: [`fn(b, c)`, `fn(a, c)`, `fn(a, b)`] | ||
|
||
export = <MutatorPlugin>{ | ||
// drops the first declared element in an array literal | ||
// `['a', 'b']` => `['a']` | ||
name: "tweak-arguments", | ||
nodeTypes: [S.CallExpression], | ||
filter: function (node) { | ||
return R.path(["arguments", "length"], node) !== 0; | ||
}, | ||
mutator: function (node) { | ||
return dropItem(node, "arguments", "first"); | ||
mutator: function (node): ESTree.Node[] { | ||
return dropEachOfProp("arguments", node); | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export = async function runMutant (runner: RunnerPlugin, mutant: Mutant) { | ||
const before = await runner.setup(mutant); | ||
const result: RunnerResult = await runner.run(mutant); | ||
await runner.cleanup(mutant, before); | ||
return result; | ||
export = function runMutant (runner: RunnerPlugin, mutant: Mutant) { | ||
return runner.setup(mutant) | ||
.then(function (before) { | ||
return runner.run(mutant) | ||
.finally(() => runner.cleanup(mutant, before)) | ||
}); | ||
} |
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,67 @@ | ||
const expect = require("expect"); | ||
const esprima = require("esprima"); | ||
const comments = require("../built/comments"); | ||
|
||
const helpers = require("./helpers"); | ||
|
||
const ESPRIMA_OPTIONS = { | ||
attachComment: true, | ||
comments: true, | ||
loc: true, | ||
}; | ||
|
||
function createTest (obj) { | ||
it(obj.title, function () { | ||
const node = helpers.nodeFromCode(obj.code); | ||
const set = obj.set || new Set(); | ||
const expected = obj.expected; | ||
comments.applyNodeComments(node, set); | ||
expect([...set]).toEqual(expected); | ||
}); | ||
} | ||
|
||
|
||
describe("comments", function () { | ||
|
||
describe("comments", function () { | ||
|
||
createTest({ | ||
title: "enable: works for leading line comments", | ||
expected: "abc".split(""), | ||
code: ` | ||
// perturb-enable: a,,b,c | ||
const x = 1; | ||
`, | ||
}); | ||
|
||
createTest({ | ||
title: "enable: works for trailing line comments", | ||
expected: "abc".split(""), | ||
code: ` | ||
const x = 1; | ||
// perturb-enable: a,,b,c | ||
`, | ||
}); | ||
|
||
createTest({ | ||
title: "enable: works for leading and trailing comments", | ||
expected: "abcd".split(""), | ||
code: ` | ||
// perturb-enable: a,,b,c | ||
const x = 1; | ||
// perturb-enable: d | ||
`, | ||
}); | ||
|
||
createTest({ | ||
title: "disable: works for leading and trailing comments", | ||
expected: ["b"], | ||
code: ` | ||
// perturb-enable: a,b | ||
const x = 1; | ||
// perturb-disable: a | ||
`, | ||
}); | ||
}); | ||
|
||
}); |
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