Skip to content

Commit

Permalink
reworking mutator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bttmly committed May 25, 2015
1 parent bd30982 commit c93c50a
Show file tree
Hide file tree
Showing 5 changed files with 734 additions and 46 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
./node_modules/.bin/_mocha ./test/**/*.js
NODE_ENV=testing ./node_modules/.bin/_mocha ./test/**/*.js

lint:
./node_modules/.bin/jshint ./**/*.js
Expand All @@ -14,9 +14,9 @@ example-i:
./bin/perturb -r ./example -i

dogfood:
./bin/perturb -r ./ -c 'make test'
NODE_ENV=testing ./bin/perturb -r ./ -c 'make test'

dogfood-i:
./bin/perturb -r ./ -i
NODE_ENV=testing ./bin/perturb -r ./ -i

.PHONY: test example
5 changes: 2 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function perturb (settings, cb) {
var perturbSourceFiles = glob.sync(perturbSourceDir + config.sourceGlob);
var perturbTestFiles = glob.sync(perturbTestDir + config.testGlob);

var matches = perturbMatch(perturbSourceFiles, perturbTestFiles, config.matcher.bind(config));
var matches = matchFiles(perturbSourceFiles, perturbTestFiles, config.matcher.bind(config));

if (matches.length === 0) return cb(new Error(ERRORS.NoMatches));

Expand All @@ -115,7 +115,6 @@ function perturb (settings, cb) {

if (config.verbose) config.configReporter(config);

// var start = process.hrtime();
var start = Date.now();

async.mapSeries(matches, handleMatch, function (err, matches) {
Expand Down Expand Up @@ -165,7 +164,7 @@ function defaultConfig () {
return config;
}

function perturbMatch (sourceFiles, testFiles, matcher) {
function matchFiles (sourceFiles, testFiles, matcher) {
var testFileMap = util.mapMirror(testFiles);

return sourceFiles.reduce(function (matches, sourceFile) {
Expand Down
75 changes: 45 additions & 30 deletions lib/mutators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var util = require("util");

var I = require("immutable");
var Immutable = require("immutable");
var contains = require("lodash.contains");
var assign = require("object-assign");

Expand All @@ -18,13 +18,6 @@ var VOID = "VOID";
var AND = "&&";
var OR = "||";

function get (obj, prop) {
if (typeof obj.get === JS_TYPES.func) {
return obj.get(prop);
}
return obj[prop];
}

function setValue (node, value) {
return node.merge({
value: value,
Expand Down Expand Up @@ -56,7 +49,7 @@ function numberLiteralTweakFn (node) {
}

function booleanLiteralTweakFn (node) {
return setValue(node, !get(node, NODE_ATTRS.value));
return setValue(node, node.get(NODE_ATTRS.value));
}

function objectLiteralTweakFn (node) {
Expand Down Expand Up @@ -93,7 +86,7 @@ var mutators = Object.freeze([
return node.has(NODE_ATTRS.test);
},
mutator: function (node) {
return node.set(NODE_ATTRS.test, I.Map({
return node.set(NODE_ATTRS.test, Immutable.Map({
type: NODE_TYPES.UnaryExpression,
operator: BANG,
argument: node.get(NODE_ATTRS.test)
Expand Down Expand Up @@ -121,7 +114,7 @@ var mutators = Object.freeze([
var arg = node.get(NODE_ATTRS.argument);

if (arg) {
return I.Map({
return Immutable.Map({
type: NODE_TYPES.ExpressionStatement,
expression: arg
});
Expand Down Expand Up @@ -238,6 +231,7 @@ var mutators = Object.freeze([
}, {
// drops a function call made for side effects
// (the return value isn't assigned to a variable)
// (will this cause lots of test timeouts due to uncalled callbacks?)
name: "dropVoidCall",
type: NODE_TYPES.ExpressionStatement,
filter: function (node) {
Expand All @@ -246,7 +240,6 @@ var mutators = Object.freeze([
}

// skip method calls of objects since often called for side effects on `this`
// (also accounts for promises)
if (node.getIn(["callee", "type"]) !== NODE_TYPES.Identifier) {
return false;
}
Expand All @@ -264,7 +257,7 @@ var mutators = Object.freeze([
]);

function voidNode () {
return I.fromJs({
return Immutable.fromJS({
type: NODE_TYPES.UnaryExpression,
operator: VOID,
argument: {
Expand All @@ -277,8 +270,12 @@ function voidNode () {
}

function wrapMutator (m) {
return assign({}, m, {

var extendWith = {
mutator: function (node) {

assert(I.Iterable.isKeyed(node), "Argument must be an immutable keyed iterable.")

var type = node.get(NODE_ATTRS.type);

if (!contains(m.type, type)) {
Expand All @@ -288,41 +285,59 @@ function wrapMutator (m) {

return m.mutator(node);
}
});
}
};

var mutatorsIndexedByNodeType = mutators.map(wrapMutator).reduce(function (obj, mutator) {
var types = mutator.type;
if (m.filter) {
extendWith.filter = function (node) {
assert(I.Iterable.isKeyed(node), "Argument must be an immutable keyed iterable.");

if (!Array.isArray(types)) types = [types];
return m.filter(node);
};
}

types.forEach(function (t) {
if (obj[t] == null) obj[t] = [];
obj[t].push(mutator);
});
return assign({}, m, extendWith);
}

return obj;
}, {});
var mutatorsIndexedByNodeType = mutators
.map(wrapMutator)
.reduce(function (obj, mutator) {
var types = mutator.type;

function parseComment (comment) {
var c = comment.get("value");
if (c.trim() !== "") {
if (!Array.isArray(types)) types = [types];

types.forEach(function (t) {
if (obj[t] == null) obj[t] = [];
obj[t].push(mutator);
});

return obj;
}, {}
);

}
// WIP (eventually for turning on/off mutations)
function parseComment (comment) {
var c = comment.get("value");
if (c.trim() === "") return;
}


module.exports = {
mutatorsExistForNodeType: function (type) {
assert(isString(type), "Argument must be a string");
return mutatorsIndexedByNodeType.hasOwnProperty(type);
},

getMutatorsForNode: function (node) {
assert(I.Iterable.isKeyed(node), "Argument must be an immutable keyed iterable.")
return mutatorsIndexedByNodeType[node.get("type")] || [];
},

mutators: mutators
get mutators () {
if (process.env.NODE_ENV !== "testing") {
throw new Error("'mutators' property can only be read during testing.");
}
return mutators;
}
};


Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"chai": "^1.10.0",
"growl": "^1.8.1",
"jshint": "^2.6.0",
"lodash": "^3.9.2",
"sync-exec": "^0.4.0",
"watch": "^0.13.0"
},
Expand Down
Loading

0 comments on commit c93c50a

Please sign in to comment.