all repos

clerk @ 68a2727ad67e44cd018ee1b44fbdb1940ca1e372

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
feat: config (#6), 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(t)
48
	res, err := srv.server.Hover(context.Background(), &protocol.HoverParams{
49
		TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///nonexistent.journal")},
50
		Position:     protocol.Position{Line: 0, Character: 0},
51
	})
52
	if err != nil {
53
		t.Fatal(err)
54
	}
55
	if res != nil {
56
		t.Errorf("got %v, want nil", res)
57
	}
58
}
59
60
// Golden
61
62
func TestGolden_Hover(t *testing.T) {
63
	for _, tt := range []string{"hover-journal", "hover-include"} {
64
		t.Run(tt, func(t *testing.T) {
65
			ar := golden.Read(t, tt)
66
			h := newTxtarHarness(t, ar)
67
68
			var b strings.Builder
69
			for i := range h.cursors {
70
				tdp := h.textDocumentPosition(i)
71
				res, err := h.srv.Hover(t.Context(), &protocol.HoverParams{
72
					TextDocumentPositionParams: tdp,
73
				})
74
				if err != nil {
75
					t.Fatal(err)
76
				}
77
				if res == nil {
78
					fmt.Fprintf(&b, "%d:%d <none>\n", tdp.Position.Line, tdp.Position.Character)
79
					continue
80
				}
81
				mc, ok := res.Contents.(*protocol.MarkupContent)
82
				if !ok {
83
					t.Fatalf("Hover returned %T, want *MarkupContent", res.Contents)
84
				}
85
				fmt.Fprintf(&b, "%d:%d %q %d:%d-%d:%d\n",
86
					tdp.Position.Line, tdp.Position.Character, mc.Value,
87
					res.Range.Start.Line, res.Range.Start.Character, res.Range.End.Line, res.Range.End.Character)
88
			}
89
			golden.Assert(t, ar, b.String())
90
		})
91
	}
92
}
93
94
func BenchmarkHover(b *testing.B) {
95
	content := openJournal(b, "../../journal/testdata/journals/actual-1ktxns-100accts.journal")
96
97
	srv := newServer(b)
98
	srv.server.openDoc(uri.URI("file:///test.journal"), content, 1, "journal")
99
	srv.server.analysisFor(uri.URI("file:///test.journal")) // warm the per-doc cache
100
101
	for tname, tt := range map[string]int{
102
		"date":              strings.Index(content, "2000-01-01 transaction 1") + 3,
103
		"account":           strings.Index(content, "\n  1:2:3 ") + len("\n  ") + 2,
104
		"payee":             strings.Index(content, "transaction 1") + len("transaction ") + 1,
105
		"amount with cost":  strings.Index(content, "2 B @@") + 2,
106
		"commodity in cost": strings.Index(content, "@@ 2 C") + len("@@ 2 C") - 1,
107
		"late amount":       strings.Index(content, "1000 L @") + 2,
108
	} {
109
		b.Run(tname, func(b *testing.B) {
110
			line, col := lsputil.LineCol(content, tt)
111
			params := &protocol.HoverParams{
112
				TextDocument: protocol.TextDocumentIdentifier{URI: uri.URI("file:///test.journal")},
113
				Position:     protocol.Position{Line: uint32(line), Character: uint32(col)},
114
			}
115
116
			// warm up: assert the cursor resolved to a hover
117
			res, err := srv.server.Hover(b.Context(), params)
118
			if err != nil {
119
				b.Fatal(err)
120
			}
121
			if res == nil {
122
				b.Fatalf("%s: no hover", tname)
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
}