Skip to content

Commit

Permalink
chore: update test files for v3 code
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent e16dca3 commit e7904ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
28 changes: 15 additions & 13 deletions test/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global it, expect, describe */

import { parse } from "../src/index";
import { config } from "../src/config";
import { Eta } from "../src/index";
import { parse } from "../src/parse";

const eta = new Eta();

const fs = require("fs");
const path = require("path");
Expand All @@ -12,32 +14,32 @@ const complexTemplate = fs.readFileSync(filePath, "utf8");

describe("parse test", () => {
it("parses a simple template", () => {
const buff = parse("hi <%= hey %>", config);
const buff = parse.call(eta, "hi <%= hey %>");
expect(buff).toEqual(["hi ", { val: "hey", t: "i" }]);
});

it("parses a raw tag", () => {
const buff = parse("hi <%~ hey %>", config);
const buff = parse.call(eta, "hi <%~ hey %>");
expect(buff).toEqual(["hi ", { val: "hey", t: "r" }]);
});

it("works with whitespace trimming", () => {
const buff = parse("hi\n<%- = hey-%> <%_ = hi _%>", config);
const buff = parse.call(eta, "hi\n<%- = hey-%> <%_ = hi _%>");
expect(buff).toEqual(["hi", { val: "hey", t: "i" }, { val: "hi", t: "i" }]);
});

it("works with multiline comments", () => {
const buff = parse("hi <% /* comment contains delimiter %> */ %>", config);
const buff = parse.call(eta, "hi <% /* comment contains delimiter %> */ %>");
expect(buff).toEqual(["hi ", { val: "/* comment contains delimiter %> */", t: "e" }]);
});

it("parses with simple template literal", () => {
const buff = parse("hi <%= `template %> ${value}` %>", config);
const buff = parse.call(eta, "hi <%= `template %> ${value}` %>");
expect(buff).toEqual(["hi ", { val: "`template %> ${value}`", t: "i" }]);
});

it("compiles complex template", () => {
const buff = parse(complexTemplate, config);
const buff = parse.call(eta, complexTemplate);
expect(buff).toEqual([
"Hi\\n",
{ t: "e", val: 'console.log("Hope you like Eta!")' },
Expand Down Expand Up @@ -70,13 +72,13 @@ describe("parse test", () => {

test("throws with unclosed tag", () => {
expect(() => {
parse('<%hi("hey")', config);
parse.call(eta, '<%hi("hey")');
}).toThrowError("hi");
});

test("throws with unclosed single-quote string", () => {
expect(() => {
parse("<%= ' %>", config);
parse.call(eta, "<%= ' %>");
}).toThrowError(`unclosed string at line 1 col 5:
<%= ' %>
Expand All @@ -85,7 +87,7 @@ describe("parse test", () => {

test("throws with unclosed double-quote string", () => {
expect(() => {
parse('<%= " %>', config);
parse.call(eta, '<%= " %>');
}).toThrowError(`unclosed string at line 1 col 5:
<%= " %>
Expand All @@ -94,7 +96,7 @@ describe("parse test", () => {

test("throws with unclosed template literal", () => {
expect(() => {
parse("<%= ` %>", config);
parse.call(eta, "<%= ` %>");
}).toThrowError(`unclosed string at line 1 col 5:
<%= \` %>
Expand All @@ -103,7 +105,7 @@ describe("parse test", () => {

test("throws with unclosed multi-line comment", () => {
expect(() => {
parse("<%= /* %>", config);
parse.call(eta, "<%= /* %>");
}).toThrowError(`unclosed comment at line 1 col 5:
<%= /* %>
Expand Down
11 changes: 6 additions & 5 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global it, expect, describe */

import { trimWS, XMLEscape } from "../src/utils";
import { defaultConfig, getConfig } from "../src/index";
import { defaultConfig } from "../src/config";

describe("Whitespace trim", () => {
describe("#trimLeft", () => {
Expand Down Expand Up @@ -42,13 +42,13 @@ describe("Whitespace trim", () => {

describe("#trim", () => {
it("WS slurp both sides", () => {
expect(trimWS(" somestring ", getConfig({ autoTrim: ["slurp", "slurp"] }), "", "")).toBe(
"somestring"
);
expect(
trimWS(" somestring ", { ...defaultConfig, autoTrim: ["slurp", "slurp"] }, "", "")
).toBe("somestring");
});

it("defaultConfig.autoTrim set to false", () => {
expect(trimWS(" some string\n ", getConfig({ autoTrim: false }), "", "")).toBe(
expect(trimWS(" some string\n ", { ...defaultConfig, autoTrim: false }, "", "")).toBe(
" some string\n "
);
});
Expand All @@ -58,5 +58,6 @@ describe("Whitespace trim", () => {
describe("HTML Escape", () => {
it("properly escapes HTML characters", () => {
expect(XMLEscape("<p>HTML</p>")).toBe("&lt;p&gt;HTML&lt;/p&gt;");
expect(XMLEscape("no html here")).toBe("no html here");
});
});

0 comments on commit e7904ec

Please sign in to comment.