github.com/v2fly/tools@v0.100.0/internal/lsp/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 12 func TestContentPosition(t *testing.T) { 13 content := "foo\nš\nbar" 14 tests := []struct { 15 offset, wantLine, wantColumn int 16 }{ 17 {0, 0, 0}, 18 {3, 0, 3}, 19 {4, 1, 0}, 20 {5, 1, 1}, 21 {6, 2, 0}, 22 } 23 for _, test := range tests { 24 pos, err := contentPosition(content, test.offset) 25 if err != nil { 26 t.Fatal(err) 27 } 28 if pos.Line != test.wantLine { 29 t.Errorf("contentPosition(%q, %d): Line = %d, want %d", content, test.offset, pos.Line, test.wantLine) 30 } 31 if pos.Column != test.wantColumn { 32 t.Errorf("contentPosition(%q, %d): Column = %d, want %d", content, test.offset, pos.Column, test.wantColumn) 33 } 34 } 35 } 36 37 const exampleProgram = ` 38 -- go.mod -- 39 go 1.12 40 -- main.go -- 41 package main 42 43 import "fmt" 44 45 func main() { 46 fmt.Println("Hello World.") 47 } 48 ` 49 50 func TestClientEditing(t *testing.T) { 51 ws, err := NewSandbox(&SandboxConfig{Files: exampleProgram}) 52 if err != nil { 53 t.Fatal(err) 54 } 55 defer ws.Close() 56 ctx := context.Background() 57 editor := NewEditor(ws, EditorConfig{}) 58 if err := editor.OpenFile(ctx, "main.go"); err != nil { 59 t.Fatal(err) 60 } 61 if err := editor.EditBuffer(ctx, "main.go", []Edit{ 62 { 63 Start: Pos{5, 14}, 64 End: Pos{5, 26}, 65 Text: "Hola, mundo.", 66 }, 67 }); err != nil { 68 t.Fatal(err) 69 } 70 got := editor.buffers["main.go"].text() 71 want := `package main 72 73 import "fmt" 74 75 func main() { 76 fmt.Println("Hola, mundo.") 77 } 78 ` 79 if got != want { 80 t.Errorf("got text %q, want %q", got, want) 81 } 82 }