-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.zig
357 lines (306 loc) · 10.1 KB
/
tests.zig
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
test "sx.Reader" {
const str =
\\(test 1 (1 2)
\\ 2 -3 ( "
\\" 4 5 6)
\\ () a b c
\\)
\\
\\
\\ true
\\ 0x20
\\ 0.35
\\ unsigned
\\ "hello world"
\\ 1 2 3 4
\\ "hello world 2"
\\ 1 2 3
\\ nil 1234
\\ x y 1
\\ (a asdf)
\\ (b 1)
\\ (c 2)
\\ (d multiple-words)
\\
;
var stream = std.io.fixedBufferStream(str);
var reader = sx.reader(std.testing.allocator, stream.reader().any());
defer reader.deinit();
var buf: [4096]u8 = undefined;
var buf_stream = std.io.fixedBufferStream(&buf);
var ctx = try reader.token_context();
try ctx.print_for_string(str, buf_stream.writer(), 80);
try expectEqualStrings(
\\ 1 |(test 1 (1 2)
\\ |^^^^^
\\ 2 | 2 -3 ( "
\\
, buf_stream.getWritten());
buf_stream.reset();
try expectEqual(try reader.expression("asdf"), false);
try reader.require_expression("test");
try expectEqual(try reader.open(), false);
try expectEqual(try reader.close(), false);
try expectEqual(try reader.require_any_unsigned(usize, 10), @as(usize, 1));
try expectEqualStrings(try reader.require_any_expression(), "1");
try expectEqual(try reader.any_expression(), null);
try reader.ignore_remaining_expression();
try expectEqual(try reader.require_any_unsigned(usize, 0), @as(usize, 2));
try expectEqual(try reader.require_any_int(i8, 0), @as(i8, -3));
try reader.require_open();
ctx = try reader.token_context();
try ctx.print_for_string(str, buf_stream.writer(), 80);
try expectEqualStrings(
\\ 1 |(test 1 (1 2)
\\ 2 | 2 -3 ( "
\\ | ^^^
\\ 3 |" 4 5 6)
\\ |^
\\ 4 | () a b c
\\
, buf_stream.getWritten());
buf_stream.reset();
try reader.require_string(" \n");
try expectEqual(try reader.string("x"), false);
try reader.require_string("4");
try expectEqual(try reader.require_any_float(f32), @as(f32, 5));
try expectEqualStrings(try reader.require_any_string(), "6");
try expectEqual(try reader.any_string(), null);
try expectEqual(try reader.any_float(f32), null);
try expectEqual(try reader.any_int(u12, 0), null);
try expectEqual(try reader.any_unsigned(u12, 0), null);
try reader.require_close();
try reader.require_open();
try reader.require_close();
try reader.ignore_remaining_expression();
ctx = try reader.token_context();
try ctx.print_for_string(str, buf_stream.writer(), 80);
try expectEqualStrings(
\\ 7 |
\\ 8 | true
\\ | ^^^^
\\ 9 | 0x20
\\
, buf_stream.getWritten());
buf_stream.reset();
const Ctx = struct {
pub fn type_name(comptime T: type) []const u8 {
const raw = @typeName(T);
if (std.mem.lastIndexOfScalar(u8, raw, '.')) |index| {
return raw[index + 1 ..];
}
return raw;
}
};
try expectEqual(true, try reader.require_object(std.testing.allocator, bool, Ctx));
try expectEqual(0x20, try reader.require_object(std.testing.allocator, u8, Ctx));
try expectEqual(0.35, try reader.require_object(std.testing.allocator, f64, Ctx));
try expectEqual(std.builtin.Signedness.unsigned, try reader.require_object(std.testing.allocator, std.builtin.Signedness, Ctx));
const xyz = try reader.require_object(std.testing.allocator, []const u8, Ctx);
defer std.testing.allocator.free(xyz);
try expectEqualStrings("hello world", xyz);
const slice = try reader.require_object(std.testing.allocator, []const u32, Ctx);
defer std.testing.allocator.free(slice);
try expectEqualSlices(u32, &.{ 1, 2, 3, 4 }, slice);
const ptr = try reader.require_object(std.testing.allocator, *const []const u8, Ctx);
defer std.testing.allocator.destroy(ptr);
defer std.testing.allocator.free(ptr.*);
try expectEqualStrings("hello world 2", ptr.*);
const arr = try reader.require_object(std.testing.allocator, [3]u4, Ctx);
try expectEqualSlices(u4, &.{ 1, 2, 3 }, &arr);
var opt = try reader.require_object(std.testing.allocator, ?u32, Ctx);
try expectEqual(null, opt);
opt = try reader.require_object(std.testing.allocator, ?u32, Ctx);
try expectEqual(1234, opt);
const U = union (enum) {
x,
y: u32
};
var u = try reader.require_object(std.testing.allocator, U, Ctx);
try expectEqual(.x, u);
u = try reader.require_object(std.testing.allocator, U, Ctx);
try expectEqual(@as(U, .{ .y = 1 }), u);
const MyEnum = enum {
abc,
multiple_words,
};
const MyStruct = struct {
a: []const u8 = "",
b: u8 = 0,
c: i64 = 0,
d: MyEnum = .abc,
};
const s = try reader.require_object(std.testing.allocator, MyStruct, Ctx);
defer std.testing.allocator.free(s.a);
try expectEqualStrings("asdf", s.a);
try expectEqual(1, s.b);
try expectEqual(2, s.c);
try expectEqual(.multiple_words, s.d);
try reader.require_done();
}
test "sx.Writer" {
const expected =
\\(box my-box
\\ (dimensions 4.3 7 14)
\\ (color red)
\\ (contents
\\ 42
\\ "Big Phil's To Do List:\n - paint it black\n - clean up around the house\n"
\\ "x y \""
\\ false
\\ 32
\\ 0.35
\\ unsigned
\\ "hello world"
\\ "hello world 2"
\\ 1
\\ 2
\\ 3
\\ 4
\\ 9
\\ 6
\\ 5
\\ nil
\\ 1234
\\ x
\\ y
\\ 1 (a asdf) (b 123) (c 12355) (d multiple-words))
\\)
;
var buf: [4096]u8 = undefined;
var buf_stream = std.io.fixedBufferStream(&buf);
var writer = sx.writer(std.testing.allocator, buf_stream.writer().any());
defer writer.deinit();
try writer.expression("box");
try writer.string("my-box");
writer.set_compact(false);
try writer.expression("dimensions");
try writer.float(4.3);
try writer.float(7);
try writer.float(14);
_ = try writer.close();
try writer.expression("color");
try writer.string("red");
writer.set_compact(false);
_ = try writer.close();
try writer.expression_expanded("contents");
try writer.int(42, 10);
try writer.string(
\\Big Phil's To Do List:
\\ - paint it black
\\ - clean up around the house
\\
);
try writer.print_value("x y \"", .{});
const Ctx = struct {};
try writer.object(false, Ctx);
try writer.object(@as(u8, 0x20), Ctx);
try writer.object(@as(f64, 0.35), Ctx);
try writer.object(std.builtin.Signedness.unsigned, Ctx);
const xyz: []const u8 = "hello world";
try writer.object(xyz, Ctx);
const ptr: *const []const u8 = &"hello world 2";
try writer.object(ptr, Ctx);
const slice: []const u32 = &.{ 1, 2, 3, 4 };
try writer.object(slice, Ctx);
try writer.object([_]u4 { 9, 6, 5 }, Ctx);
var opt: ?u32 = null;
try writer.object(opt, Ctx);
opt = 1234;
try writer.object(opt, Ctx);
const U = union (enum) {
x,
y: u32
};
var u: U = .x;
try writer.object(u, Ctx);
u = .{ .y = 1 };
try writer.object(u, Ctx);
writer.set_compact(true);
const MyEnum = enum {
abc,
multiple_words,
};
const MyStruct = struct {
a: []const u8 = "",
b: u8 = 0,
c: i64 = 0,
d: MyEnum = .abc,
};
try writer.object(MyStruct{
.a = "asdf",
.b = 123,
.c = 12355,
.d = .multiple_words,
}, Ctx);
writer.set_compact(false);
try writer.done();
try expectEqualStrings(expected, buf_stream.getWritten());
}
const Inline_Fields_Struct = struct {
a: []const u8 = "",
inline_items: []const []const u8 = &.{},
misc: u32 = 0,
multi: []const u32 = &.{},
};
const Inline_Fields_Ctx = struct {
pub const inline_fields = &.{ "a", "inline_items" };
};
test "read struct with inline fields" {
const str =
\\asdf abc 123
\\(multi 1)
\\(misc 5678)
\\(multi 7)
\\(multi 1234)
\\
;
var stream = std.io.fixedBufferStream(str);
var reader = sx.reader(std.testing.allocator, stream.reader().any());
defer reader.deinit();
const result = try reader.require_object(std.testing.allocator, Inline_Fields_Struct, Inline_Fields_Ctx);
defer std.testing.allocator.free(result.a);
defer std.testing.allocator.free(result.inline_items);
defer for(result.inline_items) |item| {
std.testing.allocator.free(item);
};
defer std.testing.allocator.free(result.multi);
try expectEqualStrings("asdf", result.a);
try expectEqual(2, result.inline_items.len);
try expectEqualStrings("abc", result.inline_items[0]);
try expectEqualStrings("123", result.inline_items[1]);
try expectEqual(5678, result.misc);
try expectEqual(3, result.multi.len);
try expectEqual(1, result.multi[0]);
try expectEqual(7, result.multi[1]);
try expectEqual(1234, result.multi[2]);
}
test "write struct with inline fields" {
const expected =
\\asdf
\\abc
\\123
\\(misc 5678)
\\(multi 1)
\\(multi 7)
\\(multi 1234)
;
const obj: Inline_Fields_Struct = .{
.a = "asdf",
.inline_items = &.{ "abc", "123" },
.misc = 5678,
.multi = &.{ 1, 7, 1234 },
};
var buf: [4096]u8 = undefined;
var buf_stream = std.io.fixedBufferStream(&buf);
var writer = sx.writer(std.testing.allocator, buf_stream.writer().any());
defer writer.deinit();
try writer.object(obj, Inline_Fields_Ctx);
try writer.done();
try expectEqualStrings(expected, buf_stream.getWritten());
}
const expectEqual = std.testing.expectEqual;
const expectEqualSlices = std.testing.expectEqualSlices;
const expectEqualStrings = std.testing.expectEqualStrings;
const sx = @import("sx");
const std = @import("std");