all repos

clerk @ a1448b50802379b39dabce5ce0603df42b09c45d

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
tests: switch to txtar, 2 days 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
	"olexsmir.xyz/clerk/journal/semantic"
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
	"undeclared-commodity":     {&UndeclaredCommodity{}},
26
	"undeclared-account":       {&UndeclaredAccount{}},
27
	"unbalanced-transaction":   {&UnbalancedTransaction{}},
28
	"unused-account":           {&UnusedAccount{}},
29
}
30
31
func TestLinter(t *testing.T) {
32
	for tname, trules := range tests {
33
		t.Run(tname, func(t *testing.T) {
34
			a := golden.Read(t, tname)
35
			fsys, err := a.FS()
36
			if err != nil {
37
				t.Fatal(err)
38
			}
39
40
			l := journal.NewLoader()
41
			if _, err := l.LoadFS(fsys, "in.journal"); err != nil {
42
				t.Fatalf("failed to load test journal: %v", err)
43
			}
44
45
			ctx := semantic.Build(l.Ordered())
46
			finds := NewLinter(trules).Run(ctx)
47
48
			var b strings.Builder
49
			Fprint(&b, PathBasename, finds)
50
			golden.Assert(t, a, b.String())
51
		})
52
	}
53
}
54
55
func BenchmarkLinter(b *testing.B) {
56
	ldr := journal.NewLoader()
57
	_, err := ldr.Load(
58
		"../../journal/testdata/journals/actual-1ktxns-100accts.journal",
59
	)
60
	if err != nil {
61
		b.Fatalf("failed to load benchmark journal: %v", err)
62
	}
63
64
	ctx := semantic.Build(ldr.Ordered())
65
	l := NewLinter(Rules)
66
67
	b.ResetTimer()
68
	b.ReportAllocs()
69
	for b.Loop() {
70
		l.Run(ctx)
71
	}
72
}