all repos

clerk @ a69e534

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
linter: add undeclared-account, 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
	"multiple-omitted-amounts": {&MultipleOmittedAmounts{}},
20
	"orderdate":                {&OrderDate{}},
21
	"duplicated-account":       {&DuplicatedAccount{}},
22
	"duplicated-commodity":     {&DuplicatedCommodity{}},
23
	"undeclared-account":       {&UndeclaredAccount{}},
24
}
25
26
func TestLinter(t *testing.T) {
27
	for tname, trules := range tests {
28
		t.Run(tname, func(t *testing.T) {
29
			inp := golden.Load(t, tname)
30
			pf, err := journal.NewLoader().LoadBytes(tname+".journal", inp)
31
			if err != nil {
32
				t.Fatalf("failed to load test journal: %v\n", err)
33
			}
34
35
			finds := NewLinter(trules).Run(pf.Ast)
36
37
			var b strings.Builder
38
			Fprint(&b, PathBasename, finds)
39
			golden.Assert(t, tname, b.String())
40
		})
41
	}
42
}
43
44
func BenchmarkLinter(b *testing.B) {
45
	pf, err := journal.NewLoader().Load(
46
		"../../journal/testdata/journals/actual-1ktxns-100accts.journal",
47
	)
48
	if err != nil {
49
		b.Fatalf("failed to load benchmark journal: %v", err)
50
	}
51
	l := NewLinter(Rules)
52
53
	b.ResetTimer()
54
	b.ReportAllocs()
55
	for b.Loop() {
56
		l.Run(pf.Ast)
57
	}
58
}