1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
import unittest
import streams
import sequtils
import nimrec/[parser, record, recordset, utils]
suite "parsing":
test "basics":
const data = """
Name: John Doe
Age: 34
Name: Jane Doe
Age: 32
"""
var ss = newStringStream(data)
var records = toSeq(records(ss))
check(len(records) == 2)
check(len(records[0]) == 2)
check(len(records[1]) == 2)
check(records[0]["Name"] == "John Doe")
check(records[0]["Age"] == "34")
check(records[1]["Name"] == "Jane Doe")
check(records[1]["Age"] == "32")
test "comments":
const data = """
# This is a comment
Name: John Doe
Age: 34
# A comment between records
# With multiple lines!
Name: Jane Doe
# A comment between fields
Age: 32
"""
let ss = newStringStream(data)
let records = toSeq(records(ss))
check(len(records) == 2)
check(records[0]["Name"] == "John Doe")
check(records[0]["Age"] == "34")
check(records[1]["Name"] == "Jane Doe")
check(records[1]["Age"] == "32")
test "only initial whitespace skipped from values":
const data = """
Name: John Doe
Age: 34
Name: Jane Doe
Age: 32
"""
let ss = newStringStream(data)
let records = toSeq(records(ss))
check(len(records) == 2)
check(records[0]["Name"] == " John Doe")
check(records[0]["Age"] == " 34")
check(records[1]["Name"] == "Jane Doe")
check(records[1]["Age"] == "\t32")
test "trailing whitespace included in values":
const data =
"Name: John Doe \l" &
"Age: 34\t\l"
let ss = newStringStream(data)
let records = toSeq(records(ss))
check(len(records) == 1)
check(records[0]["Name"] == "John Doe ")
check(records[0]["Age"] == "34\t")
test "records with single field":
const data = """
Name: John Doe
Name: Jane Doe
Name: Foobar!
"""
let ss = newStringStream(data)
let records = toSeq(records(ss))
check(len(records) == 3)
check(len(records[0]) == 1)
check(records[0]["Name"] == "John Doe")
check(len(records[1]) == 1)
check(records[1]["Name"] == "Jane Doe")
check(len(records[2]) == 1)
check(records[2]["Name"] == "Foobar!")
test "parse error if colon missing":
let ss = newStringStream("Name\nAge: 34\n")
expect(ParseError):
discard toSeq(records(ss))
test "parse error if invalid label":
let ss = newStringStream("Name: John Doe\nFoo-bar: 111")
expect(ParseError):
discard toSeq(records(ss))
test "label can start with %":
let ss = newStringStream("%rec: Entry\n")
let records = toSeq(records(ss))
check(len(records) == 1)
check(len(records[0]) == 1)
let fields = toSeq(items(records[0]))
check(fields[0].label == "%rec")
check(fields[0].value == "Entry")
test "field must be terminated by newline":
let ss = newStringStream("%rec: Entry\n%type: Id int")
expect(ParseError):
discard toSeq(records(ss))
test "multiple fields with same label":
const data = """
Name: John Doe
Age: 34
Email: john@doe.me
Email: john.doe@foobar.com
Name: Jane Doe
Age: 32
Email: jane@doe.me
"""
var ss = newStringStream(data)
var emails: seq[string] = @[]
for record in records(ss):
for label, value in record:
if label == "Email":
add(emails, value)
check(len(emails) == 3)
check(emails[0] == "john@doe.me")
check(emails[1] == "john.doe@foobar.com")
check(emails[2] == "jane@doe.me")
suite "misc":
test "record items iterator":
const data = """
Name: John Doe
Age: 34
Name: Jane Doe
Age: 32
"""
var ss = newStringStream(data)
var fields: seq[Field] = @[]
for record in records(ss):
for field in record:
add(fields, field)
check(len(fields) == 4)
check(fields[0].label == "Name")
check(fields[0].value == "John Doe")
check(fields[1].label == "Age")
check(fields[1].value == "34")
check(fields[2].label == "Name")
check(fields[2].value == "Jane Doe")
check(fields[3].label == "Age")
check(fields[3].value == "32")
test "record pairs iterator":
const data = """
Name: John Doe
Age: 34
Name: Jane Doe
Age: 32
"""
var ss = newStringStream(data)
var results: seq[string] = @[]
for record in records(ss):
for label, value in record:
add(results, label)
add(results, value)
check(len(results) == 8)
check(results[0] == "Name")
check(results[1] == "John Doe")
check(results[2] == "Age")
check(results[3] == "34")
check(results[4] == "Name")
check(results[5] == "Jane Doe")
check(results[6] == "Age")
check(results[7] == "32")
test "hasField":
const data = """
Name: John Doe
Age: 34
Name: Jane Doe
Age: 32
Email: jane@doe.me
"""
var ss = newStringStream(data)
var records = toSeq(records(ss))
check(len(records) == 2)
check(not hasField(records[0], "Email"))
check(hasField(records[1], "Email"))
test "contains":
const data = """
Name: John Doe
Age: 34
Name: Jane Doe
Age: 32
Email: jane@doe.me
"""
var ss = newStringStream(data)
var records = toSeq(records(ss))
check(len(records) == 2)
check("Email" notin records[0])
check("Email" in records[1])
suite "recordset":
test "basics":
const data = """
%rec: Entry
%doc: docs for Entry
"""
var ss = newStringStream(data)
var records = toSeq(records(ss))
var rset = newRecordSet(records[0])
check(rset.kind == "Entry")
check(rset.doc == "docs for Entry")
test "descriptor skipped when iterating records":
const data = """
%rec: Entry
%doc: docs for Entry
Name: John
Name: Jane
Name: Bill
"""
var ss = newStringStream(data)
var records = toSeq(recordsInSet(ss, "Entry"))
check(len(records) == 3)
check(records[0]["Name"] == "John")
check(records[1]["Name"] == "Jane")
check(records[2]["Name"] == "Bill")
test "mandatory does not raise if all present":
const data = """
%rec: Entry
%mandatory: Name Age
Name: John
Age: 0
Name: Jane
Age: 0
Name: Bill
Age: 0
"""
var ss = newStringStream(data)
var records = toSeq(recordsInSet(ss, "Entry"))
check(len(records) == 3)
test "mandatory raises if something is missing":
const data = """
%rec: Entry
%mandatory: Name Age
Name: John
Age: 0
Name: Jane
Name: Bill
Age: 0
"""
var ss = newStringStream(data)
expect(IntegrityError):
discard toSeq(recordsInSet(ss, "Entry"))
test "prohibit does not fail if fields not present":
const data = """
%rec: Entry
%prohibit: result
Name: John
Name: Jane
Name: Bill
"""
var ss = newStringStream(data)
var records = toSeq(recordsInSet(ss, "Entry"))
check(len(records) == 3)
test "prohibit fails if fields present":
const data = """
%rec: Entry
%prohibit: result
Name: John
Name: Jane
Name: Bill
result: 100
"""
var ss = newStringStream(data)
expect(IntegrityError):
discard toSeq(recordsInSet(ss, "Entry"))
test "allowed allows allowed fields":
const data = """
%rec: Entry
%allowed: Name Age Address
Name: John
Name: Jane
Age: 29
Name: Bill
Age: 56
Address: 10 Foobar Way
"""
var ss = newStringStream(data)
var records = toSeq(recordsInSet(ss, "Entry"))
check(len(records) == 3)
test "allowed does not allow undeclared fields":
const data = """
%rec: Entry
%allowed: Name Age Address
Name: John
Name: Jane
Age: 29
Phone: 12345
Name: Bill
Age: 56
Address: 10 Foobar Way
"""
var ss = newStringStream(data)
expect(IntegrityError):
discard toSeq(recordsInSet(ss, "Entry"))
test "mandatory and prohibit for same field fails":
const data1 = """
%rec: Entry
%mandatory: Name
%prohibit: Name
"""
var ss = newStringStream(data1)
var records = toSeq(records(ss))
expect(Exception):
discard newRecordSet(records[0])
const data2 = """
%rec: Entry
%prohibit: Name
%mandatory: Name
"""
ss = newStringStream(data2)
records = toSeq(records(ss))
expect(Exception):
discard newRecordSet(records[0])
test "allowed and prohibit for same field fails":
const data1 = """
%rec: Entry
%allowed: Name
%prohibit: Name
"""
var ss = newStringStream(data1)
var records = toSeq(records(ss))
expect(Exception):
discard newRecordSet(records[0])
const data2 = """
%rec: Entry
%prohibit: Name
%allowed: Name
"""
ss = newStringStream(data2)
records = toSeq(records(ss))
expect(Exception):
discard newRecordSet(records[0])
suite "type basics: integers":
const prologue = """
%rec: Entry
%type: Value int
"""
test "valid integers":
const values = ["0", "1", "123456789", "987654321", "-123456789",
"-987654321", "-0"]
for value in values:
let data = prologue & "Value: " & value & "\n"
discard toSeq(recordsInSet(newStringStream(data), "Entry"))
test "invalid integers":
const values = ["0.0", "foobar", "01", "1-"]
for value in values:
let data = prologue & "Value: " & value & "\n"
expect(Exception):
discard toSeq(recordsInSet(newStringStream(data), "Entry"))
|