2026-02-16 23:40:40 +00:00
|
|
|
package html
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2026-03-26 18:12:06 +00:00
|
|
|
func TestNestedLayout_PathChain_Good(t *testing.T) {
|
2026-02-16 23:40:40 +00:00
|
|
|
inner := NewLayout("HCF").H(Raw("inner h")).C(Raw("inner c")).F(Raw("inner f"))
|
|
|
|
|
outer := NewLayout("HLCRF").
|
|
|
|
|
H(Raw("header")).L(inner).C(Raw("main")).R(Raw("right")).F(Raw("footer"))
|
|
|
|
|
got := outer.Render(NewContext())
|
|
|
|
|
|
|
|
|
|
// Inner layout paths must be prefixed with parent block ID
|
|
|
|
|
for _, want := range []string{`data-block="L-0-H-0"`, `data-block="L-0-C-0"`, `data-block="L-0-F-0"`} {
|
2026-03-26 15:24:16 +00:00
|
|
|
if !containsText(got, want) {
|
2026-02-16 23:40:40 +00:00
|
|
|
t.Errorf("nested layout missing %q in:\n%s", want, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Outer layout must still have root-level paths
|
|
|
|
|
for _, want := range []string{`data-block="H-0"`, `data-block="C-0"`, `data-block="F-0"`} {
|
2026-03-26 15:24:16 +00:00
|
|
|
if !containsText(got, want) {
|
2026-02-16 23:40:40 +00:00
|
|
|
t.Errorf("outer layout missing %q in:\n%s", want, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 18:12:06 +00:00
|
|
|
func TestNestedLayout_DeepNesting_Ugly(t *testing.T) {
|
2026-02-16 23:40:40 +00:00
|
|
|
deepest := NewLayout("C").C(Raw("deep"))
|
|
|
|
|
middle := NewLayout("C").C(deepest)
|
|
|
|
|
outer := NewLayout("C").C(middle)
|
|
|
|
|
got := outer.Render(NewContext())
|
|
|
|
|
|
|
|
|
|
for _, want := range []string{`data-block="C-0"`, `data-block="C-0-C-0"`, `data-block="C-0-C-0-C-0"`} {
|
2026-03-26 15:24:16 +00:00
|
|
|
if !containsText(got, want) {
|
2026-02-16 23:40:40 +00:00
|
|
|
t.Errorf("deep nesting missing %q in:\n%s", want, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 04:55:58 +00:00
|
|
|
func TestBlockID_BuildsPath_Good(t *testing.T) {
|
2026-02-16 23:40:40 +00:00
|
|
|
tests := []struct {
|
|
|
|
|
path string
|
|
|
|
|
slot byte
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{"", 'H', "H-0"},
|
|
|
|
|
{"L-0-", 'C', "L-0-C-0"},
|
|
|
|
|
{"C-0-C-0-", 'C', "C-0-C-0-C-0"},
|
|
|
|
|
{"", 'F', "F-0"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
l := &Layout{path: tt.path}
|
|
|
|
|
got := l.blockID(tt.slot)
|
|
|
|
|
if got != tt.want {
|
|
|
|
|
t.Errorf("blockID(%q, %c) = %q, want %q", tt.path, tt.slot, got, tt.want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 04:55:58 +00:00
|
|
|
func TestParseBlockID_ExtractsSlots_Good(t *testing.T) {
|
2026-02-16 23:40:40 +00:00
|
|
|
tests := []struct {
|
|
|
|
|
id string
|
|
|
|
|
want []byte
|
|
|
|
|
}{
|
|
|
|
|
{"L-0-C-0", []byte{'L', 'C'}},
|
|
|
|
|
{"H-0", []byte{'H'}},
|
|
|
|
|
{"C-0-C-0-C-0", []byte{'C', 'C', 'C'}},
|
|
|
|
|
{"", nil},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
got := ParseBlockID(tt.id)
|
|
|
|
|
if len(got) != len(tt.want) {
|
|
|
|
|
t.Errorf("ParseBlockID(%q) = %v, want %v", tt.id, got, tt.want)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for i := range got {
|
|
|
|
|
if got[i] != tt.want[i] {
|
|
|
|
|
t.Errorf("ParseBlockID(%q)[%d] = %c, want %c", tt.id, i, got[i], tt.want[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-31 19:55:44 +00:00
|
|
|
|
|
|
|
|
func TestParseBlockID_InvalidInput_Good(t *testing.T) {
|
|
|
|
|
tests := []string{
|
|
|
|
|
"L-1-C-0",
|
|
|
|
|
"L-0-C",
|
|
|
|
|
"L-0-",
|
|
|
|
|
"X",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, id := range tests {
|
|
|
|
|
if got := ParseBlockID(id); got != nil {
|
|
|
|
|
t.Errorf("ParseBlockID(%q) = %v, want nil", id, got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|