Skip to content

Commit

Permalink
feat(example): add example
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 24, 2019
1 parent ef61120 commit de07e6a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/comment-to-assert/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ crashlytics.properties
crashlytics-build.properties



lib
10 changes: 10 additions & 0 deletions packages/comment-to-assert/example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// LICENSE : MIT
"use strict";
var assert = require("assert");
var toAssert = require("comment-to-assert");
var resultOfPrimitive = toAssert.commentToAssertFromCode("1;// => 1");
assert.equal(resultOfPrimitive, 'assert.equal(1, 1);');
var resultOfObject = toAssert.commentToAssertFromCode("[1];// => [1]");
assert.equal(resultOfPrimitive, 'assert.deepEqual([1], [1]);');
var resultOfIdentifier = toAssert.commentToAssertFromCode("var foo=1;foo;// => 1");
assert.equal(resultOfPrimitive, 'assert.equal(foo, 1);');
10 changes: 8 additions & 2 deletions packages/comment-to-assert/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
},
"version": "1.0.0",
"description": "convert single line comment to assert.",
"main": "index.js",
"main": "lib/comment-to-assert.js",
"files": [
"lib"
],
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "babel src --out-dir lib --source-maps",
"watch": "babel src --out-dir lib --watch --source-maps",
"test": "mocha test/*",
"example": "npm i && npm run build && cd example && npm test"
},
"keywords": [
"ast",
Expand Down
10 changes: 9 additions & 1 deletion packages/comment-to-assert/src/comment-to-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import {
wrapAssert
} from "./ast-utils"
export function commentToAssertFromCode(code) {
return code;
var parseOption = {
loc: true,
range: true,
comment: true,
attachComment: true
};
var AST = parse(code, parseOption);
var modifiedAST = commentToAssertFromAST(AST);
return generate(modifiedAST);
}
export function commentToAssertFromAST(ast) {
estraverse.replace(ast, {
Expand Down
7 changes: 6 additions & 1 deletion packages/comment-to-assert/test/comment-to-assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "power-assert"
import {
commentToAssertFromCode,
commentToAssertFromAST
} from "../src/comment-to-assert"
} from "../src/comment-to-assert"
import {parse} from "esprima"
import {astEqual} from "./lib/ast-equal"

Expand All @@ -29,6 +29,11 @@ describe("comment-to-assert", function () {
assert(typeof result === "string");
astEqual(result, code);
});
it("should keep code mean", function () {
var code = "1;// => 1";
var result = commentToAssertFromCode(code);
assert.equal(result, "assert.equal(1, 1);");
});
});
describe("#commentToAssertFromAST", function () {
it("should return AST", function () {
Expand Down

0 comments on commit de07e6a

Please sign in to comment.