all repos

clerk @ 1f3a17349e37c8189adb11b8d956acbf3b2080d1

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
improve loader, 1 day ago
1
package linter
2
3
import (
4
	"strings"
5
	"testing"
6
7
	"olexsmir.xyz/clerk/internal/analyzer"
8
	"olexsmir.xyz/clerk/internal/testutil/golden"
9
	"olexsmir.xyz/clerk/journal"
10
)
11
12
var tests = map[string][]Rule{
13
	"correct":                  Rules,
14
	"empty-postings":           {&EmptyPostings{}},
15
	"parse-error":              {&ParseError{}},
16
	"omitted-precision":        {&OmittedPrecision{}},
17
	"missing-commodity":        {&MissingCommodity{}},
18
	"missing-status":           {&MissingStatus{}},
19
	"missing-payee":            {&MissingPayee{}},
20
	"account-depth":            {&AccountDepthLimit{MaxDepth: 3}},
21
	"multiple-omitted-amounts": {&MultipleOmittedAmounts{}},
22
	"orderdate":                {&OrderDate{}},
23
	"duplicated-account":       {&DuplicatedAccount{}},
24
	"duplicated-commodity":     {&DuplicatedCommodity{}},
25
	"duplicated-transaction":   {&DuplicatedTransaction{}},
26
	"undeclared-commodity":     {&UndeclaredCommodity{}},
27
	"undeclared-account":       {&UndeclaredAccount{}},
28
	"unbalanced-transaction":   {&UnbalancedTransaction{}},
29
	"unused-account":           {&UnusedAccount{}},
30
}
31
32
func TestLinter(t *testing.T) {
33
	for tname, trules := range tests {
34
		t.Run(tname, func(t *testing.T) {
35
			a := golden.Read(t, tname)
36
			fsys, err := a.FS()
37
			if err != nil {
38
				t.Fatal(err)
39
			}
40
41
			l := journal.NewLoader()
42
			rj, err := l.ResolveFS(fsys, "in.journal")
43
			if err != nil {
44
				t.Fatalf("failed to load test journal: %v", err)
45
			}
46
47
			ctx := analyzer.Build(rj)
48
			finds := NewLinter(trules).Run(ctx)
49
50
			var b strings.Builder
51
			Fprint(&b, PathBasename, finds)
52
			golden.Assert(t, a, b.String())
53
		})
54
	}
55
}
56
57
func BenchmarkLinter(b *testing.B) {
58
	ldr := journal.NewLoader()
59
	rj, err := ldr.Resolve(
60
		"../../journal/testdata/journals/actual-1ktxns-100accts.journal",
61
	)
62
	if err != nil {
63
		b.Fatalf("failed to load benchmark journal: %v", err)
64
	}
65
66
	ctx := analyzer.Build(rj)
67
	l := NewLinter(Rules)
68
69
	b.ResetTimer()
70
	b.ReportAllocs()
71
	for b.Loop() {
72
		l.Run(ctx)
73
	}
74
}