refactor: Improved the way sites are loaded.

This commit is contained in:
Spencer Brower
2026-07-12 22:34:52 -04:00
parent 4521960824
commit 9671a90726
9 changed files with 573 additions and 229 deletions
+28 -20
View File
@@ -2,13 +2,14 @@
#+test
package main
import "core:fmt"
import "core:testing"
import mustache "mustache"
import "mustache"
@(test)
test_simple_substitution :: proc(t: ^testing.T) {
data := map[string]string{"name" = "World"}
data := map[string]string {
"name" = "World",
}
result, err := mustache.render("Hello, {{name}}!", data)
testing.expect(t, err == nil)
testing.expect(t, result == "Hello, World!")
@@ -16,7 +17,9 @@ test_simple_substitution :: proc(t: ^testing.T) {
@(test)
test_bool_section :: proc(t: ^testing.T) {
data := map[string]bool{"show" = true}
data := map[string]bool {
"show" = true,
}
result, err := mustache.render("{{#show}}visible{{/show}}", data)
testing.expect(t, err == nil)
testing.expect(t, result == "visible")
@@ -24,7 +27,9 @@ test_bool_section :: proc(t: ^testing.T) {
@(test)
test_inverted_section :: proc(t: ^testing.T) {
data := map[string]bool{"show" = false}
data := map[string]bool {
"show" = false,
}
result, err := mustache.render("{{^show}}hidden{{/show}}", data)
testing.expect(t, err == nil)
testing.expect(t, result == "hidden")
@@ -32,7 +37,9 @@ test_inverted_section :: proc(t: ^testing.T) {
@(test)
test_unescaped :: proc(t: ^testing.T) {
data := map[string]string{"html" = "<b>bold</b>"}
data := map[string]string {
"html" = "<b>bold</b>",
}
result, err := mustache.render("{{{html}}}", data)
testing.expect(t, err == nil)
testing.expect(t, result == "<b>bold</b>")
@@ -45,7 +52,9 @@ test_array_iteration :: proc(t: ^testing.T) {
append(&items, map[string]string{"name" = "Alice"})
append(&items, map[string]string{"name" = "Bob"})
data := map[string][dynamic]map[string]string{"items" = items}
data := map[string][dynamic]map[string]string {
"items" = items,
}
result, err := mustache.render("{{#items}}{{name}} {{/items}}", data)
testing.expect(t, err == nil)
testing.expect(t, result == "Alice Bob ")
@@ -53,8 +62,12 @@ test_array_iteration :: proc(t: ^testing.T) {
@(test)
test_partial :: proc(t: ^testing.T) {
data := map[string]string{"name" = "Test"}
partials := map[string]string{"greeting" = "Hello, {{name}}!"}
data := map[string]string {
"name" = "Test",
}
partials := map[string]string {
"greeting" = "Hello, {{name}}!",
}
result, err := mustache.render("{{>greeting}}", data, partials)
testing.expect(t, err == nil)
testing.expect(t, result == "Hello, Test!")
@@ -62,34 +75,28 @@ test_partial :: proc(t: ^testing.T) {
@(test)
test_mixed_types :: proc(t: ^testing.T) {
data := map[string]any{
data := map[string]any {
"site_title" = "One Idiot Developer",
"has_date" = true,
"date" = "17 Jul 2025",
}
result, err := mustache.render(
"{{site_title}}: {{#has_date}}{{date}}{{/has_date}}",
data,
)
result, err := mustache.render("{{site_title}}: {{#has_date}}{{date}}{{/has_date}}", data)
testing.expect(t, err == nil)
testing.expect(t, result == "One Idiot Developer: 17 Jul 2025")
}
@(test)
test_nested_context :: proc(t: ^testing.T) {
page := map[string]string{
page := map[string]string {
"title" = "My Post",
}
data := map[string]any{
data := map[string]any {
"site_title" = "One Idiot Developer",
"page" = page,
}
result, err := mustache.render(
"{{site_title}}: {{#page}}{{title}}{{/page}}",
data,
)
result, err := mustache.render("{{site_title}}: {{#page}}{{title}}{{/page}}", data)
testing.expect(t, err == nil)
testing.expect(t, result == "One Idiot Developer: My Post")
}
@@ -104,3 +111,4 @@ test_layout :: proc(t: ^testing.T) {
testing.expect(t, err == nil)
testing.expect(t, result == "<html><body><p>Hello!</p></body></html>")
}