import { describe, test, expect, beforeAll } from "vitest" import { Herb } from "@herb-tools/node-wasm" import { Formatter } from "../src" import { BYTE_ORDER_MARK as BOM } from "@herb-tools/core" let formatter: Formatter describe("byte order mark", () => { beforeAll(async () => { await Herb.load() formatter = new Formatter(Herb, { indentWidth: 2, maxLineLength: 80 }) }) test("removes a leading byte order mark", () => { expect(formatter.format(`${BOM}
hello
`)).toEqual("
hello
") }) test("removes a leading byte order mark ahead of a doctype", () => { expect(formatter.format(`${BOM}\n`)).toEqual(formatter.format("\n")) expect(formatter.format(`${BOM}\n`).startsWith(" { expect(formatter.format(`${BOM}
\nhello\n
`)).toEqual(formatter.format(`
\nhello\n
`)) }) test("removes a byte order mark from a document that is otherwise empty", () => { expect(formatter.format(BOM)).toEqual("") }) test("leaves a document without a byte order mark untouched", () => { expect(formatter.format("
hello
")).toEqual("
hello
") }) test("keeps a byte order mark that isn't at the start of the document", () => { expect(formatter.format(`
a${BOM}b
`)).toEqual(`
a${BOM}b
`) }) test("keeps a byte order mark inside an attribute value", () => { expect(formatter.format(`
hello
`)).toEqual(`
hello
`) }) test("keeps a byte order mark inside an ERB String literal", () => { expect(formatter.format(`
<%= "a${BOM}b" %>
`)).toEqual(`
<%= "a${BOM}b" %>
`) }) test("keeps the byte order mark when the document is skipped for parse errors", () => { const source = `${BOM}
unclosed` const result = formatter.formatWithResult(source) expect(result.skipped).toEqual("parse-errors") expect(result.output).toEqual(source) }) test("keeps the byte order mark when the document is skipped for an ignore directive", () => { const source = `${BOM}<%# herb:formatter ignore %>\n
hello
\n` const result = formatter.formatWithResult(source) expect(result.skipped).toEqual("ignore-directive") expect(result.output).toEqual(source) }) })