golang.org/x/tools/gopls@v0.15.3/internal/golang/format_test.go (about)

     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 golang
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/tools/gopls/internal/test/compare"
    12  )
    13  
    14  func TestImportPrefix(t *testing.T) {
    15  	for i, tt := range []struct {
    16  		input, want string
    17  	}{
    18  		{"package foo", "package foo"},
    19  		{"package foo\n", "package foo\n"},
    20  		{"package foo\n\nfunc f(){}\n", "package foo\n"},
    21  		{"package foo\n\nimport \"fmt\"\n", "package foo\n\nimport \"fmt\""},
    22  		{"package foo\nimport (\n\"fmt\"\n)\n", "package foo\nimport (\n\"fmt\"\n)"},
    23  		{"\n\n\npackage foo\n", "\n\n\npackage foo\n"},
    24  		{"// hi \n\npackage foo //xx\nfunc _(){}\n", "// hi \n\npackage foo //xx\n"},
    25  		{"package foo //hi\n", "package foo //hi\n"},
    26  		{"//hi\npackage foo\n//a\n\n//b\n", "//hi\npackage foo\n//a\n\n//b\n"},
    27  		{
    28  			"package a\n\nimport (\n  \"fmt\"\n)\n//hi\n",
    29  			"package a\n\nimport (\n  \"fmt\"\n)\n//hi\n",
    30  		},
    31  		{`package a /*hi*/`, `package a /*hi*/`},
    32  		{"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"},
    33  		{"package x; import \"os\"; func f() {}\n\n", "package x; import \"os\""},
    34  		{"package x; func f() {fmt.Println()}\n\n", "package x"},
    35  	} {
    36  		got, err := importPrefix([]byte(tt.input))
    37  		if err != nil {
    38  			t.Fatal(err)
    39  		}
    40  		if d := compare.Text(tt.want, got); d != "" {
    41  			t.Errorf("%d: failed for %q:\n%s", i, tt.input, d)
    42  		}
    43  	}
    44  }
    45  
    46  func TestCRLFFile(t *testing.T) {
    47  	for i, tt := range []struct {
    48  		input, want string
    49  	}{
    50  		{
    51  			input: `package main
    52  
    53  /*
    54  Hi description
    55  */
    56  func Hi() {
    57  }
    58  `,
    59  			want: `package main
    60  
    61  /*
    62  Hi description
    63  */`,
    64  		},
    65  	} {
    66  		got, err := importPrefix([]byte(strings.ReplaceAll(tt.input, "\n", "\r\n")))
    67  		if err != nil {
    68  			t.Fatal(err)
    69  		}
    70  		want := strings.ReplaceAll(tt.want, "\n", "\r\n")
    71  		if d := compare.Text(want, got); d != "" {
    72  			t.Errorf("%d: failed for %q:\n%s", i, tt.input, d)
    73  		}
    74  	}
    75  }