authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-15 17:08:39 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-02-15 17:08:39 -08:00
log73871a617d80b4750fd51f903bf1cf0a29ee8f4c
tree858882ac84a933848689f8428c1ece1ee79479a8
parent5358e659d76828a82d2fd277192e27a243296ff8

add test for {#if and {#ifnot with optionals


2 files changed, 66 insertions(+), 10 deletions(-)

src/lib.zig+2-10
...@@ -201,7 +201,7 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val...@@ -201,7 +201,7 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
201 }201 }
202 switch (comptime TI) {202 switch (comptime TI) {
203 .bool => try doif(alloc, writer, body, bottom, data, ctx, opts, x),203 .bool => try doif(alloc, writer, body, bottom, data, ctx, opts, x),
204 .optional => try docap(alloc, writer, body, bottom, data, ctx, opts, x),204 .optional => try doif(alloc, writer, body, bottom, data, ctx, opts, x != null),
205 .int => try doif(alloc, writer, body, bottom, data, ctx, opts, x != 0),205 .int => try doif(alloc, writer, body, bottom, data, ctx, opts, x != 0),
206 else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #if block", .{@typeName(T)})),206 else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #if block", .{@typeName(T)})),
207 }207 }
...@@ -232,7 +232,7 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val...@@ -232,7 +232,7 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
232 }232 }
233 switch (comptime TI) {233 switch (comptime TI) {
234 .bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),234 .bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x),
235 .optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x),235 .optional => try doif(alloc, writer, body, bottom, data, ctx, opts, x == null),
236 .int => try doif(alloc, writer, body, bottom, data, ctx, opts, x == 0),236 .int => try doif(alloc, writer, body, bottom, data, ctx, opts, x == 0),
237 else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #ifnot block", .{@typeName(T)})),237 else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #ifnot block", .{@typeName(T)})),
238 }238 }
...@@ -512,14 +512,6 @@ fn doif(alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, c...@@ -512,14 +512,6 @@ fn doif(alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, c
512 }512 }
513}513}
514514
515fn docap(alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, comptime bottom: astgen.Value, data: anytype, ctx: anytype, comptime opts: DoOptions, flag2: anytype) anyerror!void {
516 if (flag2) |_| {
517 try do(alloc, writer, top, data, ctx, opts);
518 } else {
519 try do(alloc, writer, bottom, data, ctx, opts);
520 }
521}
522
523fn isArrayOf(comptime T: type) fn (type) bool {515fn isArrayOf(comptime T: type) fn (type) bool {
524 const Closure = struct {516 const Closure = struct {
525 pub fn trait(comptime C: type) bool {517 pub fn trait(comptime C: type) bool {
test.zig+64
...@@ -146,6 +146,41 @@ test "if: basic" {...@@ -146,6 +146,41 @@ test "if: basic" {
146 );146 );
147}147}
148148
149test "if: optional" {
150 const alloc = std.testing.allocator;
151 var builder = std.ArrayList(u8).init(alloc);
152 defer builder.deinit();
153 const foo: ?u8 = null;
154 const bar: ?u8 = 24;
155 const doc = comptime pek.parse(
156 \\body(
157 \\ h1("Pek Example")
158 \\ hr
159 \\ {#if foo}
160 \\ p("This is an example HTML document written in "a[href="https://github.com/nektro/zig-pek"]("Pek")".")
161 \\ /if/
162 \\ {#if bar}
163 \\ p("This will show up because "code("bar")" is non-null instead.")
164 \\ /if/
165 \\)
166 );
167 try pek.compileInner(
168 alloc,
169 builder.writer(),
170 doc,
171 .{ .Ctx = @This(), .indent = 0, .doindent = true, .doindent2 = true },
172 .{ .foo = foo, .bar = bar },
173 );
174 try expect(builder.items).toEqualString(
175 \\<body>
176 \\ <h1>Pek Example</h1>
177 \\ <hr />
178 \\ <p>This will show up because <code>bar</code> is non-null instead.</p>
179 \\</body>
180 \\
181 );
182}
183
149// if else184// if else
150test "if else + field access" {185test "if else + field access" {
151 const alloc = std.testing.allocator;186 const alloc = std.testing.allocator;
...@@ -230,6 +265,35 @@ test {...@@ -230,6 +265,35 @@ test {
230 \\265 \\
231 );266 );
232}267}
268// ifnot optional
269test {
270 const alloc = std.testing.allocator;
271 var builder = std.ArrayList(u8).init(alloc);
272 defer builder.deinit();
273 const foo: ?u8 = null;
274 const doc = comptime pek.parse(
275 \\body(
276 \\ {#ifnot foo}
277 \\ p("This will show up because "code("foo")" is null.")
278 \\ <else>
279 \\ p("This will show up because "code("foo")" is non-null.")
280 \\ /ifnot/
281 \\)
282 );
283 try pek.compileInner(
284 alloc,
285 builder.writer(),
286 doc,
287 .{ .Ctx = @This(), .indent = 0, .doindent = true, .doindent2 = true },
288 .{ .foo = foo },
289 );
290 try expect(builder.items).toEqualString(
291 \\<body>
292 \\ <p>This will show up because <code>foo</code> is null.</p>
293 \\</body>
294 \\
295 );
296}
233297
234// ifequal298// ifequal
235test { // bool299test { // bool