import { describe, test, expect, beforeAll } from "vitest"
import { Herb } from "@herb-tools/node-wasm"
import { Formatter } from "../src"
let formatter: Formatter
describe("Blank line normalization", () => {
beforeAll(async () => {
await Herb.load()
formatter = new Formatter(Herb, {
indentWidth: 2,
maxLineLength: 80
})
})
test("keeps a single blank line between elements", () => {
const source = `
`
expect(formatter.format(source)).toEqual(``)
})
test("keeps exactly two blank lines as an intentional wider break", () => {
const source = ``
expect(formatter.format(source)).toEqual(``)
})
test("collapses three or more blank lines down to one", () => {
const source = `\n
First
\n\n\n\n
Second
\n
`
expect(formatter.format(source)).toEqual(``)
})
test("collapses many blank lines down to one", () => {
const source = `\n
First
\n\n\n\n\n\n\n
Second
\n
`
expect(formatter.format(source)).toEqual(``)
})
test("keeps two blank lines between top-level elements", () => {
const source = `\n\n\n\n Content
\n`
expect(formatter.format(source)).toEqual(`\n\n\n\n Content
\n`)
})
test("collapses three blank lines between top-level elements", () => {
const source = `\n\n\n\n\n Content
\n`
expect(formatter.format(source)).toEqual(`\n\n\n Content
\n`)
})
test("keeps two blank lines inside ERB control-flow bodies", () => {
const source = `<% if condition %>\n First
\n\n\n Second
\n<% end %>`
expect(formatter.format(source)).toEqual(`<% if condition %>\n First
\n\n\n Second
\n<% end %>`)
})
test("collapses excess blank lines inside ERB control-flow bodies", () => {
const source = `<% if condition %>\n First
\n\n\n\n\n Second
\n<% end %>`
expect(formatter.format(source)).toEqual(`<% if condition %>\n First
\n\n Second
\n<% end %>`)
})
test("two-blank-line output is idempotent", () => {
const source = ``
const once = formatter.format(source)
const twice = formatter.format(once)
expect(twice).toEqual(once)
})
test("keeps a blank run after a comment that follows a text flow run", () => {
const source = `<% if a %>\n <%= x %> /\n <%# note %>\n\n\n <%= y %>\n<% end %>`
expect(formatter.format(source)).toEqual(`<% if a %>\n <%= x %> / <%# note %>\n\n\n <%= y %>\n<% end %>`)
})
test("a blank run after a comment that follows a text flow run is idempotent", () => {
const source = `<% if a %>\n <%= x %> /\n <%# note %>\n\n\n <%= y %>\n<% end %>`
const once = formatter.format(source)
const twice = formatter.format(once)
expect(twice).toEqual(once)
})
test("keeps a blank run after a text flow run that ends in an ERB output tag", () => {
const source = `\n Available on <%= provider %>\n\n\n <% if x %>\n y\n <% end %>\n
`
expect(formatter.format(source)).toEqual(source)
})
test("keeps a single blank line after a text flow run that ends in an ERB output tag", () => {
const source = `\n Available on <%= provider %>\n\n <% if x %>\n y\n <% end %>\n
`
expect(formatter.format(source)).toEqual(source)
})
test("a blank run after a text flow run that ends in an ERB output tag is idempotent", () => {
const source = `\n Available on <%= provider %>\n\n\n <% if x %>\n y\n <% end %>\n
`
const once = formatter.format(source)
const twice = formatter.format(once)
expect(twice).toEqual(once)
})
})