| 🛟 Updated. 28d5985 k33g 6h ago | 1 | package app |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // These tests cover what saving says to the language server. The defect they |
| 9 | // were written against: a window that started Untitled gains its path through |
| 10 | // Save As, afterSave sent only didSave, and a server ignores didSave for a |
| 11 | // document it was never told is open — so the window had no completion until |
| 12 | // the editor was restarted. |
| 13 | |
| 14 | func TestSavingAnUntitledWindowAnnouncesItsDocument(t *testing.T) { |
| 15 | project := t.TempDir() |
| 16 | t.Chdir(project) |
| 17 | a, _ := newTestApp(t) |
| 18 | client, server := newFakeLanguage(t, a) |
| 19 | connectLanguage(a, client) |
| 20 | a.Tick() // the start-up announcement runs, and finds nothing to tell |
| 21 | |
| 22 | a.NewFile() |
| 23 | typeText(a, "package main") |
| 24 | path := filepath.Join(project, "main.go") |
| 25 | a.save(a.ActiveView(), path) |
| 26 | |
| 27 | waitForMethod(t, server, "textDocument/didOpen") |
| 28 | if got := server.methodCount("textDocument/didSave"); got != 0 { |
| 29 | t.Errorf("didSave was sent %d times for a document the server was never told is open", got) |
| 30 | } |
| 31 | if !a.language.Knows(path) { |
| 32 | t.Errorf("after the save, the server still does not know %q", path) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestSaveAsUnderANewNameClosesTheOldDocument(t *testing.T) { |
| 37 | project := t.TempDir() |
| 38 | t.Chdir(project) |
| 39 | a, _ := newTestApp(t) |
| 40 | client, server := newFakeLanguage(t, a) |
| 41 | connectLanguage(a, client) |
| 42 | |
| 43 | writeTestFile(t, filepath.Join(project, "old.go"), "package main\n") |
| 44 | a.Open("old.go") |
| 45 | waitForMethod(t, server, "textDocument/didOpen") |
| 46 | |
| 47 | newPath := filepath.Join(project, "new.go") |
| 48 | a.save(a.ActiveView(), newPath) |
| 49 | |
| 50 | waitForMethod(t, server, "textDocument/didClose") |
| 51 | if a.language.Knows("old.go") { |
| 52 | t.Errorf("the old document is still open on the server after Save As gave the buffer a new name") |
| 53 | } |
| 54 | if !a.language.Knows(newPath) { |
| 55 | t.Errorf("the new document was never announced to the server") |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestSaveAsToTheSamePathClosesNothing(t *testing.T) { |
| 60 | // Save As offered the file's own name and the user accepted it: that is a |
| 61 | // write, not a rename, however the dialog spelt the path. |
| 62 | project := t.TempDir() |
| 63 | t.Chdir(project) |
| 64 | a, _ := newTestApp(t) |
| 65 | client, server := newFakeLanguage(t, a) |
| 66 | connectLanguage(a, client) |
| 67 | |
| 68 | writeTestFile(t, filepath.Join(project, "main.go"), "package main\n") |
| 69 | a.Open("main.go") // relative, as the command line gives it |
| 70 | waitForMethod(t, server, "textDocument/didOpen") |
| 71 | |
| 72 | a.save(a.ActiveView(), filepath.Join(project, "main.go")) // absolute, as the dialog gives it |
| 73 | |
| 74 | waitForMethod(t, server, "textDocument/didSave") |
| 75 | if got := server.methodCount("textDocument/didClose"); got != 0 { |
| 76 | t.Errorf("didClose was sent %d times for a save that renamed nothing", got) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestSavingAKnownDocumentReportsTheWrite(t *testing.T) { |
| 81 | project := t.TempDir() |
| 82 | t.Chdir(project) |
| 83 | a, _ := newTestApp(t) |
| 84 | client, server := newFakeLanguage(t, a) |
| 85 | connectLanguage(a, client) |
| 86 | |
| 87 | writeTestFile(t, filepath.Join(project, "main.go"), "package main\n") |
| 88 | a.Open("main.go") |
| 89 | waitForMethod(t, server, "textDocument/didOpen") |
| 90 | |
| 91 | a.save(a.ActiveView(), a.ActiveView().Buffer().Path()) |
| 92 | |
| 93 | waitForMethod(t, server, "textDocument/didSave") |
| 94 | if got := server.methodCount("textDocument/didOpen"); got != 1 { |
| 95 | t.Errorf("didOpen was sent %d times; saving an open document must not re-announce it", got) |
| 96 | } |
| 97 | } |