add a few tests
This commit is contained in:
parent
2325df1593
commit
279a3c7163
4 changed files with 67 additions and 2 deletions
|
|
@ -10,6 +10,7 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/fasthttp/websocket v1.5.10 // indirect
|
github.com/fasthttp/websocket v1.5.10 // indirect
|
||||||
github.com/gofiber/contrib/websocket v1.3.2 // indirect
|
github.com/gofiber/contrib/websocket v1.3.2 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
|
@ -19,8 +20,10 @@ require (
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.16 // indirect
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
|
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
|
||||||
|
github.com/stretchr/testify v1.9.0 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
github.com/valyala/fasthttp v1.55.0 // indirect
|
github.com/valyala/fasthttp v1.55.0 // indirect
|
||||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||||
|
|
@ -28,4 +31,5 @@ require (
|
||||||
golang.org/x/crypto v0.27.0 // indirect
|
golang.org/x/crypto v0.27.0 // indirect
|
||||||
golang.org/x/sys v0.25.0 // indirect
|
golang.org/x/sys v0.25.0 // indirect
|
||||||
golang.org/x/text v0.18.0 // indirect
|
golang.org/x/text v0.18.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
57
framework/h/render_test.go
Normal file
57
framework/h/render_test.go
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
package h
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRender(t *testing.T) {
|
||||||
|
div := Div(
|
||||||
|
Id("my-div"),
|
||||||
|
Attribute("data-attr-2", "value"),
|
||||||
|
Attributes(&AttributeMap{
|
||||||
|
"data-attr-3": "value",
|
||||||
|
"data-attr-4": "value",
|
||||||
|
}),
|
||||||
|
BeforeRequest(
|
||||||
|
SetText("before request"),
|
||||||
|
),
|
||||||
|
AfterRequest(
|
||||||
|
SetText("after request"),
|
||||||
|
),
|
||||||
|
Children(
|
||||||
|
Div(Text("hello, world")),
|
||||||
|
),
|
||||||
|
Text("hello, child"),
|
||||||
|
)
|
||||||
|
|
||||||
|
div.attributes["data-attr-1"] = "value"
|
||||||
|
|
||||||
|
result := Render(div)
|
||||||
|
|
||||||
|
assert.Equal(t,
|
||||||
|
`<div data-attr-1="value" id="my-div" data-attr-2="value" data-attr-3="value" data-attr-4="value" hx-on::before-request="this.innerText = 'before request';" hx-on::after-request="this.innerText = 'after request';"><div >hello, world</div>hello, child</div>`,
|
||||||
|
result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRawContent(t *testing.T) {
|
||||||
|
str := "<div>hello, world</div>"
|
||||||
|
raw := Raw(str)
|
||||||
|
assert.Equal(t, str, Render(raw))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConditional(t *testing.T) {
|
||||||
|
result := Render(
|
||||||
|
Div(
|
||||||
|
IfElse(true, Text("true"), Text("false")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert.Equal(t, "<div >true</div>", result)
|
||||||
|
|
||||||
|
result = Render(
|
||||||
|
Div(
|
||||||
|
If(false, Text("true")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assert.Equal(t, "<div ></div>", result)
|
||||||
|
}
|
||||||
|
|
@ -7,9 +7,11 @@ import (
|
||||||
|
|
||||||
func (node *Element) Render(builder *strings.Builder) {
|
func (node *Element) Render(builder *strings.Builder) {
|
||||||
// some elements may not have a tag, such as a Fragment
|
// some elements may not have a tag, such as a Fragment
|
||||||
|
|
||||||
if node.tag != "" {
|
if node.tag != "" {
|
||||||
builder.WriteString("<" + node.tag)
|
builder.WriteString("<" + node.tag)
|
||||||
builder.WriteString(" ")
|
builder.WriteString(" ")
|
||||||
|
|
||||||
for name, value := range node.attributes {
|
for name, value := range node.attributes {
|
||||||
NewAttribute(name, value).Render(builder)
|
NewAttribute(name, value).Render(builder)
|
||||||
}
|
}
|
||||||
|
|
@ -82,6 +84,7 @@ func (m *AttributeMap) Render(builder *strings.Builder) {
|
||||||
m2 := m.ToMap()
|
m2 := m.ToMap()
|
||||||
|
|
||||||
for k, v := range m2 {
|
for k, v := range m2 {
|
||||||
|
builder.WriteString(" ")
|
||||||
NewAttribute(k, v).Render(builder)
|
NewAttribute(k, v).Render(builder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,7 @@ func Tag(tag string, children ...Ren) *Element {
|
||||||
return &Element{
|
return &Element{
|
||||||
tag: tag,
|
tag: tag,
|
||||||
children: children,
|
children: children,
|
||||||
|
attributes: make(map[string]string),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue