all repos

clerk @ dc54e90

missing tooling for ledger/hledger

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

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