golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/misc_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  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/tools/gopls/internal/hooks"
    12  	"golang.org/x/tools/gopls/internal/protocol"
    13  	"golang.org/x/tools/gopls/internal/test/integration"
    14  	. "golang.org/x/tools/gopls/internal/test/integration"
    15  	"golang.org/x/tools/gopls/internal/util/bug"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	bug.PanicOnBugs = true
    20  	integration.Main(m, hooks.Options)
    21  }
    22  
    23  // TestDocumentURIFix ensures that a DocumentURI supplied by the
    24  // client is subject to the "fixing" operation documented at
    25  // [protocol.DocumentURI.UnmarshalText]. The details of the fixing are
    26  // tested in the protocol package; here we aim to test only that it
    27  // occurs at all.
    28  func TestDocumentURIFix(t *testing.T) {
    29  	const mod = `
    30  -- go.mod --
    31  module testdata
    32  go 1.18
    33  
    34  -- a.go --
    35  package a
    36  
    37  const K = 1
    38  `
    39  	Run(t, mod, func(t *testing.T, env *Env) {
    40  		env.OpenFile("a.go")
    41  		loc := env.RegexpSearch("a.go", "K")
    42  		path := strings.TrimPrefix(string(loc.URI), "file://") // (absolute)
    43  
    44  		check := func() {
    45  			t.Helper()
    46  			t.Logf("URI = %s", loc.URI)
    47  			content, _ := env.Hover(loc) // must succeed
    48  			if content == nil || !strings.Contains(content.Value, "const K") {
    49  				t.Errorf("wrong content: %#v", content)
    50  			}
    51  		}
    52  
    53  		// Regular URI (e.g. file://$TMPDIR/TestDocumentURIFix/default/work/a.go)
    54  		check()
    55  
    56  		// URL-encoded path (e.g. contains %2F instead of last /)
    57  		loc.URI = protocol.DocumentURI("file://" + strings.Replace(path, "/a.go", "%2Fa.go", 1))
    58  		check()
    59  
    60  		// We intentionally do not test further cases (e.g.
    61  		// file:// without a third slash) as it would quickly
    62  		// get bogged down in irrelevant details of the
    63  		// fake editor's own handling of URIs.
    64  	})
    65  }