.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / internal / lsp / source / format_test.go
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package source
6
7 import (
8         "fmt"
9         "strings"
10         "testing"
11
12         "golang.org/x/tools/internal/lsp/diff"
13         "golang.org/x/tools/internal/lsp/diff/myers"
14 )
15
16 func TestImportPrefix(t *testing.T) {
17         for i, tt := range []struct {
18                 input, want string
19         }{
20                 {"package foo", "package foo"},
21                 {"package foo\n", "package foo\n"},
22                 {"package foo\n\nfunc f(){}\n", "package foo\n"},
23                 {"package foo\n\nimport \"fmt\"\n", "package foo\n\nimport \"fmt\""},
24                 {"package foo\nimport (\n\"fmt\"\n)\n", "package foo\nimport (\n\"fmt\"\n)"},
25                 {"\n\n\npackage foo\n", "\n\n\npackage foo\n"},
26                 {"// hi \n\npackage foo //xx\nfunc _(){}\n", "// hi \n\npackage foo //xx\n"},
27                 {"package foo //hi\n", "package foo //hi\n"},
28                 {"//hi\npackage foo\n//a\n\n//b\n", "//hi\npackage foo\n//a\n\n//b\n"},
29                 {
30                         "package a\n\nimport (\n  \"fmt\"\n)\n//hi\n",
31                         "package a\n\nimport (\n  \"fmt\"\n)\n//hi\n",
32                 },
33                 {`package a /*hi*/`, `package a /*hi*/`},
34                 {"package main\r\n\r\nimport \"go/types\"\r\n\r\n/*\r\n\r\n */\r\n", "package main\r\n\r\nimport \"go/types\"\r\n\r\n/*\r\n\r\n */\r\n"},
35                 {"package x; import \"os\"; func f() {}\n\n", "package x; import \"os\""},
36                 {"package x; func f() {fmt.Println()}\n\n", "package x"},
37         } {
38                 got := importPrefix([]byte(tt.input))
39                 if got != tt.want {
40                         t.Errorf("%d: failed for %q:\n%s", i, tt.input, diffStr(t, tt.want, got))
41                 }
42         }
43 }
44
45 func TestCRLFFile(t *testing.T) {
46         for i, tt := range []struct {
47                 input, want string
48         }{
49                 {
50                         input: `package main
51
52 /*
53 Hi description
54 */
55 func Hi() {
56 }
57 `,
58                         want: `package main
59
60 /*
61 Hi description
62 */`,
63                 },
64         } {
65                 got := importPrefix([]byte(strings.ReplaceAll(tt.input, "\n", "\r\n")))
66                 want := strings.ReplaceAll(tt.want, "\n", "\r\n")
67                 if got != want {
68                         t.Errorf("%d: failed for %q:\n%s", i, tt.input, diffStr(t, want, got))
69                 }
70         }
71 }
72
73 func diffStr(t *testing.T, want, got string) string {
74         if want == got {
75                 return ""
76         }
77         // Add newlines to avoid newline messages in diff.
78         want += "\n"
79         got += "\n"
80         d, err := myers.ComputeEdits("", want, got)
81         if err != nil {
82                 t.Fatal(err)
83         }
84         return fmt.Sprintf("%q", diff.ToUnified("want", "got", want, d))
85 }