all repos

clerk @ 3d2b11f588919a9b0b09013478b96b54b4e43c67

missing tooling for ledger/hledger

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

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