Skip to content

Commit

Permalink
Refactor refactor.js module and add tests. Fix #14
Browse files Browse the repository at this point in the history
Signed-off-by: Kaustav Das Modak <[email protected]>
  • Loading branch information
Kaustav Das Modak committed Feb 11, 2016
1 parent dc1795a commit 25573c1
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/refactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@ var fs = require("fs"),
estraverse = require("estraverse"),
escodegen = require("escodegen");

var refactor_content = function (content, info, req) {
var processNode = function (node, req) {
node.arguments[0].value = req.newStepValue.parameterizedStepValue;
node.arguments[0].raw = "\"" + req.newStepValue.parameterizedStepValue + "\"";
if (node.arguments[1] && node.arguments[1].type === "FunctionExpression") {
node.arguments[1].params = req.newStepValue.parameters.map(function (param) {
return {
type: "Identifier",
name: param
};
});
}
return node;
};

var refactor_content = function (content, info, req) {
var ast = esprima.parse(content);
estraverse.replace(ast, {
enter: function (node) {
if (node.type === "CallExpression" && node.callee.name === "gauge" && node.arguments[0].value === info.stepText) {
node.arguments[0].value = req.newStepValue.parameterizedStepValue;
node.arguments[0].raw = "\"" + req.newStepValue.parameterizedStepValue + "\"";
if (node.arguments[1] && node.arguments[1].type === "FunctionExpression") {
var newparams = [];
req.paramPositions.forEach(function (param, i) {
if (param.oldPosition < 0) {
newparams[param.newPosition] = {
type: "Identifier",
name: req.newStepValue.parameters[i]
};
} else {
newparams[param.newPosition] = node.arguments[1].params[param.oldPosition];
}
});
node.arguments[1].params = newparams;
}
node = processNode(node, req);
}
return node;
}
Expand Down
229 changes: 229 additions & 0 deletions test/refactor-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/* globals stepRegistry */

var assert = require( "chai" ).assert;

require( "../src/gauge-global" );

var refactor = require( "../src/refactor" );
var factory = require( "../src/response-factory" );
var fs = require( "fs" );
var sinon = require( "sinon" );

var sandbox, request, response;
var contentInput, contentOutput, outputFile, info;

describe( "Refactor", function () {

before( function () {
sandbox = sinon.sandbox.create();

sandbox.stub( fs, "readFileSync", function () {
return contentInput;
});

sandbox.stub( fs, "writeFileSync", function ( file, data ) {
outputFile = file;
contentOutput = data;
});

sandbox.stub( stepRegistry, "get", function () {
return info;
});
});

beforeEach( function () {
response = factory.createRefactorResponse( 123 );
});

after( function () {
sandbox.restore();
});

it( "Should refactor step text without changing function ref", function () {
var output = [];
output.push( "var vowels=[\n \'a\',\n \'e\',\n \'i\',\n \'o\',\n \'u\'\n];" );
output.push( "hakunaMatata('What a wonderful phrase!');" );
output.push( "gauge('The word <word> has <number> vowels.', function (word, number) {\n});" );
output.push( "var myfn = function (number) {\n};" );
output.push( "gauge('There are <number> vowels.', myfn);" );
contentInput = output.join( "\n" );

request = {
refactorRequest: {
oldStepValue: {
stepValue: "The word {} has {} vowels.",
parameterizedStepValue: "The word <word> has <number> vowels.",
parameters: [ "word", "number" ]
},
newStepValue: {
stepValue: "This English word {} has {} vowels.",
parameterizedStepValue: "This English word <word> has <number> vowels.",
parameters: [ "word", "number" ]
},
paramPositions: [ {
oldPosition: 0,
newPosition: 0
}, {
oldPosition: 1,
newPosition: 1
} ]
}
};

info = {
fn: function ( word, number ) { word = "such"; number = "wow"; },
stepText: "The word <word> has <number> vowels.",
generalisedText: "The word {} has {} vowels.",
filePath: "test/data/refactor-output.js"
};

response = refactor( request, response, fs );
assert.strictEqual( response.refactorResponse.error, "" );
assert.strictEqual( response.refactorResponse.success, true );
assert.strictEqual( response.refactorResponse.filesChanged.length, 1 );
});

it( "Should perform refactoring when param names are changed", function () {
contentInput = "gauge('The word <word> has <number> vowels.', function (word, number) {\n});";
request = {
refactorRequest: {
oldStepValue: {
stepValue: "The word {} has {} vowels.",
parameterizedStepValue: "The word <word> has <number> vowels.",
parameters: [ "word", "number" ]
},
newStepValue: {
stepValue: "This English word {} has {} vowels.",
parameterizedStepValue: "This English word <word_en> has <numbers> vowels.",
parameters: [ "word_en", "numbers" ]
},
paramPositions: [ {
oldPosition: -1,
newPosition: 0
}, {
oldPosition: -1,
newPosition: 1
} ]
}
};

info = {
fn: function ( word, number ) { word = "such"; number = "wow"; },
stepText: "The word <word> has <number> vowels.",
generalisedText: "The word {} has {} vowels.",
filePath: "test/data/refactor-output.js"
};

response = refactor( request, response );

assert.strictEqual( contentOutput, "gauge('This English word <word_en> has <numbers> vowels.', function (word_en, numbers) {\n});");
});

it( "Should perform refactoring when params are removed", function () {
contentInput = "gauge('The word <word> has <number> vowels.', function (word, number) {\n});";
request = {
refactorRequest: {
oldStepValue: {
stepValue: "The word {} has {} vowels.",
parameterizedStepValue: "The word <word> has <number> vowels.",
parameters: [ "word", "number" ]
},
newStepValue: {
stepValue: "This English word {} has {} vowels.",
parameterizedStepValue: "This English word has <numbers> vowels.",
parameters: [ "numbers" ]
},
paramPositions: [ {
oldPosition: -1,
newPosition: 0
}]
}
};

info = {
fn: function ( word, number ) { word = "such"; number = "wow"; },
stepText: "The word <word> has <number> vowels.",
generalisedText: "The word {} has {} vowels.",
filePath: "test/data/refactor-output.js"
};

response = refactor( request, response );

assert.strictEqual( contentOutput, "gauge('This English word has <numbers> vowels.', function (numbers) {\n});");
});

it( "Should perform refactoring when params are reordered", function () {
contentInput = "gauge('The word <word> has <number> vowels.', function (word, number) {\n});";
request = {
refactorRequest: {
oldStepValue: {
stepValue: "The word {} has {} vowels.",
parameterizedStepValue: "The word <word> has <number> vowels.",
parameters: [ "word", "number" ]
},
newStepValue: {
stepValue: "There are {} vowels in the word {}",
parameterizedStepValue: "There are <number> vowels in the word <word>.",
parameters: [ "number", "word" ]
},
paramPositions: [ {
oldPosition: 0,
newPosition: 1
}, {
oldPosition: 1,
newPosition: 0
} ]
}
};

info = {
fn: function ( word, number ) { word = "such"; number = "wow"; },
stepText: "The word <word> has <number> vowels.",
generalisedText: "The word {} has {} vowels.",
filePath: "test/data/refactor-output.js"
};

response = refactor( request, response );

assert.strictEqual( contentOutput, "gauge('There are <number> vowels in the word <word>.', function (number, word) {\n});");
});

it( "Should perform refactoring when new params are added", function () {
contentInput = "gauge('The word <word> has <number> vowels.', function (word, number) {\n});";
request = {
refactorRequest: {
oldStepValue: {
stepValue: "The word {} has {} vowels.",
parameterizedStepValue: "The word <word> has <number> vowels.",
parameters: [ "word", "number" ]
},
newStepValue: {
stepValue: "The word {} has {} vowels and ends with {}.",
parameterizedStepValue: "The word <word> has <number> vowels and ends with <end_letter>.",
parameters: [ "word", "number", "end_letter" ]
},
paramPositions: [ {
oldPosition: -1,
newPosition: 0
}, {
oldPosition: -1,
newPosition: 2
}, {
oldPosition: 0,
newPosition: 1
} ]
}
};

info = {
fn: function ( word, number ) { word = "such"; number = "wow"; },
stepText: "The word <word> has <number> vowels.",
generalisedText: "The word {} has {} vowels.",
filePath: "test/data/refactor-output.js"
};

response = refactor( request, response );

assert.strictEqual( contentOutput, "gauge('The word <word> has <number> vowels and ends with <end_letter>.', function (word, number, end_letter) {\n});");
});
});

0 comments on commit 25573c1

Please sign in to comment.