feat(format): Doc-IR core, line_width (120) and where_wrap when_long

This commit is contained in:
Hein
2026-10-06 14:44:45 +02:00
parent 4ff729eeeb
commit 553609e988
7 changed files with 257 additions and 2 deletions
+40
View File
@@ -0,0 +1,40 @@
package format
import "testing"
func TestDocGroupFlatWhenFits(t *testing.T) {
d := Group(Concat(Text("WHERE"), Indent(Concat(Line(), Text("a = 1"), Line(), Text("AND b = 2")))))
if got, want := Render(d, 80, " ", "\n"), "WHERE a = 1 AND b = 2"; got != want {
t.Errorf("flat: got %q want %q", got, want)
}
if got, want := Render(d, 10, " ", "\n"), "WHERE\n a = 1\n AND b = 2"; got != want {
t.Errorf("broken: got %q want %q", got, want)
}
if got, want := Render(d, 0, " ", "\n"), "WHERE a = 1 AND b = 2"; got != want {
t.Errorf("unlimited: got %q want %q", got, want)
}
}
func TestDocNewlineInTextForcesBreak(t *testing.T) {
d := Group(Concat(Text("x"), Line(), Text("(\n y)")))
if got, want := Render(d, 80, " ", "\n"), "x\n(\n y)"; got != want {
t.Errorf("got %q want %q", got, want)
}
}
func TestDocIfBreakAndSoftLine(t *testing.T) {
d := Group(Concat(Text("f("), Indent(Concat(SoftLine(), Text("a, b"))), SoftLine(), IfBreak(Text(" -- long"), Text(""))))
if got, want := Render(d, 80, " ", "\n"), "f(a, b"; got != want {
t.Errorf("flat: got %q want %q", got, want)
}
if got, want := Render(d, 3, " ", "\n"), "f(\n a, b\n -- long"; got != want {
t.Errorf("broken: got %q want %q", got, want)
}
}
func TestDocTrailingTextCountsTowardFit(t *testing.T) {
d := Concat(Group(Concat(Text("aaa"), Line(), Text("bbb"))), Text(";;;;"))
if got, want := Render(d, 10, " ", "\n"), "aaa\nbbb;;;;"; got != want {
t.Errorf("got %q want %q", got, want)
}
}