Skip to content

Commit

Permalink
fix(assert): fix handling of undefined or null (#3)
Browse files Browse the repository at this point in the history
* fix(assert): fix handling of undefined or null

* fix(example): fix example test

* Update
  • Loading branch information
azu committed Aug 24, 2019
1 parent 4c34563 commit daed652
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/comment-to-assert/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sudo: false
language: node_js
node_js: stable
2 changes: 1 addition & 1 deletion packages/comment-to-assert/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "npm rm comment-to-assert && npm i && node example.js"
"test": "npm i && node example.js"
},
"author": "azu",
"license": "MIT",
Expand Down
10 changes: 9 additions & 1 deletion packages/comment-to-assert/src/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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) {
return;
Expand All @@ -16,6 +18,7 @@ export function tryGetCodeFromComments(comments) {
return matchResult[1];
}
}

function isConsole(node) {
const expression = node.expression;
if (!expression) {
Expand All @@ -35,19 +38,24 @@ function isConsole(node) {
return true;
}
}

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;
const type = expectedNode.type || extractionBody(expectedNode).type;
if (type === Syntax.Identifier && ERROR_COMMENT_PATTERN.test(expectedNode.name)) {
return toAST`assert.throws(function() {
${actualNode}
})`;
} else if (type === Syntax.Identifier && expectedNode.name === "NaN") {
return toAST`assert(isNaN(${actualNode}));`;
} else if (expectedNode.name === "undefined" || expectedNode.name === "null") {
return toAST`assert.equal(${actualNode}, ${expectedNode})`;
} else if (isConsole(actualNode)) {
const args = actualNode.expression.arguments;
const firstArg = args[0];
Expand Down
13 changes: 13 additions & 0 deletions packages/comment-to-assert/test/comment-to-assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function parseToAST(code) {
};
return parse(code, parseOption);
}

describe("comment-to-assert", function() {
describe("#toAssertFromSource", function() {
it("test", function() {
Expand Down Expand Up @@ -94,6 +95,18 @@ describe("comment-to-assert", function() {
assert.equal(a, "str");`;
astEqual(result, expected);
});
it("could handle undefined", function() {
var AST = parseToAST(`this; // => undefined`);
var result = toAssertFromAST(AST);
var expected = `assert.equal(this, undefined);`;
astEqual(result, expected);
});
it("could handle null", function() {
var AST = parseToAST(`this; // => null`);
var result = toAssertFromAST(AST);
var expected = `assert.equal(this, null);`;
astEqual(result, expected);
});
it("can transform multiple comments", function() {
var AST = parseToAST("1; // => 1\n" + "2; // => 2\n");
var resultAST = toAssertFromAST(AST);
Expand Down

0 comments on commit daed652

Please sign in to comment.