all repos

clerk @ fbfbb0b5688aeeeea8d39c44eee36f775995f802

missing tooling for ledger/hledger

clerk/internal/linter/linter_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add account-depth rule, 1 month ago
1
package linter
2
3
import (
4
	"strings"
5
	"testing"
6
7
	"olexsmir.xyz/clerk/internal/testutil/golden"
8
	"olexsmir.xyz/clerk/journal"
9
)
10
11
var tests = map[string][]Rule{
12
	"correct":           Rules,
13
	"empty-postings":    {&EmptyPostings{}},
14
	"parse-error":       {&ParseError{}},
15
	"omitted-precision": {&OmittedPrecision{}},
16
	"missing-commodity": {&MissingCommodity{}},
17
	"missing-status":    {&MissingStatus{}},
18
	"account-depth":     {&AccountDepthLimit{MaxDepth: 3}},
19
}
20
21
func TestLinter(t *testing.T) {
22
	for tname, trules := range tests {
23
		t.Run(tname, func(t *testing.T) {
24
			inp := golden.Load(t, tname)
25
			pf, err := journal.NewLoader().LoadBytes(tname+".journal", inp)
26
			if err != nil {
27
				t.Fatalf("failed to load test journal: %v\n", err)
28
			}
29
30
			finds := NewLinter(trules).Run(pf.Ast)
31
32
			var b strings.Builder
33
			Fprint(&b, PathBasename, finds)
34
			golden.Assert(t, tname, b.String())
35
		})
36
	}
37
}
38
39
func BenchmarkLinter(b *testing.B) {
40
	pf, err := journal.NewLoader().Load(
41
		"../../journal/testdata/journals/actual-1ktxns-100accts.journal",
42
	)
43
	if err != nil {
44
		b.Fatalf("failed to load benchmark journal: %v", err)
45
	}
46
	l := NewLinter(Rules)
47
48
	b.ResetTimer()
49
	b.ReportAllocs()
50
	for b.Loop() {
51
		l.Run(pf.Ast)
52
	}
53
}