import dedent from "dedent"
import { describe, test, expect, beforeAll } from "vitest"
import { Herb } from "@herb-tools/node-wasm"
import { IndentPrinter } from "../src/index.js"
describe("IndentPrinter", () => {
beforeAll(async () => {
await Herb.load()
})
function printIndented(input: string, indentWidth: number = 2): string {
const parseResult = Herb.parse(input, { track_whitespace: true })
expect(parseResult.value).toBeTruthy()
const printer = new IndentPrinter(indentWidth)
return printer.print(parseResult.value!)
}
describe("Basic functionality", () => {
test("is defined", () => {
expect(IndentPrinter).toBeDefined()
})
test("can be instantiated", () => {
const printer = new IndentPrinter()
expect(printer).toBeInstanceOf(IndentPrinter)
})
test("static print method works", () => {
const input = "
Hello World
"
const parseResult = Herb.parse(input, { track_whitespace: true })
expect(parseResult.value).toBeTruthy()
const output = IndentPrinter.print(parseResult.value!)
expect(output).toBe(input)
})
})
describe("Flat elements", () => {
test("flat element prints unchanged", () => {
const input = "Hello
"
expect(printIndented(input)).toBe("Hello
")
})
test("self-closing element prints unchanged", () => {
const input = "
"
expect(printIndented(input)).toBe("
")
})
})
describe("Nested elements", () => {
test("nested elements get re-indented based on depth", () => {
const input = dedent`
`
const expected = dedent`
`
expect(printIndented(input)).toBe(expected)
})
test("deeply nested elements", () => {
const input = dedent`
`
const expected = dedent`
`
expect(printIndented(input)).toBe(expected)
})
})
describe("indentWidth option", () => {
test("indentWidth of 4 spaces", () => {
const input = dedent`
`
const expected = dedent`
`
expect(printIndented(input, 4)).toBe(expected)
})
})
describe("Whitespace-only lines", () => {
test("whitespace-only lines become empty", () => {
const input = ""
const expected = dedent`
`
expect(printIndented(input)).toBe(expected)
})
})
describe("Preserves attributes and tag structure", () => {
test("preserves attributes", () => {
const input = dedent`
`
const expected = dedent`
`
expect(printIndented(input)).toBe(expected)
})
})
describe("ERB block structures", () => {
test("ERB if/else indents each branch", () => {
const input = dedent`
<% if true %>
Yes
<% else %>
No
<% end %>
`
const expected = dedent`
<% if true %>
Yes
<% else %>
No
<% end %>
`
expect(printIndented(input)).toBe(expected)
})
test("ERB each block indents body", () => {
const input = dedent`
<% items.each do |item| %>
<%= item %>
<% end %>
`
const expected = dedent`
<% items.each do |item| %>
<%= item %>
<% end %>
`
expect(printIndented(input)).toBe(expected)
})
test("ERB unless indents body", () => {
const input = dedent`
<% unless false %>
Hello
<% end %>
`
const expected = dedent`
<% unless false %>
Hello
<% end %>
`
expect(printIndented(input)).toBe(expected)
})
})
describe("Mixed HTML + ERB", () => {
test("HTML with ERB content inside", () => {
const input = dedent`
<% if true %>
Hello
<% end %>
`
const expected = dedent`
<% if true %>
Hello
<% end %>
`
expect(printIndented(input)).toBe(expected)
})
test("ERB expression in element", () => {
const input = dedent`
<%= @name %>
`
const expected = dedent`
<%= @name %>
`
expect(printIndented(input)).toBe(expected)
})
})
})