1
0
html/validate_test.go
2025-09-11 20:50:00 +02:00

112 lines
2.1 KiB
Go

package html_test
import (
"bytes"
"code.squareroundforest.org/arpio/html"
. "code.squareroundforest.org/arpio/html/tags"
"testing"
)
func TestValidate(t *testing.T) {
t.Run("symbol", func(t *testing.T) {
t.Run("invalid", func(t *testing.T) {
mytag := html.NewTag("foo+bar")
var b bytes.Buffer
if err := html.Render(&b, mytag); err == nil {
t.Fatal()
}
})
t.Run("invalid with allowed chars number", func(t *testing.T) {
mytag := html.NewTag("0foo")
var b bytes.Buffer
if err := html.Render(&b, mytag); err == nil {
t.Fatal()
}
})
t.Run("invalid with allowed chars delimiter", func(t *testing.T) {
mytag := html.NewTag("-foo")
var b bytes.Buffer
if err := html.Render(&b, mytag); err == nil {
t.Fatal()
}
})
t.Run("valid", func(t *testing.T) {
mytag := html.NewTag("foo")
var b bytes.Buffer
if err := html.Render(&b, mytag); err != nil {
t.Fatal(err)
}
})
t.Run("valid with special chars", func(t *testing.T) {
mytag := html.NewTag("foo-bar-1")
var b bytes.Buffer
if err := html.Render(&b, mytag); err != nil {
t.Fatal(err)
}
})
})
t.Run("invalid attribute name", func(t *testing.T) {
div := Div(Attr("foo+", "bar"))
var b bytes.Buffer
if err := html.Render(&b, div); err == nil {
t.Fatal()
}
})
t.Run("void tag with children", func(t *testing.T) {
br := Br("foo")
var b bytes.Buffer
if err := html.Render(&b, br); err == nil {
t.Fatal()
}
})
t.Run("verbatim with child tag", func(t *testing.T) {
div := html.Verbatim(Div(Br()))
var b bytes.Buffer
if err := html.Render(&b, div); err == nil {
t.Fatal()
}
})
t.Run("script with child tag", func(t *testing.T) {
script := Script(Br())
var b bytes.Buffer
if err := html.Render(&b, script); err == nil {
t.Fatal()
}
})
t.Run("invalid child tag", func(t *testing.T) {
div := Div(Div(Attr("foo+", "bar")))
var b bytes.Buffer
if err := html.Render(&b, div); err == nil {
t.Fatal()
}
})
t.Run("valid child tag", func(t *testing.T) {
div := Div(Div(Attr("foo", "bar")))
var b bytes.Buffer
if err := html.Render(&b, div); err != nil {
t.Fatal()
}
})
}