From e77ab0bc28923df8ead92ece29801c7433ee2a55 Mon Sep 17 00:00:00 2001 From: maddalax Date: Thu, 26 Sep 2024 16:01:41 -0500 Subject: [PATCH] fix framework tests (sort attributes) --- framework/go.mod | 1 + framework/go.sum | 16 ++++++++++++ framework/h/render_test.go | 52 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 framework/go.sum diff --git a/framework/go.mod b/framework/go.mod index 9512443..a6a7b64 100644 --- a/framework/go.mod +++ b/framework/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-chi/chi/v5 v5.1.0 github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.9.0 + golang.org/x/net v0.29.0 ) require ( diff --git a/framework/go.sum b/framework/go.sum new file mode 100644 index 0000000..cfa8344 --- /dev/null +++ b/framework/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo= +golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/framework/h/render_test.go b/framework/h/render_test.go index 9492002..1b5a2a2 100644 --- a/framework/h/render_test.go +++ b/framework/h/render_test.go @@ -1,11 +1,55 @@ package h import ( + "bytes" "github.com/stretchr/testify/assert" + "golang.org/x/net/html" + "sort" + "strings" "testing" ) +// Sort attributes of a node by attribute name +func sortAttributes(node *html.Node) { + if node.Type == html.ElementNode && len(node.Attr) > 1 { + sort.SliceStable(node.Attr, func(i, j int) bool { + return node.Attr[i].Key < node.Attr[j].Key + }) + } +} + +// Traverse and sort attributes in the entire HTML tree +func traverseAndSortAttributes(node *html.Node) { + sortAttributes(node) + for child := node.FirstChild; child != nil; child = child.NextSibling { + traverseAndSortAttributes(child) + } +} + +// Parse HTML, sort attributes, and render back to a string +func parseSortAndRenderHTML(input string) string { + // Parse the HTML string into a node tree + doc, err := html.Parse(strings.NewReader(input)) + if err != nil { + return "" + } + + // Traverse and sort attributes for each node + traverseAndSortAttributes(doc) + + // Use a buffer to capture the rendered HTML + var buf bytes.Buffer + err = html.Render(&buf, doc) + if err != nil { + return "" + } + + // Return the rendered HTML as a string + return buf.String() +} + func TestRender(t *testing.T) { + t.Parallel() div := Div( Id("my-div"), Attribute("data-attr-2", "value"), @@ -27,20 +71,24 @@ func TestRender(t *testing.T) { div.attributes["data-attr-1"] = "value" - result := Render(div) + expectedRaw := `
hello, world
hello, child
` + expected := parseSortAndRenderHTML(expectedRaw) + result := parseSortAndRenderHTML(Render(div)) assert.Equal(t, - `
hello, world
hello, child
`, + expected, result) } func TestRawContent(t *testing.T) { + t.Parallel() str := "
hello, world
" raw := Raw(str) assert.Equal(t, str, Render(raw)) } func TestConditional(t *testing.T) { + t.Parallel() result := Render( Div( Ternary(true, Text("true"), Text("false")),