41 lines
1.5 KiB
Go
41 lines
1.5 KiB
Go
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)
|
|
}
|
|
}
|