webtools/cmds/golines/format_test.go

46 lines
1.1 KiB
Go
Raw Normal View History

2026-09-06 09:23:55 +10:00
package main
import (
"go/parser"
"go/token"
"strings"
"testing"
)
func TestFormatWidths(t *testing.T) {
source := `package main
func main() {
println("first argument", "second argument", "third argument", "fourth argument")
}
`
var narrow, wide string
for width := 40; width <= 180; width += 5 {
output, err := formatSource(source, width)
if err != nil {
t.Fatalf("width %d: %v", width, err)
}
if _, err := parser.ParseFile(token.NewFileSet(), "output.go", output, parser.AllErrors); err != nil {
t.Fatalf("width %d produced invalid Go: %v", width, err)
}
if width == 40 {
narrow = string(output)
}
if width == 180 {
wide = string(output)
}
}
if strings.Count(narrow, "\n") <= strings.Count(wide, "\n") {
t.Fatal("narrow width did not split the call")
}
}
func TestInvalidSource(t *testing.T) {
if _, err := formatSource("package main\nfunc {", 100); err == nil {
t.Fatal("expected a syntax error")
}
// A failed edit must not prevent subsequent formatting.
if _, err := formatSource("package main\nfunc main() {}", 100); err != nil {
t.Fatal(err)
}
}