1
0
html/validate_test.go

140 lines
2.8 KiB
Go

package html_test
import (
"bytes"
"code.squareroundforest.org/arpio/html"
. "code.squareroundforest.org/arpio/html/tag"
"testing"
)
func TestValidate(t *testing.T) {
t.Run("symbol", func(t *testing.T) {
t.Run("invalid", func(t *testing.T) {
mytag := html.Define("foo+bar")
var b bytes.Buffer
if err := html.WriteRaw(&b, mytag); err == nil {
t.Fatal()
}
})
t.Run("invalid with allowed chars number", func(t *testing.T) {
mytag := html.Define("0foo")
var b bytes.Buffer
if err := html.WriteRaw(&b, mytag); err == nil {
t.Fatal()
}
})
t.Run("invalid with allowed chars delimiter", func(t *testing.T) {
mytag := html.Define("-foo")
var b bytes.Buffer
if err := html.WriteRaw(&b, mytag); err == nil {
t.Fatal()
}
})
t.Run("valid", func(t *testing.T) {
mytag := html.Define("foo")
var b bytes.Buffer
if err := html.WriteRaw(&b, mytag); err != nil {
t.Fatal(err)
}
})
t.Run("valid with special chars", func(t *testing.T) {
mytag := html.Define("foo-bar-1")
var b bytes.Buffer
if err := html.WriteRaw(&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.WriteRaw(&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.WriteRaw(&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.WriteRaw(&b, div); err != nil {
t.Fatal(err)
}
})
t.Run("script with child tag", func(t *testing.T) {
script := Script(Br())
var b bytes.Buffer
if err := html.WriteRaw(&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.WriteRaw(&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.WriteRaw(&b, div); err != nil {
t.Fatal()
}
})
t.Run("declaration valid", func(t *testing.T) {
decl := html.Declaration("foo", "bar", "baz qux")
var b bytes.Buffer
if err := html.WriteRaw(&b, decl); err != nil {
t.Fatal(err)
}
})
t.Run("declaration valid with non-symbol attribute name", func(t *testing.T) {
decl := html.Declaration("#", "foo")
var b bytes.Buffer
if err := html.WriteRaw(&b, decl); err != nil {
t.Fatal(err)
}
})
t.Run("declaration with children", func(t *testing.T) {
decl := html.Declaration("foo")
decl = decl("bar")
var b bytes.Buffer
if err := html.WriteRaw(&b, decl); err == nil {
t.Fatal("failed to fail")
}
})
}