| ... | @@ -0,0 +1,867 @@ |
| 1 | const std = @import("std"); |
| 2 | const pek = @import("pek"); |
| 3 | const expect = @import("expect").expect; |
| 4 | |
| 5 | test { |
| 6 | std.testing.refAllDeclsRecursive(pek); |
| 7 | } |
| 8 | |
| 9 | test "document" { |
| 10 | const alloc = std.testing.allocator; |
| 11 | var builder = std.ArrayList(u8).init(alloc); |
| 12 | defer builder.deinit(); |
| 13 | const doc = comptime pek.parse( |
| 14 | \\html[lang="en"]( |
| 15 | \\ head( |
| 16 | \\ title("Pek Example") |
| 17 | \\ meta[charset="UTF-8"] |
| 18 | \\ meta[name="viewport" content="width=device-width,initial-scale=1"] |
| 19 | \\ ) |
| 20 | \\ body( |
| 21 | \\ h1("Pek Example") |
| 22 | \\ hr |
| 23 | \\ p("This is an example HTML document written in "a[href="https://github.com/nektro/zig-pek"]("Pek")".") |
| 24 | \\ ) |
| 25 | \\) |
| 26 | ); |
| 27 | try pek.compile( |
| 28 | @This(), |
| 29 | alloc, |
| 30 | builder.writer(), |
| 31 | doc, |
| 32 | .{}, |
| 33 | ); |
| 34 | try expect(builder.items).toEqualString( |
| 35 | \\<!DOCTYPE html> |
| 36 | \\<html lang="en"> |
| 37 | \\ <head> |
| 38 | \\ <title>Pek Example</title> |
| 39 | \\ <meta charset="UTF-8" /> |
| 40 | \\ <meta name="viewport" content="width=device-width,initial-scale=1" /> |
| 41 | \\ </head> |
| 42 | \\ <body> |
| 43 | \\ <h1>Pek Example</h1> |
| 44 | \\ <hr /> |
| 45 | \\ <p>This is an example HTML document written in <a href="https://github.com/nektro/zig-pek">Pek</a>.</p> |
| 46 | \\ </body> |
| 47 | \\</html> |
| 48 | \\ |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | test "fragment" { |
| 53 | const alloc = std.testing.allocator; |
| 54 | var builder = std.ArrayList(u8).init(alloc); |
| 55 | defer builder.deinit(); |
| 56 | const doc = comptime pek.parse( |
| 57 | \\body( |
| 58 | \\ h1("Pek Example") |
| 59 | \\ hr |
| 60 | \\ p("This is an example HTML document written in "a[href="https://github.com/nektro/zig-pek"]("Pek")".") |
| 61 | \\) |
| 62 | ); |
| 63 | try pek.compileInner( |
| 64 | alloc, |
| 65 | builder.writer(), |
| 66 | doc, |
| 67 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 68 | .{}, |
| 69 | ); |
| 70 | try expect(builder.items).toEqualString( |
| 71 | \\<body> |
| 72 | \\ <h1>Pek Example</h1> |
| 73 | \\ <hr /> |
| 74 | \\ <p>This is an example HTML document written in <a href="https://github.com/nektro/zig-pek">Pek</a>.</p> |
| 75 | \\</body> |
| 76 | \\ |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | test "if: basic" { |
| 81 | const alloc = std.testing.allocator; |
| 82 | var builder = std.ArrayList(u8).init(alloc); |
| 83 | defer builder.deinit(); |
| 84 | const doc = comptime pek.parse( |
| 85 | \\body( |
| 86 | \\ h1("Pek Example") |
| 87 | \\ hr |
| 88 | \\ {#if foo} |
| 89 | \\ p("This is an example HTML document written in "a[href="https://github.com/nektro/zig-pek"]("Pek")".") |
| 90 | \\ /if/ |
| 91 | \\ {#if bar} |
| 92 | \\ p("This will show up because "code("bar")" is true instead.") |
| 93 | \\ /if/ |
| 94 | \\) |
| 95 | ); |
| 96 | try pek.compileInner( |
| 97 | alloc, |
| 98 | builder.writer(), |
| 99 | doc, |
| 100 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 101 | .{ .foo = false, .bar = true }, |
| 102 | ); |
| 103 | try expect(builder.items).toEqualString( |
| 104 | \\<body> |
| 105 | \\ <h1>Pek Example</h1> |
| 106 | \\ <hr /> |
| 107 | \\ <p>This will show up because <code>bar</code> is true instead.</p> |
| 108 | \\</body> |
| 109 | \\ |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | // if else |
| 114 | test "if else + field access" { |
| 115 | const alloc = std.testing.allocator; |
| 116 | var builder = std.ArrayList(u8).init(alloc); |
| 117 | defer builder.deinit(); |
| 118 | const doc = comptime pek.parse( |
| 119 | \\body( |
| 120 | \\ {#if bar.qux} |
| 121 | \\ p("This will show up because "code("bar")" is true.") |
| 122 | \\ <else> |
| 123 | \\ p("This will show up because "code("bar")" is false.") |
| 124 | \\ /if/ |
| 125 | \\) |
| 126 | ); |
| 127 | try pek.compileInner( |
| 128 | alloc, |
| 129 | builder.writer(), |
| 130 | doc, |
| 131 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 132 | .{ .bar = .{ .qux = false } }, |
| 133 | ); |
| 134 | try expect(builder.items).toEqualString( |
| 135 | \\<body> |
| 136 | \\ <p>This will show up because <code>bar</code> is false.</p> |
| 137 | \\</body> |
| 138 | \\ |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | // ifnot |
| 143 | test { |
| 144 | const alloc = std.testing.allocator; |
| 145 | var builder = std.ArrayList(u8).init(alloc); |
| 146 | defer builder.deinit(); |
| 147 | const doc = comptime pek.parse( |
| 148 | \\body( |
| 149 | \\ {#ifnot bar} |
| 150 | \\ p("This will show up because "code("bar")" is false.") |
| 151 | \\ <else> |
| 152 | \\ p("This will show up because "code("bar")" is true.") |
| 153 | \\ /ifnot/ |
| 154 | \\) |
| 155 | ); |
| 156 | try pek.compileInner( |
| 157 | alloc, |
| 158 | builder.writer(), |
| 159 | doc, |
| 160 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 161 | .{ .bar = true }, |
| 162 | ); |
| 163 | try expect(builder.items).toEqualString( |
| 164 | \\<body> |
| 165 | \\ <p>This will show up because <code>bar</code> is true.</p> |
| 166 | \\</body> |
| 167 | \\ |
| 168 | ); |
| 169 | } |
| 170 | test { |
| 171 | const alloc = std.testing.allocator; |
| 172 | var builder = std.ArrayList(u8).init(alloc); |
| 173 | defer builder.deinit(); |
| 174 | const doc = comptime pek.parse( |
| 175 | \\body( |
| 176 | \\ {#ifnot bar} |
| 177 | \\ p("This will show up because "code("bar")" is false.") |
| 178 | \\ <else> |
| 179 | \\ p("This will show up because "code("bar")" is true.") |
| 180 | \\ /ifnot/ |
| 181 | \\) |
| 182 | ); |
| 183 | try pek.compileInner( |
| 184 | alloc, |
| 185 | builder.writer(), |
| 186 | doc, |
| 187 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 188 | .{ .bar = false }, |
| 189 | ); |
| 190 | try expect(builder.items).toEqualString( |
| 191 | \\<body> |
| 192 | \\ <p>This will show up because <code>bar</code> is false.</p> |
| 193 | \\</body> |
| 194 | \\ |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | // ifequal |
| 199 | test { // bool |
| 200 | const alloc = std.testing.allocator; |
| 201 | var builder = std.ArrayList(u8).init(alloc); |
| 202 | defer builder.deinit(); |
| 203 | const doc = comptime pek.parse( |
| 204 | \\body( |
| 205 | \\ {#ifequal bar true} |
| 206 | \\ p("This will show up because "code("bar")" is true.") |
| 207 | \\ <else> |
| 208 | \\ p("This will show up because "code("bar")" is false.") |
| 209 | \\ /ifequal/ |
| 210 | \\) |
| 211 | ); |
| 212 | try pek.compileInner( |
| 213 | alloc, |
| 214 | builder.writer(), |
| 215 | doc, |
| 216 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 217 | .{ .bar = true }, |
| 218 | ); |
| 219 | try expect(builder.items).toEqualString( |
| 220 | \\<body> |
| 221 | \\ <p>This will show up because <code>bar</code> is true.</p> |
| 222 | \\</body> |
| 223 | \\ |
| 224 | ); |
| 225 | } |
| 226 | test { // bool false |
| 227 | const alloc = std.testing.allocator; |
| 228 | var builder = std.ArrayList(u8).init(alloc); |
| 229 | defer builder.deinit(); |
| 230 | const doc = comptime pek.parse( |
| 231 | \\body( |
| 232 | \\ {#ifequal bar true} |
| 233 | \\ p("This will show up because "code("bar")" is true.") |
| 234 | \\ <else> |
| 235 | \\ p("This will show up because "code("bar")" is false.") |
| 236 | \\ /ifequal/ |
| 237 | \\) |
| 238 | ); |
| 239 | try pek.compileInner( |
| 240 | alloc, |
| 241 | builder.writer(), |
| 242 | doc, |
| 243 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 244 | .{ .bar = false }, |
| 245 | ); |
| 246 | try expect(builder.items).toEqualString( |
| 247 | \\<body> |
| 248 | \\ <p>This will show up because <code>bar</code> is false.</p> |
| 249 | \\</body> |
| 250 | \\ |
| 251 | ); |
| 252 | } |
| 253 | test { // string |
| 254 | const alloc = std.testing.allocator; |
| 255 | var builder = std.ArrayList(u8).init(alloc); |
| 256 | defer builder.deinit(); |
| 257 | const doc = comptime pek.parse( |
| 258 | \\body( |
| 259 | \\ {#ifequal bar "foo"} |
| 260 | \\ p("This will show up because "code("bar")" is 'foo'.") |
| 261 | \\ <else> |
| 262 | \\ p("This will show up because "code("bar")" is 'qux'.") |
| 263 | \\ /ifequal/ |
| 264 | \\) |
| 265 | ); |
| 266 | try pek.compileInner( |
| 267 | alloc, |
| 268 | builder.writer(), |
| 269 | doc, |
| 270 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 271 | .{ .bar = @as([]const u8, "foo") }, |
| 272 | ); |
| 273 | try expect(builder.items).toEqualString( |
| 274 | \\<body> |
| 275 | \\ <p>This will show up because <code>bar</code> is &apos;foo&apos;.</p> |
| 276 | \\</body> |
| 277 | \\ |
| 278 | ); |
| 279 | } |
| 280 | test { // string false |
| 281 | const alloc = std.testing.allocator; |
| 282 | var builder = std.ArrayList(u8).init(alloc); |
| 283 | defer builder.deinit(); |
| 284 | const doc = comptime pek.parse( |
| 285 | \\body( |
| 286 | \\ {#ifequal bar "foo"} |
| 287 | \\ p("This will show up because "code("bar")" is 'foo'.") |
| 288 | \\ <else> |
| 289 | \\ p("This will show up because "code("bar")" is 'qux'.") |
| 290 | \\ /ifequal/ |
| 291 | \\) |
| 292 | ); |
| 293 | try pek.compileInner( |
| 294 | alloc, |
| 295 | builder.writer(), |
| 296 | doc, |
| 297 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 298 | .{ .bar = @as([]const u8, "qux") }, |
| 299 | ); |
| 300 | try expect(builder.items).toEqualString( |
| 301 | \\<body> |
| 302 | \\ <p>This will show up because <code>bar</code> is &apos;qux&apos;.</p> |
| 303 | \\</body> |
| 304 | \\ |
| 305 | ); |
| 306 | } |
| 307 | test { // enum |
| 308 | const alloc = std.testing.allocator; |
| 309 | var builder = std.ArrayList(u8).init(alloc); |
| 310 | defer builder.deinit(); |
| 311 | const doc = comptime pek.parse( |
| 312 | \\body( |
| 313 | \\ {#ifequal bar "foo"} |
| 314 | \\ p("This will show up because "code("bar")" is .foo.") |
| 315 | \\ <else> |
| 316 | \\ p("This will show up because "code("bar")" is .qux.") |
| 317 | \\ /ifequal/ |
| 318 | \\) |
| 319 | ); |
| 320 | try pek.compileInner( |
| 321 | alloc, |
| 322 | builder.writer(), |
| 323 | doc, |
| 324 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 325 | .{ .bar = @as(enum { foo, bar, qux }, .foo) }, |
| 326 | ); |
| 327 | try expect(builder.items).toEqualString( |
| 328 | \\<body> |
| 329 | \\ <p>This will show up because <code>bar</code> is .foo.</p> |
| 330 | \\</body> |
| 331 | \\ |
| 332 | ); |
| 333 | } |
| 334 | test { // enum false |
| 335 | const alloc = std.testing.allocator; |
| 336 | var builder = std.ArrayList(u8).init(alloc); |
| 337 | defer builder.deinit(); |
| 338 | const doc = comptime pek.parse( |
| 339 | \\body( |
| 340 | \\ {#ifequal bar "foo"} |
| 341 | \\ p("This will show up because "code("bar")" is .foo.") |
| 342 | \\ <else> |
| 343 | \\ p("This will show up because "code("bar")" is .qux.") |
| 344 | \\ /ifequal/ |
| 345 | \\) |
| 346 | ); |
| 347 | try pek.compileInner( |
| 348 | alloc, |
| 349 | builder.writer(), |
| 350 | doc, |
| 351 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 352 | .{ .bar = @as(enum { foo, bar, qux }, .qux) }, |
| 353 | ); |
| 354 | try expect(builder.items).toEqualString( |
| 355 | \\<body> |
| 356 | \\ <p>This will show up because <code>bar</code> is .qux.</p> |
| 357 | \\</body> |
| 358 | \\ |
| 359 | ); |
| 360 | } |
| 361 | |
| 362 | // ifnotequal |
| 363 | test { // bool |
| 364 | const alloc = std.testing.allocator; |
| 365 | var builder = std.ArrayList(u8).init(alloc); |
| 366 | defer builder.deinit(); |
| 367 | const doc = comptime pek.parse( |
| 368 | \\body( |
| 369 | \\ {#ifnotequal bar true} |
| 370 | \\ p("This will show up because "code("bar")" is false.") |
| 371 | \\ <else> |
| 372 | \\ p("This will show up because "code("bar")" is true.") |
| 373 | \\ /ifnotequal/ |
| 374 | \\) |
| 375 | ); |
| 376 | try pek.compileInner( |
| 377 | alloc, |
| 378 | builder.writer(), |
| 379 | doc, |
| 380 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 381 | .{ .bar = false }, |
| 382 | ); |
| 383 | try expect(builder.items).toEqualString( |
| 384 | \\<body> |
| 385 | \\ <p>This will show up because <code>bar</code> is false.</p> |
| 386 | \\</body> |
| 387 | \\ |
| 388 | ); |
| 389 | } |
| 390 | test { // bool false |
| 391 | const alloc = std.testing.allocator; |
| 392 | var builder = std.ArrayList(u8).init(alloc); |
| 393 | defer builder.deinit(); |
| 394 | const doc = comptime pek.parse( |
| 395 | \\body( |
| 396 | \\ {#ifnotequal bar true} |
| 397 | \\ p("This will show up because "code("bar")" is false.") |
| 398 | \\ <else> |
| 399 | \\ p("This will show up because "code("bar")" is true.") |
| 400 | \\ /ifnotequal/ |
| 401 | \\) |
| 402 | ); |
| 403 | try pek.compileInner( |
| 404 | alloc, |
| 405 | builder.writer(), |
| 406 | doc, |
| 407 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 408 | .{ .bar = true }, |
| 409 | ); |
| 410 | try expect(builder.items).toEqualString( |
| 411 | \\<body> |
| 412 | \\ <p>This will show up because <code>bar</code> is true.</p> |
| 413 | \\</body> |
| 414 | \\ |
| 415 | ); |
| 416 | } |
| 417 | test { // string |
| 418 | const alloc = std.testing.allocator; |
| 419 | var builder = std.ArrayList(u8).init(alloc); |
| 420 | defer builder.deinit(); |
| 421 | const doc = comptime pek.parse( |
| 422 | \\body( |
| 423 | \\ {#ifnotequal bar "foo"} |
| 424 | \\ p("This will show up because "code("bar")" is 'qux'.") |
| 425 | \\ <else> |
| 426 | \\ p("This will show up because "code("bar")" is 'foo'.") |
| 427 | \\ /ifnotequal/ |
| 428 | \\) |
| 429 | ); |
| 430 | try pek.compileInner( |
| 431 | alloc, |
| 432 | builder.writer(), |
| 433 | doc, |
| 434 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 435 | .{ .bar = @as([]const u8, "qux") }, |
| 436 | ); |
| 437 | try expect(builder.items).toEqualString( |
| 438 | \\<body> |
| 439 | \\ <p>This will show up because <code>bar</code> is &apos;qux&apos;.</p> |
| 440 | \\</body> |
| 441 | \\ |
| 442 | ); |
| 443 | } |
| 444 | test { // string false |
| 445 | const alloc = std.testing.allocator; |
| 446 | var builder = std.ArrayList(u8).init(alloc); |
| 447 | defer builder.deinit(); |
| 448 | const doc = comptime pek.parse( |
| 449 | \\body( |
| 450 | \\ {#ifnotequal bar "foo"} |
| 451 | \\ p("This will show up because "code("bar")" is 'qux'.") |
| 452 | \\ <else> |
| 453 | \\ p("This will show up because "code("bar")" is 'foo'.") |
| 454 | \\ /ifnotequal/ |
| 455 | \\) |
| 456 | ); |
| 457 | try pek.compileInner( |
| 458 | alloc, |
| 459 | builder.writer(), |
| 460 | doc, |
| 461 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 462 | .{ .bar = @as([]const u8, "foo") }, |
| 463 | ); |
| 464 | try expect(builder.items).toEqualString( |
| 465 | \\<body> |
| 466 | \\ <p>This will show up because <code>bar</code> is &apos;foo&apos;.</p> |
| 467 | \\</body> |
| 468 | \\ |
| 469 | ); |
| 470 | } |
| 471 | test { // enum |
| 472 | const alloc = std.testing.allocator; |
| 473 | var builder = std.ArrayList(u8).init(alloc); |
| 474 | defer builder.deinit(); |
| 475 | const doc = comptime pek.parse( |
| 476 | \\body( |
| 477 | \\ {#ifnotequal bar "foo"} |
| 478 | \\ p("This will show up because "code("bar")" is .qux.") |
| 479 | \\ <else> |
| 480 | \\ p("This will show up because "code("bar")" is .foo.") |
| 481 | \\ /ifnotequal/ |
| 482 | \\) |
| 483 | ); |
| 484 | try pek.compileInner( |
| 485 | alloc, |
| 486 | builder.writer(), |
| 487 | doc, |
| 488 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 489 | .{ .bar = @as(enum { foo, bar, qux }, .qux) }, |
| 490 | ); |
| 491 | try expect(builder.items).toEqualString( |
| 492 | \\<body> |
| 493 | \\ <p>This will show up because <code>bar</code> is .qux.</p> |
| 494 | \\</body> |
| 495 | \\ |
| 496 | ); |
| 497 | } |
| 498 | test { // enum false |
| 499 | const alloc = std.testing.allocator; |
| 500 | var builder = std.ArrayList(u8).init(alloc); |
| 501 | defer builder.deinit(); |
| 502 | const doc = comptime pek.parse( |
| 503 | \\body( |
| 504 | \\ {#ifnotequal bar "foo"} |
| 505 | \\ p("This will show up because "code("bar")" is .qux.") |
| 506 | \\ <else> |
| 507 | \\ p("This will show up because "code("bar")" is .foo.") |
| 508 | \\ /ifnotequal/ |
| 509 | \\) |
| 510 | ); |
| 511 | try pek.compileInner( |
| 512 | alloc, |
| 513 | builder.writer(), |
| 514 | doc, |
| 515 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 516 | .{ .bar = @as(enum { foo, bar, qux }, .foo) }, |
| 517 | ); |
| 518 | try expect(builder.items).toEqualString( |
| 519 | \\<body> |
| 520 | \\ <p>This will show up because <code>bar</code> is .foo.</p> |
| 521 | \\</body> |
| 522 | \\ |
| 523 | ); |
| 524 | } |
| 525 | |
| 526 | // replacement |
| 527 | test { |
| 528 | const alloc = std.testing.allocator; |
| 529 | var builder = std.ArrayList(u8).init(alloc); |
| 530 | defer builder.deinit(); |
| 531 | const doc = comptime pek.parse( |
| 532 | \\body( |
| 533 | \\ p("This text for this link is "a[href="https://github.com/nektro/zig-pek"]({text})".") |
| 534 | \\) |
| 535 | ); |
| 536 | try pek.compileInner( |
| 537 | alloc, |
| 538 | builder.writer(), |
| 539 | doc, |
| 540 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 541 | .{ .text = @as([]const u8, "dynamic") }, |
| 542 | ); |
| 543 | try expect(builder.items).toEqualString( |
| 544 | \\<body> |
| 545 | \\ <p>This text for this link is <a href="https://github.com/nektro/zig-pek">dynamic</a>.</p> |
| 546 | \\</body> |
| 547 | \\ |
| 548 | ); |
| 549 | } |
| 550 | test { |
| 551 | const alloc = std.testing.allocator; |
| 552 | var builder = std.ArrayList(u8).init(alloc); |
| 553 | defer builder.deinit(); |
| 554 | const doc = comptime pek.parse( |
| 555 | \\body( |
| 556 | \\ p("This url for this link is "a[href=({url})]("dynamic")".") |
| 557 | \\) |
| 558 | ); |
| 559 | try pek.compileInner( |
| 560 | alloc, |
| 561 | builder.writer(), |
| 562 | doc, |
| 563 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 564 | .{ .url = @as([]const u8, "https://github.com/nektro/zig-pek") }, |
| 565 | ); |
| 566 | try expect(builder.items).toEqualString( |
| 567 | \\<body> |
| 568 | \\ <p>This url for this link is <a href="https:&sol;&sol;github.com&sol;nektro&sol;zig-pek">dynamic</a>.</p> |
| 569 | \\</body> |
| 570 | \\ |
| 571 | ); |
| 572 | } |
| 573 | test { |
| 574 | const alloc = std.testing.allocator; |
| 575 | var builder = std.ArrayList(u8).init(alloc); |
| 576 | defer builder.deinit(); |
| 577 | const doc = comptime pek.parse( |
| 578 | \\body( |
| 579 | \\ p("This host for this link is "a[href=("https://"{host}"/nektro/zig-pek")]("dynamic")".") |
| 580 | \\) |
| 581 | ); |
| 582 | try pek.compileInner( |
| 583 | alloc, |
| 584 | builder.writer(), |
| 585 | doc, |
| 586 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 587 | .{ .host = @as([]const u8, "github.com") }, |
| 588 | ); |
| 589 | try expect(builder.items).toEqualString( |
| 590 | \\<body> |
| 591 | \\ <p>This host for this link is <a href="https:&sol;&sol;github.com&sol;nektro&sol;zig-pek">dynamic</a>.</p> |
| 592 | \\</body> |
| 593 | \\ |
| 594 | ); |
| 595 | } |
| 596 | test { |
| 597 | const alloc = std.testing.allocator; |
| 598 | var builder = std.ArrayList(u8).init(alloc); |
| 599 | defer builder.deinit(); |
| 600 | const doc = comptime pek.parse( |
| 601 | \\body( |
| 602 | \\ p("This host for this link is "a[href=("https://"{host}"/nektro/zig-pek")]("dynamic")".") |
| 603 | \\) |
| 604 | ); |
| 605 | try pek.compileInner( |
| 606 | alloc, |
| 607 | builder.writer(), |
| 608 | doc, |
| 609 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 610 | .{ .host = "github.com".* }, |
| 611 | ); |
| 612 | try expect(builder.items).toEqualString( |
| 613 | \\<body> |
| 614 | \\ <p>This host for this link is <a href="https:&sol;&sol;github.com&sol;nektro&sol;zig-pek">dynamic</a>.</p> |
| 615 | \\</body> |
| 616 | \\ |
| 617 | ); |
| 618 | } |
| 619 | |
| 620 | // replacement with custom serializer |
| 621 | test { |
| 622 | const S = struct { |
| 623 | name: []const u8, |
| 624 | |
| 625 | pub fn format(s: @This(), comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 626 | _ = fmt; |
| 627 | _ = options; |
| 628 | try writer.writeAll(s.name); |
| 629 | try writer.writeAll(".com"); |
| 630 | } |
| 631 | }; |
| 632 | const alloc = std.testing.allocator; |
| 633 | var builder = std.ArrayList(u8).init(alloc); |
| 634 | defer builder.deinit(); |
| 635 | const doc = comptime pek.parse( |
| 636 | \\body( |
| 637 | \\ p("This host for this link is "a[href=("https://"{host}"/nektro/zig-pek")]("dynamic")".") |
| 638 | \\) |
| 639 | ); |
| 640 | try pek.compileInner( |
| 641 | alloc, |
| 642 | builder.writer(), |
| 643 | doc, |
| 644 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 645 | .{ .host = S{ .name = "github" } }, |
| 646 | ); |
| 647 | try expect(builder.items).toEqualString( |
| 648 | \\<body> |
| 649 | \\ <p>This host for this link is <a href="https:&sol;&sol;github.com&sol;nektro&sol;zig-pek">dynamic</a>.</p> |
| 650 | \\</body> |
| 651 | \\ |
| 652 | ); |
| 653 | } |
| 654 | test { |
| 655 | const S = struct { |
| 656 | name: []const u8, |
| 657 | |
| 658 | pub fn toString(s: @This(), alloc: std.mem.Allocator) ![]const u8 { |
| 659 | return std.fmt.allocPrint(alloc, "{s}.com", .{s.name}); |
| 660 | } |
| 661 | }; |
| 662 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 663 | defer arena.deinit(); |
| 664 | const alloc = arena.allocator(); |
| 665 | var builder = std.ArrayList(u8).init(alloc); |
| 666 | defer builder.deinit(); |
| 667 | const doc = comptime pek.parse( |
| 668 | \\body( |
| 669 | \\ p("This host for this link is "a[href=("https://"{host}"/nektro/zig-pek")]("dynamic")".") |
| 670 | \\) |
| 671 | ); |
| 672 | try pek.compileInner( |
| 673 | alloc, |
| 674 | builder.writer(), |
| 675 | doc, |
| 676 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 677 | .{ .host = S{ .name = "github" } }, |
| 678 | ); |
| 679 | try expect(builder.items).toEqualString( |
| 680 | \\<body> |
| 681 | \\ <p>This host for this link is <a href="https:&sol;&sol;github.com&sol;nektro&sol;zig-pek">dynamic</a>.</p> |
| 682 | \\</body> |
| 683 | \\ |
| 684 | ); |
| 685 | } |
| 686 | |
| 687 | // raw replacement |
| 688 | test { |
| 689 | const alloc = std.testing.allocator; |
| 690 | var builder = std.ArrayList(u8).init(alloc); |
| 691 | defer builder.deinit(); |
| 692 | const doc = comptime pek.parse( |
| 693 | \\body( |
| 694 | \\ p("This url for this link is "a[href=({{url}})]("dynamic but not escaped")".") |
| 695 | \\) |
| 696 | ); |
| 697 | try pek.compileInner( |
| 698 | alloc, |
| 699 | builder.writer(), |
| 700 | doc, |
| 701 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 702 | .{ .url = "https://github.com/nektro/zig-pek".* }, |
| 703 | ); |
| 704 | try expect(builder.items).toEqualString( |
| 705 | \\<body> |
| 706 | \\ <p>This url for this link is <a href="https://github.com/nektro/zig-pek">dynamic but not escaped</a>.</p> |
| 707 | \\</body> |
| 708 | \\ |
| 709 | ); |
| 710 | } |
| 711 | test { |
| 712 | const alloc = std.testing.allocator; |
| 713 | var builder = std.ArrayList(u8).init(alloc); |
| 714 | defer builder.deinit(); |
| 715 | const doc = comptime pek.parse( |
| 716 | \\body( |
| 717 | \\ p("Raw replacements can also "{{foo}}".") |
| 718 | \\) |
| 719 | ); |
| 720 | try pek.compileInner( |
| 721 | alloc, |
| 722 | builder.writer(), |
| 723 | doc, |
| 724 | .{ .Ctx = @This(), .indent = 0, .flag1 = false }, |
| 725 | .{ .foo = "contain HTML such as <button>this</button> as an escape hatch when you know content is safe".* }, |
| 726 | ); |
| 727 | try expect(builder.items).toEqualString( |
| 728 | \\<body> |
| 729 | \\ <p>Raw replacements can also contain HTML such as <button>this</button> as an escape hatch when you know content is safe.</p> |
| 730 | \\</body> |
| 731 | \\ |
| 732 | ); |
| 733 | } |
| 734 | |
| 735 | // custom function |
| 736 | test { |
| 737 | const C = struct { |
| 738 | pub fn pek__input_text(alloc: std.mem.Allocator, writer: pek.Writer, comptime opts: pek.DoOptions, args: struct { []const u8, []const u8, []const u8, []const u8 }) !void { |
| 739 | const tmpl = comptime pek.parse( |
| 740 | \\label( |
| 741 | \\ div({label}) |
| 742 | \\ textarea[type="text" required="" name=({name}) placeholder=({placeholder})]({value}) |
| 743 | \\) |
| 744 | ); |
| 745 | try pek.compileInner(alloc, writer, tmpl, opts, .{ |
| 746 | .name = args[0], |
| 747 | .label = args[1], |
| 748 | .value = args[2], |
| 749 | .placeholder = args[3], |
| 750 | }); |
| 751 | } |
| 752 | }; |
| 753 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 754 | defer arena.deinit(); |
| 755 | const alloc = arena.allocator(); |
| 756 | var builder = std.ArrayList(u8).init(alloc); |
| 757 | defer builder.deinit(); |
| 758 | var person: struct { name: []const u8 } = .{ .name = "meghan" }; |
| 759 | _ = &person; |
| 760 | const doc = comptime pek.parse( |
| 761 | \\body( |
| 762 | \\ form( |
| 763 | \\ {##input_text "name" "Name" prev.name ""} |
| 764 | \\ button("Submit ▶") |
| 765 | \\ ) |
| 766 | \\) |
| 767 | ); |
| 768 | try pek.compileInner( |
| 769 | alloc, |
| 770 | builder.writer(), |
| 771 | doc, |
| 772 | .{ .Ctx = C, .indent = 0, .flag1 = false }, |
| 773 | .{ .prev = person }, |
| 774 | ); |
| 775 | try expect(builder.items).toEqualString( |
| 776 | \\<body> |
| 777 | \\ <form><label> |
| 778 | \\ <div>Name</div> |
| 779 | \\ <textarea type="text" required="" name="name" placeholder="">meghan</textarea> |
| 780 | \\ </label> |
| 781 | \\<button>Submit ▶</button></form> |
| 782 | \\</body> |
| 783 | \\ |
| 784 | ); |
| 785 | } |
| 786 | |
| 787 | // custom bool function |
| 788 | test { |
| 789 | const S = struct { |
| 790 | id: u64, |
| 791 | }; |
| 792 | const C = struct { |
| 793 | pub fn pek_is_admin(alloc: std.mem.Allocator, user: S) !bool { |
| 794 | _ = alloc; |
| 795 | return user.id < 10; |
| 796 | } |
| 797 | }; |
| 798 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 799 | defer arena.deinit(); |
| 800 | const alloc = arena.allocator(); |
| 801 | var builder = std.ArrayList(u8).init(alloc); |
| 802 | defer builder.deinit(); |
| 803 | var person: S = .{ .id = 0 }; |
| 804 | _ = &person; |
| 805 | const doc = comptime pek.parse( |
| 806 | \\body( |
| 807 | \\ {#if#is_admin user} |
| 808 | \\ p("only an admin user can see this") |
| 809 | \\ <else> |
| 810 | \\ p("unauthorized users will see this") |
| 811 | \\ /if/ |
| 812 | \\) |
| 813 | ); |
| 814 | try pek.compileInner( |
| 815 | alloc, |
| 816 | builder.writer(), |
| 817 | doc, |
| 818 | .{ .Ctx = C, .indent = 0, .flag1 = false }, |
| 819 | .{ .user = person }, |
| 820 | ); |
| 821 | try expect(builder.items).toEqualString( |
| 822 | \\<body> |
| 823 | \\ <p>only an admin user can see this</p> |
| 824 | \\</body> |
| 825 | \\ |
| 826 | ); |
| 827 | } |
| 828 | test { |
| 829 | const S = struct { |
| 830 | id: u64, |
| 831 | }; |
| 832 | const C = struct { |
| 833 | pub fn pek_is_admin(alloc: std.mem.Allocator, user: S) !bool { |
| 834 | _ = alloc; |
| 835 | return user.id < 10; |
| 836 | } |
| 837 | }; |
| 838 | var arena = std.heap.ArenaAllocator.init(std.testing.allocator); |
| 839 | defer arena.deinit(); |
| 840 | const alloc = arena.allocator(); |
| 841 | var builder = std.ArrayList(u8).init(alloc); |
| 842 | defer builder.deinit(); |
| 843 | var person: S = .{ .id = 25 }; |
| 844 | _ = &person; |
| 845 | const doc = comptime pek.parse( |
| 846 | \\body( |
| 847 | \\ {#if#is_admin user} |
| 848 | \\ p("only an admin user can see this") |
| 849 | \\ <else> |
| 850 | \\ p("unauthorized users will see this") |
| 851 | \\ /if/ |
| 852 | \\) |
| 853 | ); |
| 854 | try pek.compileInner( |
| 855 | alloc, |
| 856 | builder.writer(), |
| 857 | doc, |
| 858 | .{ .Ctx = C, .indent = 0, .flag1 = false }, |
| 859 | .{ .user = person }, |
| 860 | ); |
| 861 | try expect(builder.items).toEqualString( |
| 862 | \\<body> |
| 863 | \\ <p>unauthorized users will see this</p> |
| 864 | \\</body> |
| 865 | \\ |
| 866 | ); |
| 867 | } |