AtlatestRepositorysigil-ledger
sigil-ledger / tree / testtest-report.sgl
1
;;; Test suite for (ledger report)3
(import (sigil test)4
(sigil string)5
(ledger)6
(ledger report))8
;; Test journal path — used by all report tests9
(define test-journal-path "/tmp/sigil-ledger-test.journal")11
;; Track whether hledger is available for conditional assertions12
(define hledger? (hledger-available?))14
;; Write test journal file using the ledger module's own write function15
(write-journal test-journal-path16
(list17
(journal-transaction18
date: "2026-03-15"19
status: "*"20
description: "Grocery store"21
postings: (list22
(journal-posting23
account: "expenses:food:groceries"24
amount: (journal-amount quantity: 47.23 commodity: "$"))25
(journal-posting26
account: "assets:checking")))27
(journal-transaction28
date: "2026-03-16"29
status: "*"30
description: "Gas station"31
postings: (list32
(journal-posting33
account: "expenses:transport:fuel"34
amount: (journal-amount quantity: 30 commodity: "$"))35
(journal-posting36
account: "assets:checking")))37
(journal-transaction38
date: "2026-03-17"39
status: "*"40
description: "Paycheck"41
postings: (list42
(journal-posting43
account: "assets:checking"44
amount: (journal-amount quantity: 3000 commodity: "$"))45
(journal-posting46
account: "income:salary")))))48
;; ========== Availability ==========50
(test-group "hledger availability"51
(test "hledger-available? returns boolean"52
(assert-true (boolean? hledger?))))54
;; ========== hledger-command (first to warm up process-output->string) ==========56
(test-group "hledger-command"57
(test "list accounts via hledger command"58
(if (not hledger?)59
(assert-true #t)60
(let ((result (hledger-command "accounts" file: test-journal-path)))61
(assert-true (string? result))62
(assert-true (string-find result "assets:checking"))63
(assert-true (string-find result "expenses:food:groceries")))))65
(test "hledger-command with extra args"66
(if (not hledger?)67
(assert-true #t)68
(let ((result (hledger-command "bal" file: test-journal-path69
args: (list "expenses" "--depth" "1"))))70
(assert-true (string? result))71
(assert-true (string-find result "expenses"))))))73
;; ========== hledger-balance ==========75
(test-group "hledger-balance"76
(test "balance report returns non-empty string"77
(if (not hledger?)78
(assert-true #t)79
(let ((result (hledger-balance test-journal-path)))80
(assert-true (string? result))81
(assert-true (> (string-length result) 0)))))83
(test "balance report with account query"84
(if (not hledger?)85
(assert-true #t)86
(let ((result (hledger-balance test-journal-path "expenses")))87
(assert-true (string? result))88
(assert-true (string-find result "expenses")))))90
(test "balance report with depth option"91
(if (not hledger?)92
(assert-true #t)93
(let ((result (hledger-balance test-journal-path depth: 1)))94
(assert-true (string? result))95
(assert-true (string-find result "assets")))))97
(test "balance report with tree option"98
(if (not hledger?)99
(assert-true #t)100
(let ((result (hledger-balance test-journal-path tree: #t)))101
(assert-true (string? result))102
(assert-true (> (string-length result) 0)))))104
(test "balance report with date filter"105
(if (not hledger?)106
(assert-true #t)107
(let ((result (hledger-balance test-journal-path108
begin: "2026-03-16" end: "2026-03-17")))109
(assert-true (string? result))110
(assert-true (string-find result "fuel"))))))112
;; ========== hledger-register ==========114
(test-group "hledger-register"115
(test "register report returns non-empty string"116
(if (not hledger?)117
(assert-true #t)118
(let ((result (hledger-register test-journal-path)))119
(assert-true (string? result))120
(assert-true (> (string-length result) 0)))))122
(test "register report with account filter"123
(if (not hledger?)124
(assert-true #t)125
(let ((result (hledger-register test-journal-path "expenses")))126
(assert-true (string? result))127
;; hledger may abbreviate account names in register output128
(assert-true (string-find result "food")))))130
(test "register report with date range"131
(if (not hledger?)132
(assert-true #t)133
(let ((result (hledger-register test-journal-path begin: "2026-03-17")))134
(assert-true (string? result))135
(assert-true (string-find result "Paycheck"))))))137
;; ========== hledger-income-statement ==========139
(test-group "hledger-income-statement"140
(test "income statement returns non-empty string"141
(if (not hledger?)142
(assert-true #t)143
(let ((result (hledger-income-statement test-journal-path)))144
(assert-true (string? result))145
(assert-true (> (string-length result) 0)))))147
(test "income statement shows expenses"148
(if (not hledger?)149
(assert-true #t)150
(let ((result (hledger-income-statement test-journal-path)))151
(assert-true (string-find result "expenses"))))))153
;; ========== hledger-print-json ==========155
(test-group "hledger-print-json"156
(test "print-json returns parsed data"157
(if (not hledger?)158
(assert-true #t)159
(let ((result (hledger-print-json test-journal-path)))160
(assert-true (not (null? result)))))))162
(run-tests)