-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.zig
178 lines (167 loc) · 4.26 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
const talloc = testing.allocator;
const example_mods = [_]type{
@import("gen_1"),
@import("gen_2"),
@import("gen_3"),
@import("gen_4"),
@import("gen_5"),
@import("gen_6"),
@import("gen_7"),
@import("gen_8"),
};
const example_fmts = [_]struct { []const u8, Whitespace }{
.{
\\[{"a":{"b":[{"key":"value"},{}]}}]
,
.minified,
},
.{
\\[
\\ {
\\ "a": {
\\ "c": {
\\ "d": 1
\\ }
\\ }
\\ },
\\ {
\\ "a": {},
\\ "b": "d"
\\ },
\\ {
\\ "a": {},
\\ "c": 1,
\\ "d": 1.1e0,
\\ "e": true,
\\ "f": false,
\\ "g": "foo",
\\ "h": [
\\ {},
\\ {
\\ "a": 1
\\ }
\\ ]
\\ }
\\]
,
.indent_2,
},
.{
\\{"a":{"b":{"key":"value"}}}
,
.minified,
},
.{
\\{"a":[]}
,
.minified,
},
.{ // these become std.json.Value
\\[1,"a"]
,
.minified,
},
.{
\\[{"a":1},{"b":"c"}]
,
.minified,
},
.{
\\[{},{"a":1}]
,
.minified,
},
.{
\\[1,null]
,
.minified,
},
};
fn parseExample(comptime i: usize) !void {
const path_key = std.fmt.comptimePrint("path_{}", .{i});
const f = try std.fs.cwd().openFile(@field(build_options, path_key), .{});
defer f.close();
var jr = json.reader(talloc, f.reader());
defer jr.deinit();
// const schema_path_key = std.fmt.comptimePrint("schema_path_{}", .{i});
// std.debug.print("{s}\n", .{@field(build_options, schema_path_key)});
const parsed = try json.parseFromTokenSource(example_mods[i - 1].Root, talloc, &jr, .{});
defer parsed.deinit();
try f.seekTo(0);
const src = try f.readToEndAlloc(talloc, 1024);
defer talloc.free(src);
// std.debug.print("{s}: {s}\n", .{ path_key, src });
// std.debug.print("{}\n", .{JsonFmt(example_mods[i - 1].Root){ .t = parsed.value }});
try testing.expectFmt(
example_fmts[i - 1][0],
"{}",
.{JsonFmt(example_mods[i - 1].Root){ .t = parsed.value, .ws = example_fmts[i - 1][1] }},
);
}
const Whitespace = std.meta.FieldType(std.json.StringifyOptions, .whitespace);
fn JsonFmt(comptime T: type) type {
return struct {
t: T,
ws: Whitespace = .minified,
pub fn format(f: @This(), comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
_ = options;
try json.stringify(f.t, .{ .whitespace = f.ws, .emit_null_optional_fields = false }, writer);
}
};
}
test "parse examples" {
try parseExample(1);
try parseExample(2);
try parseExample(3);
try parseExample(4);
try parseExample(5);
try parseExample(6);
try parseExample(7);
try parseExample(8);
}
test "unions" {
const f = try std.fs.cwd().openFile(build_options.schema_path_6, .{});
defer f.close();
const s = try f.readToEndAlloc(talloc, 1024);
defer talloc.free(s);
try testing.expectEqualStrings(
\\pub const Root = []const union(enum) {
\\ a: i64,
\\ b: []const u8,
\\};
\\
\\const std = @import("std");
\\
, s);
}
test "optional instead of union with null" {
const f = try std.fs.cwd().openFile(build_options.schema_path_7, .{});
defer f.close();
const s = try f.readToEndAlloc(talloc, 1024);
defer talloc.free(s);
try testing.expectEqualStrings(
\\pub const Root = []const struct {
\\ a: ?i64,
\\};
\\
\\const std = @import("std");
\\
, s);
}
test "optional 2" {
const f = try std.fs.cwd().openFile(build_options.schema_path_8, .{});
defer f.close();
const s = try f.readToEndAlloc(talloc, 1024);
defer talloc.free(s);
try testing.expectEqualStrings(
\\pub const Root = []const ?i64;
\\
\\const std = @import("std");
\\
, s);
}
const std = @import("std");
const testing = std.testing;
const json = std.json;
const build_options = @import("build-options");