Skip to content

Commit 4dccda4

Browse files
authored
Formatter: Add expectFormattedToMatch test helper (#1215)
This pull request introduces a `expectFormattedToMatch` test helper to reduce boilerplate in formatter tests that assert idempotent formatting. This three-line pattern: ```js const source = dedent`...` const result = formatter.format(source) expect(result).toEqual(source) ``` becomes: ```js expectFormattedToMatch(dedent`...`) ```
1 parent ab93987 commit 4dccda4

14 files changed

+272
-602
lines changed

javascript/packages/formatter/test/document-formatting.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { describe, test, expect, beforeAll } from "vitest"
22
import { Herb } from "@herb-tools/node-wasm"
33
import { Formatter } from "../src"
4+
import { createExpectFormattedToMatch } from "./helpers"
45

56
import dedent from "dedent"
67

78
let formatter: Formatter
9+
let expectFormattedToMatch: ReturnType<typeof createExpectFormattedToMatch>
810

911
describe("Document-level formatting", () => {
1012
beforeAll(async () => {
@@ -14,6 +16,7 @@ describe("Document-level formatting", () => {
1416
indentWidth: 2,
1517
maxLineLength: 80
1618
})
19+
expectFormattedToMatch = createExpectFormattedToMatch(formatter)
1720
})
1821

1922
test("preserves newline between ERB assignment and HTML element", () => {
@@ -481,16 +484,13 @@ describe("Document-level formatting", () => {
481484
})
482485

483486
test("preserves inline opening tag for block elements with few attributes", () => {
484-
const source = dedent`
487+
expectFormattedToMatch(dedent`
485488
<div class="flex flex-col">
486489
<h3 class="line-clamp-1">
487490
<pre>Content</pre>
488491
</h3>
489492
</div>
490-
`
491-
492-
const result = formatter.format(source)
493-
expect(result).toEqual(source)
493+
`)
494494
})
495495

496496
test("split ERB tag if it doesn't fit on current line", () => {

javascript/packages/formatter/test/erb-formatter/erb-formatter-additional.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { describe, test, expect, beforeAll } from "vitest"
22
import { Herb } from "@herb-tools/node-wasm"
33
import { Formatter } from "../../src"
4+
import { createExpectFormattedToMatch } from "../helpers"
45
import dedent from "dedent"
56

67
let formatter: Formatter
8+
let expectFormattedToMatch: ReturnType<typeof createExpectFormattedToMatch>
79

810
describe("ERB Formatter Additional Tests", () => {
911
beforeAll(async () => {
@@ -13,6 +15,7 @@ describe("ERB Formatter Additional Tests", () => {
1315
indentWidth: 2,
1416
maxLineLength: 80,
1517
})
18+
expectFormattedToMatch = createExpectFormattedToMatch(formatter)
1619
})
1720

1821
describe("Basic formatting edge cases", () => {
@@ -174,17 +177,13 @@ describe("ERB Formatter Additional Tests", () => {
174177

175178
describe("Error handling", () => {
176179
test("handles unmatched ERB tags gracefully", () => {
177-
const source = dedent`
180+
expectFormattedToMatch(dedent`
178181
<% if true %>
179182
<h1>
180183
<%= link_to 'New Order', new_order_path, class: "btn btn-success" %>
181184
<% end %>
182185
</h1>
183-
`
184-
185-
const result = formatter.format(source)
186-
187-
expect(result).toBe(source)
186+
`)
188187
})
189188
})
190189

javascript/packages/formatter/test/erb/comment.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { describe, test, expect, beforeAll } from "vitest"
22
import { Herb } from "@herb-tools/node-wasm"
33
import { Formatter } from "../../src"
4+
import { createExpectFormattedToMatch } from "../helpers"
45

56
import dedent from "dedent"
67

78
let formatter: Formatter
9+
let expectFormattedToMatch: ReturnType<typeof createExpectFormattedToMatch>
810

911
describe("@herb-tools/formatter", () => {
1012
beforeAll(async () => {
@@ -14,6 +16,8 @@ describe("@herb-tools/formatter", () => {
1416
indentWidth: 2,
1517
maxLineLength: 80
1618
})
19+
20+
expectFormattedToMatch = createExpectFormattedToMatch(formatter)
1721
})
1822

1923
test("formats ERB comments", () => {
@@ -308,10 +312,7 @@ describe("@herb-tools/formatter", () => {
308312
})
309313

310314
test("handles long ERB comments that exceed maxLineLength", () => {
311-
const source = '<%# herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp %>'
312-
313-
const result = formatter.format(source)
314-
expect(result).toEqual(source)
315+
expectFormattedToMatch('<%# herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp herb lsp %>')
315316
})
316317

317318
test("handles various long ERB comment lengths", () => {

javascript/packages/formatter/test/erb/erb.test.ts

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { describe, test, expect, beforeAll } from "vitest"
22
import { Herb } from "@herb-tools/node-wasm"
33
import { Formatter } from "../../src"
4+
import { createExpectFormattedToMatch } from "../helpers"
45

56
import dedent from "dedent"
67

78
let formatter: Formatter
9+
let expectFormattedToMatch: ReturnType<typeof createExpectFormattedToMatch>
810

911
describe("@herb-tools/formatter", () => {
1012
beforeAll(async () => {
@@ -14,6 +16,8 @@ describe("@herb-tools/formatter", () => {
1416
indentWidth: 2,
1517
maxLineLength: 80,
1618
})
19+
20+
expectFormattedToMatch = createExpectFormattedToMatch(formatter)
1721
})
1822

1923
test("formats simple HTML with ERB content", () => {
@@ -31,12 +35,10 @@ describe("@herb-tools/formatter", () => {
3135
})
3236

3337
test("ERB output tags on two lines on top-level", () => {
34-
const source = dedent`
38+
expectFormattedToMatch(dedent`
3539
<%= title %>
3640
<%= title %>
37-
`
38-
const result = formatter.format(source)
39-
expect(result).toEqual(source)
41+
`)
4042
})
4143

4244
test("adjecent ERB output tags without space on top-level", () => {
@@ -102,16 +104,13 @@ describe("@herb-tools/formatter", () => {
102104
})
103105

104106
test("should not add extra % to ERB closing tags with quoted strings", () => {
105-
const input = dedent`
107+
expectFormattedToMatch(dedent`
106108
<div>
107109
<%= link_to "Nederlands", url_for(locale: 'nl'), class: "px-4 py-2 hover:bg-slate-100 rounded block" %>
108110
<%= link_to "Français", url_for(locale: 'fr'), class: "px-4 py-2 hover:bg-slate-100 rounded block" %>
109111
<%= link_to "English", url_for(locale: 'en'), class: "px-4 py-2 hover:bg-slate-100 rounded block" %>
110112
</div>
111-
`
112-
113-
const result = formatter.format(input)
114-
expect(result).toBe(input)
113+
`)
115114
})
116115

117116
test("should handle complex ERB in layout files", () => {
@@ -897,7 +896,7 @@ describe("@herb-tools/formatter", () => {
897896
})
898897

899898
test("https://github.com/marcoroth/herb/issues/436#issuecomment-3219820557 Example 3", () => {
900-
const input = dedent`
899+
expectFormattedToMatch(dedent`
901900
<style>
902901
[data-test-page-header] {
903902
margin-bottom: 0px;
@@ -922,10 +921,7 @@ describe("@herb-tools/formatter", () => {
922921
</script>
923922
924923
<div class="pt-3" id="chat"></div>
925-
`
926-
927-
const result = formatter.format(input)
928-
expect(result).toBe(input)
924+
`)
929925
})
930926

931927
test("https://github.com/marcoroth/herb/issues/436#issuecomment-3219820557 Example 4", () => {
@@ -1358,7 +1354,7 @@ describe("@herb-tools/formatter", () => {
13581354
})
13591355

13601356
test("https://github.com/marcoroth/herb/issues/436#issuecomment-3356738094", () => {
1361-
const input = dedent`
1357+
expectFormattedToMatch(dedent`
13621358
<% if cover.present? %>
13631359
<figure class="figure">
13641360
<%= image_tag attachment_url(cover), size: "460x249", class: "img-fluid figure-img" %>
@@ -1376,10 +1372,7 @@ describe("@herb-tools/formatter", () => {
13761372
</figcaption>
13771373
</figure>
13781374
<% end %>
1379-
`
1380-
1381-
const result = formatter.format(input)
1382-
expect(result).toBe(input)
1375+
`)
13831376
})
13841377

13851378
test("adjecent ERB text within elements", () => {
@@ -1579,97 +1572,65 @@ describe("@herb-tools/formatter", () => {
15791572
})
15801573

15811574
test("ERB output with heredoc (issue 476)", () => {
1582-
const input = dedent`
1575+
expectFormattedToMatch(dedent`
15831576
<%= <<EXAMPLE
15841577
example
15851578
EXAMPLE
15861579
%>
1587-
`
1588-
1589-
const result = formatter.format(input)
1590-
1591-
expect(result).toBe(input)
1580+
`)
15921581
})
15931582

15941583
test("ERB output with squiggly heredoc", () => {
1595-
const input = dedent`
1584+
expectFormattedToMatch(dedent`
15961585
<%= <<~HEREDOC
15971586
example
15981587
HEREDOC
15991588
%>
1600-
`
1601-
1602-
const result = formatter.format(input)
1603-
1604-
expect(result).toBe(input)
1589+
`)
16051590
})
16061591

16071592
test("ERB output with hyphen heredoc", () => {
1608-
const input = dedent`
1593+
expectFormattedToMatch(dedent`
16091594
<%= <<-HEREDOC
16101595
example
16111596
HEREDOC
16121597
%>
1613-
`
1614-
1615-
const result = formatter.format(input)
1616-
1617-
expect(result).toBe(input)
1598+
`)
16181599
})
16191600

16201601
test("ERB output with single-quoted heredoc", () => {
1621-
const input = dedent`
1602+
expectFormattedToMatch(dedent`
16221603
<%= <<'HEREDOC'
16231604
no #{interpolation}
16241605
HEREDOC
16251606
%>
1626-
`
1627-
1628-
const result = formatter.format(input)
1629-
1630-
expect(result).toBe(input)
1607+
`)
16311608
})
16321609

16331610
test("ERB output with double-quoted heredoc", () => {
1634-
const input = dedent`
1611+
expectFormattedToMatch(dedent`
16351612
<%= <<"HEREDOC"
16361613
with #{interpolation}
16371614
HEREDOC
16381615
%>
1639-
`
1640-
1641-
const result = formatter.format(input)
1642-
1643-
expect(result).toBe(input)
1616+
`)
16441617
})
16451618

16461619
test("ERB output with heredoc and method chaining", () => {
1647-
const input = dedent`
1620+
expectFormattedToMatch(dedent`
16481621
<%= <<HEREDOC.strip
16491622
example
16501623
HEREDOC
16511624
%>
1652-
`
1653-
1654-
const result = formatter.format(input)
1655-
1656-
expect(result).toBe(input)
1625+
`)
16571626
})
16581627

16591628
test("ERB output with bitshift operator", () => {
1660-
const input = `<%= 1 << 4 %>`
1661-
1662-
const result = formatter.format(input)
1663-
1664-
expect(result).toBe(input)
1629+
expectFormattedToMatch(`<%= 1 << 4 %>`)
16651630
})
16661631

16671632
test("ERB output with append operator", () => {
1668-
const input = `<%= array << item %>`
1669-
1670-
const result = formatter.format(input)
1671-
1672-
expect(result).toBe(input)
1633+
expectFormattedToMatch(`<%= array << item %>`)
16731634
})
16741635

16751636
// The heredoc is not detected because it doesn't start with "<<".

0 commit comments

Comments
 (0)