1
0
mirror of https://github.com/golang/go synced 2024-11-06 01:46:12 -07:00
go/internal/lsp/source/format_test.go
Peter Weinbergr 9048b464a0 internal/lsp: avoid panic caused by assuming file ends with newline
importPrefix in format.go computes the end of the package statement
including a trailing newline. This causes a panic if the user has
not typed the newline. The code now checks for that case.

Fixes golang/go#40208

Change-Id: I4c5118a5de78027e6c5e6ed91dfddca81e38f7e9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242638
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-14 19:07:37 +00:00

32 lines
939 B
Go

package source
import (
"testing"
)
type data struct {
input, want string
}
func TestImportPrefix(t *testing.T) {
var tdata = []data{
{"package foo", "package foo"},
{"package foo\n", "package foo\n"},
{"package foo\n\nfunc f(){}\n", "package foo\n"},
{"package foo\n\nimport \"fmt\"\n", "package foo\n\nimport \"fmt\""},
{"package foo\nimport (\n\"fmt\"\n)\n", "package foo\nimport (\n\"fmt\"\n)"},
{"\n\n\npackage foo\n", "\n\n\npackage foo\n"},
{"// hi \n\npackage foo //xx\nfunc _(){}\n", "// hi \n\npackage foo //xx\n"},
{"package foo //hi\n", "package foo //hi\n"},
{"//hi\npackage foo\n//a\n\n//b\n", "//hi\npackage foo\n//a\n\n//b\n"},
{"package a\n\nimport (\n \"fmt\"\n)\n//hi\n",
"package a\n\nimport (\n \"fmt\"\n)\n//hi\n"},
}
for i, x := range tdata {
got := importPrefix([]byte(x.input))
if got != x.want {
t.Errorf("%d: got\n%q, wanted\n%q for %q", i, got, x.want, x.input)
}
}
}