all repos

clerk @ e5cc255

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
parser: support C conversion directive, 14 days 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
135
		{"transaction with tabs", `2024-01-01 groceries
136
	expenses:food  $10.00
137
	assets:checking
138
`},
139
		{"transaction in ukrainian", `2024/03/02 Обід
140
  витрати:їжа  350 UAH
141
  активи:готівка
142
`},
143
		{"transaction with code", `2024-01-01 (123) groceries
144
    expenses:food  $10.00
145
    assets:checking
146
`},
147
		{"transaction with posting amounts", `2024.01.01 groceries
148
    expenses:food  10.00 UAH
149
    assets:checking  -10.00 UAH
150
`},
151
		{"transaction with spaced account name", `2022-01-01 opening balances
152
    assets                    21 = 21
153
    equity:opening/closing balances
154
`},
155
		{"transaction with inline comment", `2024/01/01 groceries
156
	Expenses:Good  $10.00 ; food
157
	Assets:Checking
158
`},
159
		{"transaction with header comment", `2024/01/01 groceries
160
	; header comment
161
	expenses:food  $10.00
162
	assets:checking
163
`},
164
		{"transaction with trilling indent", `
165
2013/1/1 * pay taxes
166
    expenses:personal:tax              $1250
167
    assets:bank:checking               $-1250
168
    
169
`},
170
		{"transaction with balance assertion", `2024/01/01 groceries
171
	expenses:food  $10.00 = $100.00
172
	assets:checking
173
174
2025/03/07 groceries2
175
	expenses:food  $10.00 == $100.00
176
	assets:checking  == $0.00
177
178
2025/04/08 groceries3
179
	expenses:food  $10.00
180
	assets:checking  = $0.00
181
`},
182
		{"transaction with virtual accounts", `2024/01/01 groceries
183
	(virtual:account)  1 PESO
184
	[something:else]   5 PESO
185
	something:else
186
`},
187
		{"virtual postings with statuses", `2024/01/01 test
188
  ! (assets:cash)  $10
189
  (income:gift)  $-10
190
191
2024/01/01 test
192
    (! assets:cash)  $10
193
    (income:gift)  $-10
194
195
2024/01/01 test
196
    ! (! assets:cash)  $10
197
    (* income:gift)  $-10
198
`},
199
		{"period transaction expressions", `
200
~ monthly
201
    expenses:rent          $2000
202
    assets:bank:checking
203
204
~ monthly from 2023-04-15 to 2023-06-16
205
    expenses:utilities          $400
206
    assets:bank:checking
207
208
~ every 2 months  in 2023, we will review
209
    expenses:utilities          $400
210
    assets:bank:checking
211
212
~ monthly  Next year blah blah
213
    expenses:food  $100
214
    assets:checking
215
216
~ monthly from 2018/6 ;In 2019 we will change this
217
    expenses:food  $100
218
    assets:checking
219
`},
220
		{"unexpected token", `@@@ garbage\n`},
221
		{"recovery after bad posting", `2024-01-01 groceries
222
    @@@invalid
223
    assets:checking
224
225
2024/01/02 salary
226
    income:salary  $1000
227
    assets:checking
228
`},
229
		{"illegal only", "@@@\n"},
230
		{"illegal at start", "@@@ garbage\n"},
231
		{"illegal in posting", `2024/01/01 groceries
232
    @@@invalid
233
    assets:checking
234
`},
235
		{"illegal between transactions", `2024/01/01 groceries
236
    expenses:food  $10
237
    assets:checking
238
@@@
239
2024/01/02 salary
240
    income:salary  $1000
241
    assets:checking
242
`},
243
		{"multiple bad postings", `2024/01/01 groceries
244
    expenses:food  $10
245
    @@@bad1
246
    @bad2
247
    assets:checking
248
`},
249
		{"three bad postings", `2024/01/01 groceries
250
    expenses:food  $10
251
    @@@bad1
252
    @bad2
253
    @bad3
254
    assets:checking
255
`},
256
		{"quoted payee names", `2024-01-01 "groceries store"
257
  expenses:food  $10
258
  assets:checking
259
`},
260
		{"digit group marks", `2024-01-01 groceries
261
  expenses:food  1 000.00 UAH
262
  expenses:supplies  1'000.00 USD
263
  assets:checking  -2_000.00 USD
264
`},
265
		{"directive prefixes", `!account expenses:food
266
@commodity USD
267
`},
268
		{"bad between good", `2024/01/01 groceries
269
    expenses:food  $10
270
    @@@bad
271
    assets:cash  $5
272
`},
273
		{"all postings bad", `2024/01/01 groceries
274
    @@@bad1
275
    @bad2
276
    @bad3
277
`},
278
		{"bad then next transaction", `2024/01/01 groceries
279
    expenses:food  $10
280
    @@@bad
281
282
2024/01/02 salary
283
    income:salary  $1000
284
    assets:checking
285
`},
286
		{"comment between bad postings", `2024/01/01 groceries
287
    expenses:food  $10
288
    @@@bad1
289
    ; a comment
290
    @bad2
291
    assets:checking
292
`},
293
		{"bad posting at end", `2024/01/01 groceries
294
    expenses:food  $10
295
    @@@bad
296
297
2024/01/02 salary
298
    income:salary  $1000
299
    assets:checking
300
`},
301
	}
302
	for _, tt := range tests {
303
		t.Run(tt.name, func(t *testing.T) {
304
			l := lexer.New("j", []byte(tt.inp))
305
			f := New(l).ParseJournal()
306
			golden.Assert(t, ast.Dump(f))
307
		})
308
	}
309
}
310
311
func FuzzParser(f *testing.F) {
312
	f.Add([]byte(""))
313
	f.Add([]byte("account expenses:food\n"))
314
	f.Add([]byte("account a\n  ; subdirective\n"))
315
	f.Add([]byte("commodity 1,000.00 UAH\n"))
316
	f.Add([]byte("include other.journal\n"))
317
	f.Add([]byte("alias checking = assets:bank:checking\n"))
318
	f.Add([]byte("2024/01/01 * groceries\n    expenses:food  $10.00\n    assets:checking\n"))
319
	f.Add([]byte("2024/01/01=2024/01/02 groceries\n"))
320
	f.Add([]byte("2024/01/01 groceries\n  expenses:food  $10.00\n  assets:checking\n"))
321
	f.Add([]byte("2008/06/03 * eat & shop\n    expenses:food      $1\n    expenses:supplies  $1\n    assets:cash\n"))
322
	f.Add([]byte("2015-01-03 * Money exchange office\n    Assets:Cash  -20 EUR @ 7.53 HRK\n    Assets:Cash  150.60 HRK\n"))
323
	f.Add([]byte("2024/01/01 t ; inline comment\n  a  $10\n"))
324
	f.Add([]byte("2024/01/01 t\n  (a)  10 @@ $20\n  [b]  30\n"))
325
	f.Add([]byte("2024/01/01 ß\n  (ß)  10 ß\n"))
326
	f.Add([]byte("2024/01/01 t\n  (! a)  10\n"))
327
	f.Add([]byte("2024/01/01 t\n  a  $10 == $10\n"))
328
	f.Add([]byte("  2024/01/01 t\n  a  $10\n"))
329
	f.Add([]byte("P 2024/01/01 USD 41.50 UAH\n"))
330
	f.Add([]byte("P 2024-01-01 12:00:00 USD 41.50 UAH\n"))
331
	f.Add([]byte("P 2024-01-01 12:00 USD 41.50 UAH\n"))
332
	f.Add([]byte("~ monthly\n    expenses:food  $100\n    assets:checking\n"))
333
	f.Add([]byte("= /^Income/\n  expenses:food  $10\n"))
334
	f.Add([]byte("; a comment\n"))
335
	f.Add([]byte("comment\nbody\nend\n"))
336
	f.Add([]byte("\n\n\n"))
337
	f.Add([]byte("перевірка\n"))
338
	f.Add([]byte("N $\n"))
339
	f.Add([]byte("apply tag foo\nend\n"))
340
	f.Add([]byte("@@@\n"))
341
	f.Add([]byte("   \n"))
342
	f.Add([]byte("0\n"))
343
	f.Add([]byte{0xff, 0xfe, 0x00})
344
345
	f.Fuzz(func(t *testing.T, data []byte) {
346
		l := lexer.New("j", data)
347
		j := New(l).ParseJournal()
348
		if j == nil {
349
			t.Fatalf("nil journal for input %q", string(data))
350
		}
351
352
		// error spans must be in bounds (allow sentinel extension past input)
353
		dataLen := len(data)
354
		for _, e := range j.Errors {
355
			if e.Span.Start.Offset < 0 {
356
				t.Fatal("error span start is negative")
357
			}
358
			if e.Span.End.Offset > dataLen+1 {
359
				t.Fatalf("error span end out of bounds: [%d,%d] len=%d", e.Span.Start.Offset, e.Span.End.Offset, dataLen)
360
			}
361
			if len(e.Message) == 0 {
362
				t.Fatal("empty error message")
363
			}
364
		}
365
366
		// dump must not panic and must be deterministic
367
		dump1, dump2 := ast.Dump(j), ast.Dump(j)
368
		if dump1 != dump2 {
369
			t.Fatal("non-deterministic dump")
370
		}
371
	})
372
}