Skip to content

Commit

Permalink
Merge pull request #54 from power-assert-js/case-skip-capturing-tople…
Browse files Browse the repository at this point in the history
…vel-expression

fix: dealing with node not to be captured that appears immediately beneath assert
  • Loading branch information
twada committed Jun 7, 2019
2 parents c2f8633 + 309e062 commit 0b388de
Show file tree
Hide file tree
Showing 32 changed files with 373 additions and 219 deletions.
30 changes: 17 additions & 13 deletions lib/argument-modification.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class ArgumentModification {
}

leave (controller) {
const shouldCapture = this.isArgumentModified() || toBeCaptured(controller);
const resultNode = shouldCapture ? this.captureArgument(controller) : controller.current();
const currentNode = controller.current();
const shouldCaptureValue = toBeCaptured(controller);
const pathToBeCaptured = shouldCaptureValue ? controller.path() : null;
const shouldCaptureArgument = this.isArgumentModified() || shouldCaptureValue;
const resultNode = shouldCaptureArgument ? this.captureArgument(currentNode, pathToBeCaptured) : currentNode;
if (this.currentArgumentMatchResult.name === 'message' && this.currentArgumentMatchResult.kind === 'optional') {
this.messageUpdated = true;
// enclose it in AssertionMessage
Expand Down Expand Up @@ -60,27 +63,28 @@ class ArgumentModification {
}

captureNode (controller) {
return this.insertRecorder(controller, '_tap');
return this.insertRecorder(controller.current(), controller.path(), '_tap');
}

captureArgument (controller) {
return this.insertRecorder(controller, '_rec');
captureArgument (currentNode, path) {
return this.insertRecorder(currentNode, path, '_rec');
}

// internal

insertRecorder (controller, methodName) {
const currentNode = controller.current();
insertRecorder (currentNode, path, methodName) {
const receiver = this.argumentRecorderIdent;
const path = controller.path();
const relativeEsPath = path.slice(this.assertionPath.length);
const types = new NodeCreator(currentNode);
const args = [
currentNode
];
if (path) {
const relativeEsPath = path.slice(this.assertionPath.length);
args.push(types.stringLiteral(relativeEsPath.join('/')));
}
const newNode = types.callExpression(
types.memberExpression(receiver, types.identifier(methodName)),
[
currentNode,
types.stringLiteral(relativeEsPath.join('/'))
]
args
);
this.argumentModified = true;
return newNode;
Expand Down
186 changes: 109 additions & 77 deletions lib/templates/argument-recorder.json
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,25 @@
},
"alternate": null
},
{
"type": "IfStatement",
"test": {
"type": "UnaryExpression",
"operator": "!",
"prefix": true,
"argument": {
"type": "Identifier",
"name": "espath"
}
},
"consequent": {
"type": "ReturnStatement",
"argument": {
"type": "ThisExpression"
}
},
"alternate": null
},
{
"type": "VariableDeclaration",
"declarations": [
Expand Down Expand Up @@ -1342,83 +1361,6 @@
},
"alternate": null
},
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"object": {
"type": "ThisExpression"
},
"property": {
"type": "Identifier",
"name": "_recorded"
},
"computed": false
},
"right": {
"type": "ObjectExpression",
"properties": [
{
"type": "Property",
"key": {
"type": "Identifier",
"name": "value"
},
"value": {
"type": "Identifier",
"name": "value"
},
"kind": "init",
"method": false,
"shorthand": true,
"computed": false
},
{
"type": "Property",
"key": {
"type": "Identifier",
"name": "logs"
},
"value": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "ArrayExpression",
"elements": []
},
"property": {
"type": "Identifier",
"name": "concat"
},
"computed": false
},
"arguments": [
{
"type": "MemberExpression",
"object": {
"type": "ThisExpression"
},
"property": {
"type": "Identifier",
"name": "_logs"
},
"computed": false
}
]
},
"kind": "init",
"method": false,
"shorthand": false,
"computed": false
}
]
}
}
},
{
"type": "ReturnStatement",
"argument": {
Expand All @@ -1431,6 +1373,96 @@
"finalizer": {
"type": "BlockStatement",
"body": [
{
"type": "IfStatement",
"test": {
"type": "Identifier",
"name": "empowered"
},
"consequent": {
"type": "BlockStatement",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"object": {
"type": "ThisExpression"
},
"property": {
"type": "Identifier",
"name": "_recorded"
},
"computed": false
},
"right": {
"type": "ObjectExpression",
"properties": [
{
"type": "Property",
"key": {
"type": "Identifier",
"name": "value"
},
"value": {
"type": "Identifier",
"name": "value"
},
"kind": "init",
"method": false,
"shorthand": true,
"computed": false
},
{
"type": "Property",
"key": {
"type": "Identifier",
"name": "logs"
},
"value": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "ArrayExpression",
"elements": []
},
"property": {
"type": "Identifier",
"name": "concat"
},
"computed": false
},
"arguments": [
{
"type": "MemberExpression",
"object": {
"type": "ThisExpression"
},
"property": {
"type": "Identifier",
"name": "_logs"
},
"computed": false
}
]
},
"kind": "init",
"method": false,
"shorthand": false,
"computed": false
}
]
}
}
}
]
},
"alternate": null
},
{
"type": "ExpressionStatement",
"expression": {
Expand Down
14 changes: 8 additions & 6 deletions templates/argument-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ module.exports = function () {
* record argument value silently then clear captured logs
* optionally, proxy block argument then store its result as a Log
* @param {any} value - actual value of target argument
* @param {string} espath - espath of target node in AST
* @param {string} [espath] - espath of target node in AST
* @return {any|ArgumentRecorder} - ArgumentRecorder or actual value of target argument
*/
_rec (value, espath) {
const empowered = this._callee && this._callee._empowered;
try {
if (!empowered) return value;
if (!espath) return this;

const log = {
value: wrap(value),
Expand All @@ -123,13 +124,14 @@ module.exports = function () {
});
}

this._recorded = {
value,
logs: [].concat(this._logs)
};

return this;
} finally {
if (empowered) {
this._recorded = {
value,
logs: [].concat(this._logs)
};
}
this._val = value; // actual value of target argument
this._logs = []; // clear logs
}
Expand Down
12 changes: 8 additions & 4 deletions test/fixtures/ArrayExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ var _ArgumentRecorder1 = function () {
try {
if (!empowered)
return value;
if (!espath)
return this;
const log = {
value: wrap(value),
espath
Expand All @@ -276,12 +278,14 @@ var _ArgumentRecorder1 = function () {
}
});
}
this._recorded = {
value,
logs: [].concat(this._logs)
};
return this;
} finally {
if (empowered) {
this._recorded = {
value,
logs: [].concat(this._logs)
};
}
this._val = value;
this._logs = [];
}
Expand Down
12 changes: 8 additions & 4 deletions test/fixtures/ArrowFunctionExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ var _ArgumentRecorder1 = function () {
try {
if (!empowered)
return value;
if (!espath)
return this;
const log = {
value: wrap(value),
espath
Expand All @@ -276,12 +278,14 @@ var _ArgumentRecorder1 = function () {
}
});
}
this._recorded = {
value,
logs: [].concat(this._logs)
};
return this;
} finally {
if (empowered) {
this._recorded = {
value,
logs: [].concat(this._logs)
};
}
this._val = value;
this._logs = [];
}
Expand Down
12 changes: 8 additions & 4 deletions test/fixtures/AssignmentExpression/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ var _ArgumentRecorder1 = function () {
try {
if (!empowered)
return value;
if (!espath)
return this;
const log = {
value: wrap(value),
espath
Expand All @@ -276,12 +278,14 @@ var _ArgumentRecorder1 = function () {
}
});
}
this._recorded = {
value,
logs: [].concat(this._logs)
};
return this;
} finally {
if (empowered) {
this._recorded = {
value,
logs: [].concat(this._logs)
};
}
this._val = value;
this._logs = [];
}
Expand Down

0 comments on commit 0b388de

Please sign in to comment.