all repos

clerk @ e2e9e2b

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
lint: add duplicated-transaction, 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
			if _, err := l.LoadFS(fsys, "in.journal"); err != nil {
43
				t.Fatalf("failed to load test journal: %v", err)
44
			}
45
46
			ctx := analyzer.Build(l.Ordered())
47
			finds := NewLinter(trules).Run(ctx)
48
49
			var b strings.Builder
50
			Fprint(&b, PathBasename, finds)
51
			golden.Assert(t, a, b.String())
52
		})
53
	}
54
}
55
56
func BenchmarkLinter(b *testing.B) {
57
	ldr := journal.NewLoader()
58
	_, err := ldr.Load(
59
		"../../journal/testdata/journals/actual-1ktxns-100accts.journal",
60
	)
61
	if err != nil {
62
		b.Fatalf("failed to load benchmark journal: %v", err)
63
	}
64
65
	ctx := analyzer.Build(ldr.Ordered())
66
	l := NewLinter(Rules)
67
68
	b.ResetTimer()
69
	b.ReportAllocs()
70
	for b.Loop() {
71
		l.Run(ctx)
72
	}
73
}