clerk/internal/linter/linter_test.go (view raw)
| 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 | inp := golden.Load(t, tname) |
| 35 | pf, err := journal.NewLoader().LoadBytes(tname+".journal", inp) |
| 36 | if err != nil { |
| 37 | t.Fatalf("failed to load test journal: %v\n", err) |
| 38 | } |
| 39 | |
| 40 | ctx := semantic.Build([]*journal.ParsedFile{pf}) |
| 41 | finds := NewLinter(trules).Run(ctx) |
| 42 | |
| 43 | var b strings.Builder |
| 44 | Fprint(&b, PathBasename, finds) |
| 45 | golden.Assert(t, tname, b.String()) |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func BenchmarkLinter(b *testing.B) { |
| 51 | ldr := journal.NewLoader() |
| 52 | _, err := ldr.Load( |
| 53 | "../../journal/testdata/journals/actual-1ktxns-100accts.journal", |
| 54 | ) |
| 55 | if err != nil { |
| 56 | b.Fatalf("failed to load benchmark journal: %v", err) |
| 57 | } |
| 58 | |
| 59 | ctx := semantic.Build(ldr.Ordered()) |
| 60 | l := NewLinter(Rules) |
| 61 | |
| 62 | b.ResetTimer() |
| 63 | b.ReportAllocs() |
| 64 | for b.Loop() { |
| 65 | l.Run(ctx) |
| 66 | } |
| 67 | } |