all repos

clerk @ 1b934a8ee8892eb089a019c5471238bdec1fcd18

missing tooling for ledger/hledger

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

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