From fc3cc0f0f3a59bf4fe27a4e32dc2ef420b3fa2f8 Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Sun, 13 Sep 2020 22:50:19 +0200 Subject: Add first implementation of Node, El, Attr, Text --- gomponents_test.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 gomponents_test.go (limited to 'gomponents_test.go') diff --git a/gomponents_test.go b/gomponents_test.go new file mode 100644 index 0000000..21ab30a --- /dev/null +++ b/gomponents_test.go @@ -0,0 +1,75 @@ +package gomponents_test + +import ( + "testing" + + g "github.com/maragudk/gomponents" +) + +func TestAttr(t *testing.T) { + t.Run("renders just the local name with one argument", func(t *testing.T) { + a := g.Attr("required") + equal(t, " required", a.Render()) + }) + + t.Run("renders the name and value when given two arguments", func(t *testing.T) { + a := g.Attr("id", "hat") + equal(t, ` id="hat"`, a.Render()) + }) + + t.Run("panics with more than two arguments", func(t *testing.T) { + called := false + defer func() { + if err := recover(); err != nil { + called = true + } + }() + g.Attr("name", "value", "what is this?") + if !called { + t.FailNow() + } + }) +} + +func TestEl(t *testing.T) { + t.Run("renders an empty element if no children given", func(t *testing.T) { + e := g.El("div") + equal(t, "
", e.Render()) + }) + + t.Run("renders an empty element if only attributes given as children", func(t *testing.T) { + e := g.El("div", g.Attr("class", "hat")) + equal(t, `
`, e.Render()) + }) + + t.Run("renders an element, attributes, and element children", func(t *testing.T) { + e := g.El("div", g.Attr("class", "hat"), g.El("span")) + equal(t, `
`, e.Render()) + }) + + t.Run("renders attributes at the correct place regardless of placement in parameter list", func(t *testing.T) { + e := g.El("div", g.El("span"), g.Attr("class", "hat")) + equal(t, `
`, e.Render()) + }) +} + +func TestText(t *testing.T) { + t.Run("renders escaped text", func(t *testing.T) { + e := g.Text("
") + equal(t, "<div/>", e.Render()) + }) +} + +func TestRaw(t *testing.T) { + t.Run("renders raw text", func(t *testing.T) { + e := g.Raw("
") + equal(t, "
", e.Render()) + }) +} + +func equal(t *testing.T, expected, actual string) { + if expected != actual { + t.Errorf("expected %v but got %v", expected, actual) + t.FailNow() + } +} -- cgit 1.4.1