all repos

clerk @ 892180582dccc852236879a1acc412c90cac01ad

missing tooling for ledger/hledger

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

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