all repos

clerk @ c2e8832

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
perf: lsp: index transactions by date for the date hover..., 1 month ago
1
package lsp
2
3
import (
4
	"context"
5
	"fmt"
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
	"olexsmir.xyz/clerk/journal/ast"
16
	"olexsmir.xyz/clerk/journal/token"
17
)
18
19
func TestTagValueSpan(t *testing.T) {
20
	tag := func(start, end int) *ast.Tag {
21
		return &ast.Tag{
22
			Span: token.Span{Start: token.Pos{Offset: start}, End: token.Pos{Offset: end}},
23
		}
24
	}
25
26
	tests := map[string]struct {
27
		content            string
28
		tag                *ast.Tag
29
		wantStart, wantEnd int
30
	}{
31
		"value":                {"; project:home", tag(2, 14), 10, 14},
32
		"empty value":          {"; done:", tag(2, 7), 7, 7},
33
		"trailing space":       {"; done: ", tag(2, 8), 7, 7},
34
		"second of comma list": {"; a:1, b:2", tag(7, 10), 9, 10},
35
	}
36
	for tname, tt := range tests {
37
		t.Run(tname, func(t *testing.T) {
38
			span := tagValueSpan(tt.content, tt.tag)
39
			if span.Start.Offset != tt.wantStart || span.End.Offset != tt.wantEnd {
40
				t.Errorf("got %d-%d, want %d-%d", span.Start.Offset, span.End.Offset, tt.wantStart, tt.wantEnd)
41
			}
42
		})
43
	}
44
}
45
46
func TestServer_Hover_DocumentNotFound(t *testing.T) {
47
	srv := NewServer("test")
48
	res, err := srv.server.Hover(context.Background(), &protocol.HoverParams{
49
		TextDocumentPositionParams: protocol.TextDocumentPositionParams{
50
			TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///nonexistent.journal")},
51
			Position:     protocol.Position{Line: 0, Character: 0},
52
		},
53
	})
54
	if err != nil {
55
		t.Fatal(err)
56
	}
57
	if res != nil {
58
		t.Errorf("got %v, want nil", res)
59
	}
60
}
61
62
// Golden
63
64
func TestGolden_Hover(t *testing.T) {
65
	for _, tt := range []string{"hover-journal", "hover-include"} {
66
		ar := golden.Read(t, tt)
67
		t.Run(tt, func(t *testing.T) {
68
			h := newTxtarHarness(t, ar)
69
70
			var b strings.Builder
71
			for i := range h.cursors {
72
				tdp := h.textDocumentPosition(i)
73
				res, err := h.srv.Hover(t.Context(), &protocol.HoverParams{
74
					TextDocumentPositionParams: tdp,
75
				})
76
				if err != nil {
77
					t.Fatal(err)
78
				}
79
				if res == nil {
80
					fmt.Fprintf(&b, "%d:%d <none>\n", tdp.Position.Line, tdp.Position.Character)
81
					continue
82
				}
83
				mc, ok := res.Contents.(*protocol.MarkupContent)
84
				if !ok {
85
					t.Fatalf("Hover returned %T, want *MarkupContent", res.Contents)
86
				}
87
				fmt.Fprintf(&b, "%d:%d %q %d:%d-%d:%d\n",
88
					tdp.Position.Line, tdp.Position.Character, mc.Value,
89
					res.Range.Start.Line, res.Range.Start.Character, res.Range.End.Line, res.Range.End.Character)
90
			}
91
			golden.Assert(t, ar, b.String())
92
		})
93
	}
94
}
95
96
func BenchmarkHover(b *testing.B) {
97
	content := openJouranl(b, "../../journal/testdata/journals/actual-1ktxns-100accts.journal")
98
99
	srv := NewServer("test")
100
	srv.server.openDoc(uri.URI("file:///test.journal"), content, 1, "journal")
101
	srv.server.analysisFor(uri.URI("file:///test.journal")) // warm the per-doc cache
102
103
	for tname, tt := range map[string]int{
104
		"date":              strings.Index(content, "2000-01-01 transaction 1") + 3,
105
		"account":           strings.Index(content, "\n  1:2:3 ") + len("\n  ") + 2,
106
		"payee":             strings.Index(content, "transaction 1") + len("transaction ") + 1,
107
		"amount with cost":  strings.Index(content, "2 B @@") + 2,
108
		"commodity in cost": strings.Index(content, "@@ 2 C") + len("@@ 2 C") - 1,
109
		"late amount":       strings.Index(content, "1000 L @") + 2,
110
	} {
111
		b.Run(tname, func(b *testing.B) {
112
			line, col := lsputil.LineCol(content, tt)
113
			params := &protocol.HoverParams{
114
				TextDocumentPositionParams: protocol.TextDocumentPositionParams{
115
					TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///test.journal")},
116
					Position:     protocol.Position{Line: uint32(line), Character: uint32(col)},
117
				},
118
			}
119
120
			// warm up
121
			if _, err := srv.server.Hover(b.Context(), params); err != nil {
122
				b.Fatal(err)
123
			}
124
			b.ReportAllocs()
125
			b.ResetTimer()
126
			for b.Loop() {
127
				if _, err := srv.server.Hover(b.Context(), params); err != nil {
128
					b.Fatal(err)
129
				}
130
			}
131
132
			// guard: hover walks entries from the top of the file, so a cursor
133
			// near the end costs ~0.8ms on this file; a whole-file re-parse per
134
			// request (~13ms) would blow past this and must be caught
135
			if avg := b.Elapsed() / time.Duration(b.N); avg > 5*time.Millisecond {
136
				b.Fatalf("hover %v/op: entry walk or reparse regression", avg)
137
			}
138
		})
139
	}
140
}