From 59f0ab8fae3fdae8c39044df2a6ad271359a4171 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 29 May 2019 21:33:30 -0600 Subject: [PATCH] Added tests for each attribute type --- attribute/generated_test.go | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 attribute/generated_test.go diff --git a/attribute/generated_test.go b/attribute/generated_test.go new file mode 100644 index 0000000..4e8e6db --- /dev/null +++ b/attribute/generated_test.go @@ -0,0 +1,75 @@ +package attribute_test + +import ( + "testing" + + "gitlab.codemonkeysoftware.net/b/hatmill" + "gitlab.codemonkeysoftware.net/b/hatmill/attribute" +) + +func testAttribValue(t *testing.T, attrib hatmill.Attrib, expectedValue string) { + t.Helper() + if attrib.Value != expectedValue { + t.Fatalf("expected attribute value to be %#v, but got %#v", expectedValue, attrib.Value) + } +} + +func TestSpaceList(t *testing.T) { + t.Run("empty", func(t *testing.T) { + testAttribValue(t, attribute.Class(), "") + }) + t.Run("nonempty", func(t *testing.T) { + testAttribValue(t, attribute.Class("alpha", "bravo", "charlie"), "alpha bravo charlie") + }) +} + +func TestString(t *testing.T) { + const s = "abcdefg" + testAttribValue(t, attribute.Id(s), s) +} + +func TestBool(t *testing.T) { + testAttribValue(t, attribute.Disabled(), "") +} + +func TestExplicitBool(t *testing.T) { + t.Run("true", func(t *testing.T) { + testAttribValue(t, attribute.Draggable(true), "true") + }) + t.Run("false", func(t *testing.T) { + testAttribValue(t, attribute.Draggable(false), "false") + }) +} + +func TestInt(t *testing.T) { + t.Run("zero", func(t *testing.T) { + testAttribValue(t, attribute.Height(0), "0") + }) + t.Run("positive", func(t *testing.T) { + testAttribValue(t, attribute.Height(45), "45") + }) + t.Run("negative", func(t *testing.T) { + testAttribValue(t, attribute.Tabindex(-45), "-45") + }) +} + +func TestFloat(t *testing.T) { + for _, testcase := range []struct{ + name string + f float32 + value string + } { + {name: "zero", f: 0, value: "0"}, + {name: "integral", f: 45, value: "45"}, + {name: "positive fractional, small positive exponent", f: 12.55, value: "12.55"}, + {name: "positive fractional, large positive exponent", f: 1.55E+12, value: "1.55E+12"}, + {name: "positive fractional, large negative exponent", f: 1.55E-12, value: "1.55E-12"}, + {name: "negative fractional, small positive exponent", f: -12.55, value: "-12.55"}, + {name: "negative fractional, large positive exponent", f: -1.55E+12, value: "-1.55E+12"}, + {name: "negative fractional, large negative exponent", f: -1.55E-12, value: "-1.55E-12"}, + } { + t.Run(testcase.name, func(t *testing.T) { + testAttribValue(t, attribute.Min(testcase.f), testcase.value) + }) + } +}