import { Of } from "silentium"; import { describe, expect, test, vi } from "vitest"; import { Branch } from "../behaviors"; import { Record } from "../structures"; import { Template } from "./Template"; describe("Template.test", () => { test("regular usage", () => { const tpl = Template( Of("

$1

"), Record({ $1: Of("one value"), }), ); const g = vi.fn(); tpl.then(g); expect(g).toHaveBeenLastCalledWith("

one value

"); const tpl2 = Template( Of("

$1

"), Record({ $1: Of("second value"), }), ); const g2 = vi.fn(); tpl2.then(g2); expect(g2).toHaveBeenLastCalledWith("

second value

"); }); test("without messages", () => { const tpl = Template("

$1

", { $1: "one value", }); const g = vi.fn(); tpl.then(g); expect(g).toHaveBeenLastCalledWith("

one value

"); }); test("with template as function", () => { const tpl = Template((t) => `

${t.raw(Of("one value"))}

`); const g = vi.fn(); tpl.then(g); expect(g).toHaveBeenLastCalledWith("

one value

"); }); test("with template as function with many vars", () => { const tpl = Template( (t) => `

${t.raw(Of("one"))}, ${t.raw(Of("two"))}, ${t.raw(Of("three"))}, ${t.raw(Of("four"))}, ${t.raw(Of("five"))}, ${t.raw(Of("six"))}, ${t.raw(Of("seven"))}, ${t.raw(Of("eight"))}, ${t.raw(Of("nine"))}, ${t.raw(Of("ten"))}, ${t.raw(Of("eleven"))}, ${t.raw(Of("twelve"))}, ${t.raw(Of("thirteen"))}, ${t.raw(Of("fourteen"))}, ${t.raw(Of("fifteen"))}, ${t.raw(Of("seventeen"))}, ${t.raw(Of("eighteen"))}, ${t.raw(Of("nineteen"))}, ${t.raw(Of("twenty"))}, ${t.raw(Of("twenty one"))}, ${t.raw(Of("twenty two"))}, ${t.raw(Of("twenty three"))}, ${t.raw(Of("twenty four"))}, ${t.raw(Of("twenty five"))}, ${t.raw(Of("twenty six"))}, ${t.raw(Of("twenty seven"))}

`, ); const g = vi.fn(); tpl.then(g); expect(g).toHaveBeenLastCalledWith( "

one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, seventeen, eighteen, nineteen, twenty, twenty one, twenty two, twenty three, twenty four, twenty five, twenty six, twenty seven

", ); }); test("with places", () => { const t = Template(); t.template(`
Hello ${t.raw(Of("User"))}
`); const g = vi.fn(); t.then(g); expect(g).toHaveBeenLastCalledWith( '
Hello User
', ); }); test("number variable", () => { const t = Template(); t.template(`
Hello ${t.raw(Of(123))}
`); const g = vi.fn(); t.then(g); expect(g).toHaveBeenLastCalledWith('
Hello 123
'); }); test("number escaped variable", () => { const t = Template(); t.template(`
Hello ${t.escaped(Of(123))}
`); const g = vi.fn(); t.then(g); expect(g).toHaveBeenLastCalledWith('
Hello 123
'); }); test("empty string variable", () => { const t = Template(); t.template(`
Hello ${t.raw(Of(""))}
`); const g = vi.fn(); t.then(g); expect(g).toHaveBeenLastCalledWith('
Hello
'); }); test("Branched string variable", async () => { const t = Template( (t) => `
Hello ${t.raw(Branch(false, "true", ""))}
`, ); expect(await t).toBe('
Hello
'); }); test("escaped method with no special characters", () => { const tpl = Template((t) => `
${t.escaped(Of("Hello world"))}
`); const g = vi.fn(); tpl.then(g); expect(g).toHaveBeenLastCalledWith("
Hello world
"); }); test("escaped method with all escape characters", () => { const tpl = Template((t) => `
${t.escaped(Of("&<>\"'/"))}
`); const g = vi.fn(); tpl.then(g); expect(g).toHaveBeenLastCalledWith( `
&<>"'/
`, ); }); });