From 5dc127d34d1c5e60eee82541da92e72d9f57ec5a Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 25 Jun 2026 00:02:14 -0400 Subject: [PATCH] test(ui): deepen Children parity (count/flatten across shapes) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Toward React-suite equivalency for the GWC-applicable ReactChildren subset (count/flatten/filter — GWC has html.Children normalization, not the Children.map/forEach/count API): count is 0 for nil/empty, 1 for a single arrayless or sliced child, N for flat and deeply (2-3 level) nested structures, nil is filtered, mixed kinds flatten; deep nesting flattens in document order. All pass; no version bump. Co-Authored-By: Claude Opus 4.8 (1M context) --- ui/children_count_test.go | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 ui/children_count_test.go diff --git a/ui/children_count_test.go b/ui/children_count_test.go new file mode 100644 index 00000000..4bee5b14 --- /dev/null +++ b/ui/children_count_test.go @@ -0,0 +1,56 @@ +//go:build !js || !wasm + +package ui_test + +import ( + "testing" + + "github.com/monstercameron/GoWebComponents/html" + "github.com/monstercameron/GoWebComponents/ui" +) + +// Deepens the ReactChildren port toward equivalency for the GWC-applicable subset +// (count / flatten / filter — GWC has html.Children normalization, not the +// Children.map/forEach/count API). Mirrors React's count/flatten cases. +func TestChildrenCountAcrossShapes(t *testing.T) { + span := func(parseS string) ui.Node { return html.Span(html.Props{}, ui.Text(parseS)) } + + cases := []struct { + name string + in []any + want int + }{ + {"nil-only -> 0", []any{nil}, 0}, + {"all-nil -> 0", []any{nil, nil}, 0}, + {"empty -> 0", []any{}, 0}, + {"single arrayless -> 1", []any{span("a")}, 1}, + {"single in slice -> 1", []any{[]ui.Node{span("a")}}, 1}, + {"flat -> 3", []any{span("a"), span("b"), span("c")}, 3}, + {"nested 2 levels", []any{span("a"), []ui.Node{span("b"), span("c")}}, 3}, + {"nested 3 levels", []any{[]any{[]ui.Node{span("a"), span("b")}, span("c")}}, 3}, + {"mixed kinds", []any{"text", span("a"), []any{"more", span("b")}}, 4}, + {"nil interspersed", []any{span("a"), nil, span("b"), nil, span("c")}, 3}, + } + for _, parseCase := range cases { + if parseGot := len(html.Children(parseCase.in...)); parseGot != parseCase.want { + t.Errorf("%s: count = %d, want %d", parseCase.name, parseGot, parseCase.want) + } + } +} + +// Deeply nested children flatten in document order (a mis-ordered or partial +// flatten would scramble the rendered output). +func TestChildrenDeepFlattenOrder(t *testing.T) { + parseNodes := html.Children( + ui.Text("1"), + []any{ui.Text("2"), []ui.Node{ui.Text("3"), ui.Text("4")}}, + ui.Text("5"), + ) + parseOut, parseErr := ui.RenderToString(html.Div(html.Props{}, parseNodes...)) + if parseErr != nil { + t.Fatalf("render error: %v", parseErr) + } + if parseOut != "
12345
" { + t.Errorf("deep flatten order = %q, want
12345
", parseOut) + } +}