all repos

clerk @ fd46af1

missing tooling for ledger/hledger

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

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