This commit is contained in:
parent
09fceca2f9
commit
40e5379eb3
11 changed files with 250 additions and 2 deletions
45
cmds/golines/format_test.go
Normal file
45
cmds/golines/format_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue