Skip to content

Commit

Permalink
feat(error): support handling Error: message
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 24, 2019
1 parent 4989865 commit 33f1b70
Show file tree
Hide file tree
Showing 6 changed files with 2,676 additions and 44 deletions.
13 changes: 12 additions & 1 deletion packages/comment-to-assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@
"test": "test"
},
"scripts": {
"precommit": "lint-staged",
"postcommit": "git reset",
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"prepublish": "npm run --if-present build",
"test": "mocha && npm run example",
"example": "npm i && npm run build && cd example && npm test"
},
"lint-staged": {
"*.{js,ts,tsx,css}": [
"prettier --write",
"git add"
]
},
"keywords": [
"ast",
"testing",
Expand All @@ -52,7 +60,10 @@
"babel-preset-es2015": "^6.18.0",
"babel-preset-power-assert": "^1.0.0",
"babel-register": "^6.9.0",
"husky": "^0.14.3",
"lint-staged": "^4.1.3",
"mocha": "^3.1.2",
"power-assert": "^1.4.1"
"power-assert": "^1.4.1",
"prettier": "^1.6.1"
}
}
4 changes: 4 additions & 0 deletions packages/comment-to-assert/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
printWidth: 120,
tabWidth: 4
};
15 changes: 8 additions & 7 deletions packages/comment-to-assert/src/ast-utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// LICENSE : MIT
"use strict";
import {Syntax} from "estraverse"
import assert from "assert"
import toAST from "tagged-template-to-ast"
import { Syntax } from "estraverse";
import assert from "assert";
import toAST from "tagged-template-to-ast";
const commentCodeRegExp = /=>\s*?(.*?)$/i;
export function tryGetCodeFromComments(comments) {
if (comments.length === 0) {
Expand All @@ -26,10 +26,10 @@ function isConsole(node) {
}
const callee = expression.callee;
if (!callee) {
return false
return false;
}
if (!callee.object) {
return false
return false;
}
if (callee.object.name === "console") {
return true;
Expand All @@ -38,10 +38,11 @@ function isConsole(node) {
function extractionBody(ast) {
return ast.body[0];
}
export const ERROR_COMMENT_PATTERN = /^([a-zA-Z]*?Error)/;
export function wrapAssert(actualNode, expectedNode) {
assert(typeof expectedNode !== "undefined");
var type = expectedNode.type || extractionBody(expectedNode).type;
if (type === Syntax.Identifier && /^[a-zA-Z]*?Error$/.test(expectedNode.name)) {
if (type === Syntax.Identifier && ERROR_COMMENT_PATTERN.test(expectedNode.name)) {
return toAST`assert.throws(function() {
${actualNode}
})`;
Expand All @@ -56,4 +57,4 @@ export function wrapAssert(actualNode, expectedNode) {
} else {
return toAST`assert.deepEqual(${actualNode}, ${expectedNode})`;
}
}
}
24 changes: 16 additions & 8 deletions packages/comment-to-assert/src/comment-to-assert.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// LICENSE : MIT
"use strict";
const espree = require("espree");
import escodegen from "escodegen"
import estraverse from "estraverse"
import {
tryGetCodeFromComments,
wrapAssert
} from "./ast-utils"
import escodegen from "escodegen";
import estraverse from "estraverse";
import { ERROR_COMMENT_PATTERN, tryGetCodeFromComments, wrapAssert } from "./ast-utils";
import { Syntax } from "estraverse";

const parseOptions = {
// attach range information to each node
range: true,
Expand Down Expand Up @@ -50,16 +49,25 @@ export function toAssertFromSource(code, filePath) {
}

function getExpressionNodeFromCommentValue(string) {
const message = string.trim();
if (ERROR_COMMENT_PATTERN.test(message)) {
const match = message.match(ERROR_COMMENT_PATTERN);
return {
type: Syntax.Identifier,
name: match[1]
};
}
// support { } object literal
const commentExpression = `0, ${string}`;
try {
const AST = espree.parse(commentExpression, parseOptions);
return AST.body[0].expression.expressions[1];
} catch (e) {
console.error(`Not parsable comments // => expression`);
console.error(`Can't parse comments // => expression`);
console.error(e);
}
}

/**
* transform AST to asserted AST.
* @param {ESTree.Node} ast
Expand All @@ -78,4 +86,4 @@ export function toAssertFromAST(ast) {
}
});
return ast;
}
}
69 changes: 41 additions & 28 deletions packages/comment-to-assert/test/comment-to-assert-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import assert from "power-assert"
import {
toAssertFromSource,
toAssertFromAST
} from "../src/comment-to-assert"
import {parse} from "esprima"
import astEqual from "ast-equal"
import assert from "power-assert";
import { toAssertFromSource, toAssertFromAST } from "../src/comment-to-assert";
import { parse } from "esprima";
import astEqual from "ast-equal";

function parseToAST(code) {
const parseOption = {
Expand All @@ -18,19 +15,19 @@ function parseToAST(code) {
describe("comment-to-assert", function() {
describe("#toAssertFromSource", function() {
it("test", function() {

function sum(...values) {
return values.reduce((total, value) => {
console.assert(Number.isFinite(value), `${ value }は有限数ではありません`);
console.assert(Number.isFinite(value), `${value}は有限数ではありません`);
return total + Number(value);
}, 0);
}

let x = 1, y, z = 10;
let x = 1,
y,
z = 10;
assert.throws(function() {
sum(x, y, z);
}, Error);

});
it("should return code", function() {
var code = "var a = 1;";
Expand All @@ -49,8 +46,7 @@ describe("comment-to-assert", function() {
});

it("should handle module", function() {
var code = "const a = 1;" +
"a;// => 1";
var code = "const a = 1;" + "a;// => 1";
var result = toAssertFromSource(code, "file.js");
assert(typeof result === "string");
});
Expand Down Expand Up @@ -99,36 +95,42 @@ describe("comment-to-assert", function() {
astEqual(result, expected);
});
it("can transform multiple comments", function() {
var AST = parseToAST("1; // => 1\n" +
"2; // => 2\n");
var AST = parseToAST("1; // => 1\n" + "2; // => 2\n");
var resultAST = toAssertFromAST(AST);
astEqual(resultAST, `
astEqual(
resultAST,
`
assert.equal(1, 1);
assert.equal(2, 2);
`);
`
);
});
it("can transform + BinaryExpression", function() {
var AST = parseToAST("var a = function(){return 1;};\n" +
"a + 1; // => 2");
var AST = parseToAST("var a = function(){return 1;};\n" + "a + 1; // => 2");
var resultAST = toAssertFromAST(AST);
astEqual(resultAST, `
astEqual(
resultAST,
`
var a = function () {
return 1;
};
assert.equal(a + 1 , 2);
`);
`
);
});
it("can transform CallExpression", function() {
var AST = parseToAST("function add(x,y){ return x + y}\n" +
"add(1,2);// => 3");
var AST = parseToAST("function add(x,y){ return x + y}\n" + "add(1,2);// => 3");
var resultAST = toAssertFromAST(AST);
astEqual(resultAST, `
astEqual(
resultAST,
`
function add(x, y) {
return x + y
}
assert.equal(add(1, 2), 3);
`);
`
);
});
it("could handle BlockComment", function() {
var AST = parseToAST(`1; /* => 1 */`);
Expand All @@ -152,6 +154,14 @@ describe("comment-to-assert", function() {
});`;
astEqual(result, expected);
});
it("could handle Error with message", function() {
var AST = parseToAST(`throw new Error("x is not defined");// => ReferenceError: x is not defined`);
var result = toAssertFromAST(AST);
var expected = `assert.throws(function() {
throw new Error("x is not defined");
});`;
astEqual(result, expected);
});
it("could handle NaN", function() {
var AST = parseToAST(`+"str";// => NaN`);
var result = toAssertFromAST(AST);
Expand All @@ -161,9 +171,12 @@ describe("comment-to-assert", function() {
it("can transform console comments", function() {
var AST = parseToAST("console.log(1); // => 1");
var resultAST = toAssertFromAST(AST);
astEqual(resultAST, `
astEqual(
resultAST,
`
assert.deepEqual(1, 1);
`);
`
);
});
});
});
});
Loading

0 comments on commit 33f1b70

Please sign in to comment.