diff --git a/src/lib.zig b/src/lib.zig index e55a52b5f6e569460b44c59c3b9513de391c6e82..ae0beeb8cf888bd2fd973c69081032aa09bfab66 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -201,7 +201,7 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val } switch (comptime TI) { .bool => try doif(alloc, writer, body, bottom, data, ctx, opts, x), - .optional => try docap(alloc, writer, body, bottom, data, ctx, opts, x), + .optional => try doif(alloc, writer, body, bottom, data, ctx, opts, x != null), .int => try doif(alloc, writer, body, bottom, data, ctx, opts, x != 0), else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #if block", .{@typeName(T)})), } @@ -232,7 +232,7 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val } switch (comptime TI) { .bool => try doif(alloc, writer, body, bottom, data, ctx, opts, !x), - .optional => try docap(alloc, writer, body, bottom, data, ctx, opts, !x), + .optional => try doif(alloc, writer, body, bottom, data, ctx, opts, x == null), .int => try doif(alloc, writer, body, bottom, data, ctx, opts, x == 0), else => @compileError(std.fmt.comptimePrint("pek: unable to use '{s}' in an #ifnot block", .{@typeName(T)})), } @@ -512,14 +512,6 @@ fn doif(alloc: std.mem.Allocator, writer: anytype, comptime top: astgen.Value, c } } -fn 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 { - if (flag2) |_| { - try do(alloc, writer, top, data, ctx, opts); - } else { - try do(alloc, writer, bottom, data, ctx, opts); - } -} - fn isArrayOf(comptime T: type) fn (type) bool { const Closure = struct { pub fn trait(comptime C: type) bool { diff --git a/test.zig b/test.zig index 2fd9ef09a956a366647a8e9b4a5e69fbd50d48e8..fff8f2b8461f7c8e65a7c760ddd5c0b129315f9a 100644 --- a/test.zig +++ b/test.zig @@ -146,6 +146,41 @@ test "if: basic" { ); } +test "if: optional" { + const alloc = std.testing.allocator; + var builder = std.ArrayList(u8).init(alloc); + defer builder.deinit(); + const foo: ?u8 = null; + const bar: ?u8 = 24; + const doc = comptime pek.parse( + \\body( + \\ h1("Pek Example") + \\ hr + \\ {#if foo} + \\ p("This is an example HTML document written in "a[href="https://github.com/nektro/zig-pek"]("Pek")".") + \\ /if/ + \\ {#if bar} + \\ p("This will show up because "code("bar")" is non-null instead.") + \\ /if/ + \\) + ); + try pek.compileInner( + alloc, + builder.writer(), + doc, + .{ .Ctx = @This(), .indent = 0, .doindent = true, .doindent2 = true }, + .{ .foo = foo, .bar = bar }, + ); + try expect(builder.items).toEqualString( + \\ + \\

Pek Example

+ \\
+ \\

This will show up because bar is non-null instead.

+ \\ + \\ + ); +} + // if else test "if else + field access" { const alloc = std.testing.allocator; @@ -230,6 +265,35 @@ test { \\ ); } +// ifnot optional +test { + const alloc = std.testing.allocator; + var builder = std.ArrayList(u8).init(alloc); + defer builder.deinit(); + const foo: ?u8 = null; + const doc = comptime pek.parse( + \\body( + \\ {#ifnot foo} + \\ p("This will show up because "code("foo")" is null.") + \\ + \\ p("This will show up because "code("foo")" is non-null.") + \\ /ifnot/ + \\) + ); + try pek.compileInner( + alloc, + builder.writer(), + doc, + .{ .Ctx = @This(), .indent = 0, .doindent = true, .doindent2 = true }, + .{ .foo = foo }, + ); + try expect(builder.items).toEqualString( + \\ + \\

This will show up because foo is null.

+ \\ + \\ + ); +} // ifequal test { // bool