all repos

clerk @ 4c27ef1

missing tooling for ledger/hledger

clerk/journal/parser/parser_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
journal/parser: fix include directive, 1 month ago
1
package parser
2
3
import (
4
	"testing"
5
6
	"olexsmir.xyz/clerk/internal/testutil/golden"
7
	"olexsmir.xyz/clerk/journal/ast"
8
	"olexsmir.xyz/clerk/journal/lexer"
9
)
10
11
func TestParser_ParseFile(t *testing.T) {
12
	tests := []struct{ name, inp string }{
13
		{"blank line", "\n"},
14
		{"comment semicolon", "; a comment\n"},
15
		{"comment hash", "# a comment\n"},
16
		{"comment percent", "% a comment\n"},
17
		{"comment star", "* a comment\n"},
18
		{"alias directive", "alias checking = assets:bank:checking\n"},
19
		{"tag directive", "tag project-xyz\n"},
20
		{"year directive", "year 1488\n"},
21
		{"decimal-mark directive", "decimal-mark ,\n"},
22
		{"C directive", "C 1.00s = 100c\n"},
23
		{"D directive", `D $1.00
24
D 10 UAH
25
`},
26
		{"P directive", "P 2024/01/01 USD 40.50 UAH\n"},
27
		{"P directive with time", "P 2024-01-01 12:00:00 USD 40.50 UAH\n"},
28
		{"N directive", "N $\n"},
29
		{"inclue directive", "include ./path-to.journal\n"},
30
		{"inclue directive with comment", "include ./path.journal ; something importnat\n"},
31
		{"apply tag directive", "apply tag hashtag\n"},
32
		{"apply fixed directive", "apply fixed CAD $0.90\n"},
33
		{"end apply directive", "end apply tag\n"},
34
		{"comment directive", "comment\nsome text\nend comment\n"},
35
		{"comment directive end alone", "comment\nsome text\nend\n"},
36
		{"comment directive with header", "comment tag:hidden\nstuff\nend\n"},
37
		{"empty comment block", "comment\nend\n"},
38
		{"never ending comment directive", "comment\nsome text\n"},
39
		{"nested apply tag directives", `apply tag hashtag
40
apply tag nestedtag: true
41
2011/01/27 Book Store
42
  expenses:books  $20.00
43
  liabilities:mastercard
44
end apply tag
45
end apply tag
46
`},
47
		{"account directive", "account expenses:food\n"},
48
		{"account directive with comment", "account expenses:food ; my account\n"},
49
		{"account with subdirectives", `account expenses:food
50
  note some note
51
  alias food  ; this gets ignored
52
`},
53
		{"comodity directive", "commodity $\n"},
54
		{"comodity directive word", "commodity UAH\n"},
55
		{"comodity directive no space", "commodity $1000.00\n"},
56
		{"commodity quantity first", "commodity 1,000.00 UAH\n"},
57
		{"commodity quantity after", "commodity UAH 1,000.00\n"},
58
		{"commodity with subdirectives", `commodity UAH
59
  format 1 000.00 UAH
60
  note Божествена Гривня  ; this gets ignored
61
`},
62
		{"payee directive", `payee grocery store
63
payee 'grocery store 3'
64
payee "grocery store 2"
65
payee grocery store 1
66
`},
67
		{"transaction", "2024/01/01\n"},
68
		{"automated transaction", `= ^income
69
    (liabilities:tax)  *.33
70
71
= expenses:gifts
72
    budget:gifts  *-1
73
    assets:budget  *1
74
75
= income:salary
76
    budget:savings  (amount * 0.5)
77
`},
78
		{"transaction with payee", "2024-01-01 groceries\n"},
79
		{"transaction with digit payees", `2002/01/01 * 1a1a6305d06ce4b284dba0d267c23f69d70c20be
80
    af0628973ff35bd62ddb048fa41dd8d83c1c46fe       $474.31
81
    fc6f6f10f627ad1a5af9d488c98405a1498d019d
82
83
2002/03/01 * 9861ce541c17b11f627e71c26bf350b33141f62b
84
    0ecbb1b15e2cf3e515cc0f8533e5bb0fb2326728        $14.91
85
    fc6f6f10f627ad1a5af9d488c98405a1498d019d
86
`},
87
		{"account with spaces", `2026-05-11 testies
88
	expenses:account name  20.00
89
	assets:bank
90
`},
91
		{"transaction with multiword payee", "2024-01-01 opening balances\n"},
92
		{"transaction pending", "2024-01-01 ! groceries\n"},
93
		{"transaction clearerd", "2024-01-01 * groceries\n"},
94
		{"transaction with note", "2024-01-01 groceries | eating out\n"},
95
		{"transaction with comment", "2024-01-01 groceries ; note\n"},
96
		{"transaction with secondary date", "2024/01/01=2024/01/02 groceries\n"},
97
		{"transaction with costs", `2026-05-11 testies
98
	expenses:atm  20.00 UAH @ 1 USD
99
	assets:bank
100
101
2026-05-11 testies2
102
	expenses:atm  20.00 UAH @@ 1 USD
103
	assets:bank
104
105
2026-05-12 testies3
106
	expenses:atm  20.00 UAH @ 1 USD = 20.00 UAH @ 1 USD
107
	assets:bank
108
109
2015-01-03 money exchange office
110
    assets:cash  -20 EUR @ 7.53 HRK
111
    assets:cash  150.60 HRK
112
`},
113
		{"transaction with cost and assertion", `2026-05-11 testies
114
	expenses:atm  20.00 UAH @ 1 USD = 0 UAH
115
	assets:bank
116
`},
117
		{"transaction with posting", `2024-01-01 groceries
118
    expenses:food  $10.00
119
    assets:checking
120
`},
121
		{"transaction with unicode commodity symbols", `2024-01-01 groceries
122
    expenses:food  €10.00
123
    expenses:food  £5.00
124
    expenses:food  ₹700.00
125
    assets:checking
126
`},
127
		{"transaction with strange commodity symbols", `2024-01-01 groceries
128
2026-05-20
129
  asdf  123 $€£
130
  asdf2
131
132
2026-05-20
133
  asdf  123 bytes
134
  asdf2
135
`},
136
		{"special chars in description", `
137
2024/01/01 groceries | 1 + 2
138
2024/01/01 groceries | 1 * 2
139
2024/01/01 groceries | 1 ! 2
140
2024/01/01 groceries | 1 # 2
141
2024/01/01 groceries | 1 % 2
142
2024/01/01 groceries | 1 ^ 2
143
2024/01/01 groceries | 1 & 2
144
2024/01/01 groceries | 1 ( 2
145
2024/01/01 groceries | 1 ) 2
146
2024/01/01 groceries | 1 [ 2
147
2024/01/01 groceries | 1 ] 2
148
2024/01/01 groceries | 1 { 2
149
2024/01/01 groceries | 1 } 2
150
2026-06-07 * payee !one | something *important*
151
    expenses:food  40.00
152
    assets:cash
153
`},
154
		{"transaction with tabs", `2024-01-01 groceries
155
	expenses:food  $10.00
156
	assets:checking
157
`},
158
		{"transaction in ukrainian", `2024/03/02 Обід
159
  витрати:їжа  350 UAH
160
  активи:готівка
161
`},
162
		{"transaction with code", `2024-01-01 (123) groceries
163
    expenses:food  $10.00
164
    assets:checking
165
`},
166
		{"transaction with posting amounts", `2024.01.01 groceries
167
    expenses:food  10.00 UAH
168
    assets:checking  -10.00 UAH
169
`},
170
		{"transaction with spaced account name", `2022-01-01 opening balances
171
    assets                    21 = 21
172
    equity:opening/closing balances
173
`},
174
		{"transaction with inline comment", `2024/01/01 groceries
175
	Expenses:Good  $10.00 ; food
176
	Assets:Checking
177
`},
178
		{"transaction with header comment", `2024/01/01 groceries
179
	; header comment
180
	expenses:food  $10.00
181
	assets:checking
182
`},
183
		{"transaction with trailing indent", `
184
2013/1/1 * pay taxes
185
    expenses:personal:tax              $1250
186
    assets:bank:checking               $-1250
187
188
`},
189
		{"transaction with balance assertion", `2024/01/01 groceries
190
	expenses:food  $10.00 = $100.00
191
	assets:checking
192
193
2025/03/07 groceries2
194
	expenses:food  $10.00 == $100.00
195
	assets:checking  == $0.00
196
197
2025/04/08 groceries3
198
	expenses:food  $10.00
199
	assets:checking  = $0.00
200
`},
201
		{"transaction with virtual accounts", `2024/01/01 groceries
202
	(virtual:account)  1 PESO
203
	[something:else]   5 PESO
204
	something:else
205
`},
206
		{"virtual postings with statuses", `2024/01/01 test
207
  ! (assets:cash)  $10
208
  (income:gift)  $-10
209
210
2024/01/01 test
211
    (! assets:cash)  $10
212
    (income:gift)  $-10
213
214
2024/01/01 test
215
    ! (! assets:cash)  $10
216
    (* income:gift)  $-10
217
`},
218
		{"period transaction expressions", `
219
~ monthly
220
    expenses:rent          $2000
221
    assets:bank:checking
222
223
~ monthly from 2023-04-15 to 2023-06-16
224
    expenses:utilities          $400
225
    assets:bank:checking
226
227
~ every 2 months  in 2023, we will review
228
    expenses:utilities          $400
229
    assets:bank:checking
230
231
~ monthly  Next year blah blah
232
    expenses:food  $100
233
    assets:checking
234
235
~ monthly from 2018/6 ;In 2019 we will change this
236
    expenses:food  $100
237
    assets:checking
238
`},
239
		{"unexpected token", `@@@ garbage\n`},
240
		{"recovery after bad posting", `2024-01-01 groceries
241
    @@@invalid
242
    assets:checking
243
244
2024/01/02 salary
245
    income:salary  $1000
246
    assets:checking
247
`},
248
		{"illegal only", "@@@\n"},
249
		{"illegal at start", "@@@ garbage\n"},
250
		{"illegal in posting", `2024/01/01 groceries
251
    @@@invalid
252
    assets:checking
253
`},
254
		{"illegal between transactions", `2024/01/01 groceries
255
    expenses:food  $10
256
    assets:checking
257
@@@
258
2024/01/02 salary
259
    income:salary  $1000
260
    assets:checking
261
`},
262
		{"multiple bad postings", `2024/01/01 groceries
263
    expenses:food  $10
264
    @@@bad1
265
    @bad2
266
    assets:checking
267
`},
268
		{"three bad postings", `2024/01/01 groceries
269
    expenses:food  $10
270
    @@@bad1
271
    @bad2
272
    @bad3
273
    assets:checking
274
`},
275
		{"quoted payee names", `2024-01-01 "groceries store"
276
  expenses:food  $10
277
  assets:checking
278
`},
279
		{"digit group marks", `2024-01-01 groceries
280
  expenses:food  1 000.00 UAH
281
  expenses:supplies  1'000.00 USD
282
  assets:checking  -2_000.00 USD
283
`},
284
		{"directive prefixes", `!account expenses:food
285
@commodity USD
286
`},
287
		{"bad between good", `2024/01/01 groceries
288
    expenses:food  $10
289
    @@@bad
290
    assets:cash  $5
291
`},
292
		{"all postings bad", `2024/01/01 groceries
293
    @@@bad1
294
    @bad2
295
    @bad3
296
`},
297
		{"bad then next transaction", `2024/01/01 groceries
298
    expenses:food  $10
299
    @@@bad
300
301
2024/01/02 salary
302
    income:salary  $1000
303
    assets:checking
304
`},
305
		{"comment between bad postings", `2024/01/01 groceries
306
    expenses:food  $10
307
    @@@bad1
308
    ; a comment
309
    @bad2
310
    assets:checking
311
`},
312
		{"bad posting at end", `2024/01/01 groceries
313
    expenses:food  $10
314
    @@@bad
315
316
2024/01/02 salary
317
    income:salary  $1000
318
    assets:checking
319
`},
320
	}
321
	for _, tt := range tests {
322
		t.Run(tt.name, func(t *testing.T) {
323
			l := lexer.New("j", []byte(tt.inp))
324
			f := New(l).ParseJournal()
325
			golden.Assert(t, ast.Dump(f))
326
		})
327
	}
328
}
329
330
func FuzzParser(f *testing.F) {
331
	f.Add([]byte(""))
332
	f.Add([]byte("account expenses:food\n"))
333
	f.Add([]byte("account a\n  ; subdirective\n"))
334
	f.Add([]byte("commodity 1,000.00 UAH\n"))
335
	f.Add([]byte("include other.journal\n"))
336
	f.Add([]byte("alias checking = assets:bank:checking\n"))
337
	f.Add([]byte("2024/01/01 * groceries\n    expenses:food  $10.00\n    assets:checking\n"))
338
	f.Add([]byte("2024/01/01=2024/01/02 groceries\n"))
339
	f.Add([]byte("2024/01/01 groceries\n  expenses:food  $10.00\n  assets:checking\n"))
340
	f.Add([]byte("2008/06/03 * eat & shop\n    expenses:food      $1\n    expenses:supplies  $1\n    assets:cash\n"))
341
	f.Add([]byte("2015-01-03 * Money exchange office\n    Assets:Cash  -20 EUR @ 7.53 HRK\n    Assets:Cash  150.60 HRK\n"))
342
	f.Add([]byte("2024/01/01 t ; inline comment\n  a  $10\n"))
343
	f.Add([]byte("2024/01/01 t\n  (a)  10 @@ $20\n  [b]  30\n"))
344
	f.Add([]byte("2024/01/01 ß\n  (ß)  10 ß\n"))
345
	f.Add([]byte("2024/01/01 t\n  (! a)  10\n"))
346
	f.Add([]byte("2024/01/01 t\n  a  $10 == $10\n"))
347
	f.Add([]byte("  2024/01/01 t\n  a  $10\n"))
348
	f.Add([]byte("P 2024/01/01 USD 41.50 UAH\n"))
349
	f.Add([]byte("P 2024-01-01 12:00:00 USD 41.50 UAH\n"))
350
	f.Add([]byte("P 2024-01-01 12:00 USD 41.50 UAH\n"))
351
	f.Add([]byte("~ monthly\n    expenses:food  $100\n    assets:checking\n"))
352
	f.Add([]byte("= /^Income/\n  expenses:food  $10\n"))
353
	f.Add([]byte("; a comment\n"))
354
	f.Add([]byte("comment\nbody\nend\n"))
355
	f.Add([]byte("\n\n\n"))
356
	f.Add([]byte("перевірка\n"))
357
	f.Add([]byte("N $\n"))
358
	f.Add([]byte("apply tag foo\nend\n"))
359
	f.Add([]byte("@@@\n"))
360
	f.Add([]byte("   \n"))
361
	f.Add([]byte("0\n"))
362
	f.Add([]byte{0xff, 0xfe, 0x00})
363
364
	f.Fuzz(func(t *testing.T, data []byte) {
365
		l := lexer.New("j", data)
366
		j := New(l).ParseJournal()
367
		if j == nil {
368
			t.Fatalf("nil journal for input %q", string(data))
369
		}
370
371
		// error spans must be in bounds (allow sentinel extension past input)
372
		dataLen := len(data)
373
		for _, e := range j.Errors {
374
			if e.Span.Start.Offset < 0 {
375
				t.Fatal("error span start is negative")
376
			}
377
			if e.Span.End.Offset > dataLen+1 {
378
				t.Fatalf("error span end out of bounds: [%d,%d] len=%d", e.Span.Start.Offset, e.Span.End.Offset, dataLen)
379
			}
380
			if len(e.Message) == 0 {
381
				t.Fatal("empty error message")
382
			}
383
		}
384
385
		// dump must not panic and must be deterministic
386
		dump1, dump2 := ast.Dump(j), ast.Dump(j)
387
		if dump1 != dump2 {
388
			t.Fatal("non-deterministic dump")
389
		}
390
	})
391
}