clerk/internal/lsp/textdocument_references_test.go (view raw)
| 1 | package lsp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "go.lsp.dev/protocol" |
| 12 | "go.lsp.dev/uri" |
| 13 | |
| 14 | "olexsmir.xyz/clerk/internal/lsp/lsputil" |
| 15 | "olexsmir.xyz/clerk/internal/testutil/golden" |
| 16 | ) |
| 17 | |
| 18 | func TestServer_References_DocumentNotFound(t *testing.T) { |
| 19 | srv := NewServer("test") |
| 20 | res, err := srv.server.References(context.Background(), &protocol.ReferenceParams{ |
| 21 | TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///nonexistent.journal")}, |
| 22 | Position: protocol.Position{Line: 0, Character: 0}, |
| 23 | }) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if res != nil { |
| 28 | t.Errorf("got %v, want nil", res) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestGolden_References(t *testing.T) { |
| 33 | for _, tt := range []string{"references-journal", "references-include"} { |
| 34 | t.Run(tt, func(t *testing.T) { |
| 35 | ar := golden.Read(t, tt) |
| 36 | h := newTxtarHarness(t, ar) |
| 37 | |
| 38 | var b strings.Builder |
| 39 | for _, incl := range []bool{false, true} { |
| 40 | fmt.Fprintf(&b, "includeDeclaration:%v\n", incl) |
| 41 | for i := range h.cursors { |
| 42 | txtDocPos := h.textDocumentPosition(i) |
| 43 | res, err := h.srv.References(t.Context(), &protocol.ReferenceParams{ |
| 44 | Context: protocol.ReferenceContext{IncludeDeclaration: incl}, |
| 45 | TextDocumentPositionParams: txtDocPos, |
| 46 | }) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if len(res) == 0 { |
| 51 | fmt.Fprintf(&b, "%d:%d <none>\n", txtDocPos.Position.Line, txtDocPos.Position.Character) |
| 52 | continue |
| 53 | } |
| 54 | for _, loc := range res { |
| 55 | r := loc.Range |
| 56 | fmt.Fprintf(&b, "%d:%d %s %d:%d-%d:%d\n", txtDocPos.Position.Line, txtDocPos.Position.Character, |
| 57 | filepath.Base(loc.URI.Path()), |
| 58 | r.Start.Line, r.Start.Character, r.End.Line, r.End.Character) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | golden.Assert(t, ar, b.String()) |
| 63 | }) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func BenchmarkReferences(b *testing.B) { |
| 68 | content := openJournal(b, "../../journal/testdata/journals/actual-1ktxns-100accts.journal") |
| 69 | |
| 70 | srv := NewServer("test") |
| 71 | u := uri.URI("file:///test.journal") |
| 72 | srv.server.openDoc(u, content, 1, "journal") |
| 73 | srv.server.analysisFor(u) // warm per-doc cache |
| 74 | |
| 75 | for tname, tt := range map[string]int{ |
| 76 | "1k txns, account": strings.Index(content, "\n 1:2:3 ") + len("\n ") + 2, |
| 77 | "1k txns, payee": strings.Index(content, "transaction 1") + len("transaction "), |
| 78 | "1k txns, commodity": strings.Index(content, "2 B @@") + len("2 B"), |
| 79 | "late tx, payee": strings.Index(content, "transaction 1000") + len("transaction "), |
| 80 | } { |
| 81 | b.Run(tname, func(b *testing.B) { |
| 82 | line, col := lsputil.LineCol(content, tt) |
| 83 | params := &protocol.ReferenceParams{ |
| 84 | Context: protocol.ReferenceContext{IncludeDeclaration: true}, |
| 85 | TextDocument: protocol.TextDocumentIdentifier{URI: u}, |
| 86 | Position: protocol.Position{Line: uint32(line), Character: uint32(col)}, |
| 87 | } |
| 88 | |
| 89 | // warm up: first request resolves the symbol lazily; assert it found references |
| 90 | res, err := srv.server.References(b.Context(), params) |
| 91 | if err != nil { |
| 92 | b.Fatal(err) |
| 93 | } |
| 94 | if len(res) == 0 { |
| 95 | b.Fatalf("%s: no references", tname) |
| 96 | } |
| 97 | |
| 98 | b.ReportAllocs() |
| 99 | b.ResetTimer() |
| 100 | for b.Loop() { |
| 101 | if _, err := srv.server.References(b.Context(), params); err != nil { |
| 102 | b.Fatal(err) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // guard: a whole-file re-parse per request (~13ms) would blow past |
| 107 | // this and must be caught |
| 108 | if avg := b.Elapsed() / time.Duration(b.N); avg > 5*time.Millisecond { |
| 109 | b.Fatalf("references %v/op: reparse regression", avg) |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | } |