golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/failures_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 misc
     6  
     7  import (
     8  	"testing"
     9  
    10  	. "golang.org/x/tools/gopls/internal/test/integration"
    11  	"golang.org/x/tools/gopls/internal/test/compare"
    12  )
    13  
    14  // This is a slight variant of TestHoverOnError in definition_test.go
    15  // that includes a line directive, which makes no difference since
    16  // gopls ignores line directives.
    17  func TestHoverFailure(t *testing.T) {
    18  	const mod = `
    19  -- go.mod --
    20  module mod.com
    21  
    22  go 1.12
    23  -- a.y --
    24  DWIM(main)
    25  
    26  -- main.go --
    27  //line a.y:1
    28  package main
    29  
    30  func main() {
    31  	var err error
    32  	err.Error()
    33  }`
    34  	Run(t, mod, func(t *testing.T, env *Env) {
    35  		env.OpenFile("main.go")
    36  		content, _ := env.Hover(env.RegexpSearch("main.go", "Error"))
    37  		if content == nil {
    38  			t.Fatalf("Hover('Error') returned nil")
    39  		}
    40  		want := "```go\nfunc (error).Error() string\n```"
    41  		if content.Value != want {
    42  			t.Fatalf("wrong Hover('Error') content:\n%s", compare.Text(want, content.Value))
    43  		}
    44  	})
    45  }
    46  
    47  // This test demonstrates a case where gopls is not at all confused by
    48  // line directives, because it completely ignores them.
    49  func TestFailingDiagnosticClearingOnEdit(t *testing.T) {
    50  	// badPackageDup contains a duplicate definition of the 'a' const.
    51  	// This is a minor variant of TestDiagnosticClearingOnEdit from
    52  	// diagnostics_test.go, with a line directive, which makes no difference.
    53  	const badPackageDup = `
    54  -- go.mod --
    55  module mod.com
    56  
    57  go 1.12
    58  -- a.go --
    59  package consts
    60  
    61  const a = 1
    62  -- b.go --
    63  package consts
    64  //line gen.go:5
    65  const a = 2
    66  `
    67  
    68  	Run(t, badPackageDup, func(t *testing.T, env *Env) {
    69  		env.OpenFile("b.go")
    70  		env.AfterChange(
    71  			Diagnostics(env.AtRegexp("b.go", `a = 2`), WithMessage("a redeclared")),
    72  			Diagnostics(env.AtRegexp("a.go", `a = 1`), WithMessage("other declaration")),
    73  		)
    74  
    75  		// Fix the error by editing the const name in b.go to `b`.
    76  		env.RegexpReplace("b.go", "(a) = 2", "b")
    77  		env.AfterChange(
    78  			NoDiagnostics(ForFile("a.go")),
    79  			NoDiagnostics(ForFile("b.go")),
    80  		)
    81  	})
    82  }