golang.org/x/tools/gopls@v0.15.3/internal/test/integration/fake/editor_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 fake
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"golang.org/x/tools/gopls/internal/protocol"
    12  )
    13  
    14  const exampleProgram = `
    15  -- go.mod --
    16  go 1.12
    17  -- main.go --
    18  package main
    19  
    20  import "fmt"
    21  
    22  func main() {
    23  	fmt.Println("Hello World.")
    24  }
    25  `
    26  
    27  func TestClientEditing(t *testing.T) {
    28  	ws, err := NewSandbox(&SandboxConfig{Files: UnpackTxt(exampleProgram)})
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	defer ws.Close()
    33  	ctx := context.Background()
    34  	editor := NewEditor(ws, EditorConfig{})
    35  	if err := editor.OpenFile(ctx, "main.go"); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if err := editor.EditBuffer(ctx, "main.go", []protocol.TextEdit{
    39  		{
    40  			Range: protocol.Range{
    41  				Start: protocol.Position{Line: 5, Character: 14},
    42  				End:   protocol.Position{Line: 5, Character: 26},
    43  			},
    44  			NewText: "Hola, mundo.",
    45  		},
    46  	}); err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	got := editor.buffers["main.go"].text()
    50  	want := `package main
    51  
    52  import "fmt"
    53  
    54  func main() {
    55  	fmt.Println("Hola, mundo.")
    56  }
    57  `
    58  	if got != want {
    59  		t.Errorf("got text %q, want %q", got, want)
    60  	}
    61  }