all repos

clerk @ 23d0a0ddf451ff6ac76c6dd2ffda2cd7d4fe34fc

missing tooling for ledger/hledger

clerk/internal/lsp/textdocument_references_test.go (view raw)

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