Skip to content

Commit

Permalink
cleaner tests for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 5, 2019
1 parent aa6c5a7 commit f29859f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "The minimal & fast UI library",
"scripts": {
"build": "node build/build.js",
"test": "jest"
"test": "MOON_ENV=\"development\" jest"
},
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@

if ("development" === "development" && charNext === undefined) {
lexError("Lexer expected a character after \"<\".", input, i);
break;
}

if (charNext === "/") {
Expand All @@ -158,6 +159,7 @@

if ("development" === "development" && closeIndex === -1) {
lexError("Lexer expected a closing \">\" after \"</\".", input, i);
break;
}

tokens.push({
Expand All @@ -172,6 +174,7 @@

if ("development" === "development" && _closeIndex === -1) {
lexError("Lexer expected a closing \"-->\" after \"<!--\".", input, i);
break;
}

i = _closeIndex + 3;
Expand Down
3 changes: 3 additions & 0 deletions packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function lex(input) {

if (process.env.MOON_ENV === "development" && charNext === undefined) {
lexError(`Lexer expected a character after "<".`, input, i);
break;
}

if (charNext === "/") {
Expand All @@ -115,6 +116,7 @@ export function lex(input) {

if (process.env.MOON_ENV === "development" && closeIndex === -1) {
lexError(`Lexer expected a closing ">" after "</".`, input, i);
break;
}

tokens.push({
Expand All @@ -134,6 +136,7 @@ export function lex(input) {

if (process.env.MOON_ENV === "development" && closeIndex === -1) {
lexError(`Lexer expected a closing "-->" after "<!--".`, input, i);
break;
}

i = closeIndex + 3;
Expand Down
1 change: 1 addition & 0 deletions packages/moon/src/compiler/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function ParseError(message, start, end, next) {
* @returns {string} Conditional error message
*/
function parseErrorMessage(message) {
/* istanbul ignore next */
return process.env.MOON_ENV === "development" ? message : "";
}

Expand Down
27 changes: 11 additions & 16 deletions packages/moon/test/compiler/lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,23 @@ test("expression token to string", () => {
expect(tokenString(lex(input)[0])).toBe(input);
});

test("lexer errors", () => {
process.env.MOON_ENV = "development";
test("lex error from unclosed opening bracket", () => {
console.error = jest.fn();

lex("<div><");
expect(Array.isArray(lex("<div><"))).toBe(true);
expect(console.error).toBeCalled();
});

lex("</input");
expect(console.error.mock.calls.length).toBe(2);

lex("<!-- never ending comment");
expect(console.error.mock.calls.length).toBe(3);

process.env.MOON_ENV = "production";
test("lex error from unclosed self-closing tag", () => {
console.error = jest.fn();

expect(() => { lex("<div><"); }).toThrow();
expect(console.error).not.toBeCalled();
expect(Array.isArray(lex("</input"))).toBe(true);
expect(console.error).toBeCalled();
});

expect(() => { lex("</input"); }).toThrow();
expect(console.error).not.toBeCalled();
test("lex error from unclosed comment", () => {
console.error = jest.fn();

expect(() => { lex("<!-- never ending comment"); }).toThrow();
expect(console.error).not.toBeCalled();
expect(Array.isArray(lex("<!-- endless comment"))).toBe(true);
expect(console.error).toBeCalled();
});
19 changes: 4 additions & 15 deletions packages/moon/test/compiler/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ test("parse nested elements", () => {
});
});

test("parse errors", () => {
process.env.MOON_ENV = "development";
test("parse error from invalid view", () => {
console.error = jest.fn();

expect(parseTest(`
Expand All @@ -99,21 +98,11 @@ test("parse errors", () => {
</div>
`).constructor.name).toBe("ParseError");
expect(console.error).toBeCalled();
});

expect(parseTest("").constructor.name).toBe("ParseError");
expect(console.error.mock.calls.length).toBe(2);

process.env.MOON_ENV = "production";
test("parse error from empty element", () => {
console.error = jest.fn();

expect(parseTest(`
<div>
<p>text?
</h1></input>
</div>
`).constructor.name).toBe("ParseError");
expect(console.error).not.toBeCalled();

expect(parseTest("").constructor.name).toBe("ParseError");
expect(console.error).not.toBeCalled();
expect(console.error).toBeCalled();
});
2 changes: 1 addition & 1 deletion packages/moon/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Moon from "../src/index";
});*/

test("root Moon constructor", () => {
const Root = new Moon({ view: "" });
const Root = new Moon({ view: "test" });
expect(Root.constructor.name).toBe("Function");
expect(new Root().name).toBe("Root");
});

0 comments on commit f29859f

Please sign in to comment.