From b204d86a0a623b3636c7fbafb59a42e3d12a82c7 Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Wed, 12 Oct 2022 22:16:41 -0400 Subject: pkg/nstypes: Add basic consistency tests --- pkg/nstypes/nstypes_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pkg/nstypes/nstypes_test.go (limited to 'pkg/nstypes/nstypes_test.go') diff --git a/pkg/nstypes/nstypes_test.go b/pkg/nstypes/nstypes_test.go new file mode 100644 index 0000000..27e5053 --- /dev/null +++ b/pkg/nstypes/nstypes_test.go @@ -0,0 +1,70 @@ +package nstypes + +import ( + "fmt" + "reflect" + "strings" + "testing" +) + +func TestMaps(t *testing.T) { + testNSType(t, Maps) +} + +func TestPlaylists(t *testing.T) { + testNSType(t, Playlists) +} + +func testNSType[T interface { + fmt.GoStringer + fmt.Stringer + SourceString() string + Known() bool + Title() (string, bool) +}](t *testing.T, all func() []T) { + var dummy T + if reflect.TypeOf(dummy).Kind() != reflect.String { + t.Error("enum type must be a string") + } + reflect.ValueOf(&dummy).Elem().SetString("__nonexistent__") + + name := reflect.TypeOf(dummy).Name() + if strings.Contains(name, ".") { + panic("wtf") + } + + if dummy.GoString() != fmt.Sprintf("%s(%q)", name, "__nonexistent__") { + t.Error("incorrect GoString output for nonexistent enum value") + } + if dummy.String() != "__nonexistent__" { + t.Error("incorrect String output for nonexistent enum value") + } + if dummy.SourceString() != "__nonexistent__" { + t.Error("incorrect SourceString output for nonexistent enum value") + } + if dummy.Known() { + t.Error("known should not return true for nonexistent enum value") + } + if x, ok := dummy.Title(); x != "" || ok { + t.Error("incorrect Title output for nonexistent enum value") + } + + for _, v := range all() { + val := reflect.ValueOf(v).String() + if v.GoString() != fmt.Sprintf("%s(%q)", name, val) { + t.Error("incorrect GoString output for existing enum value") + } + if v.String() == val { + t.Error("incorrect String output for existing enum value (should be the title, not the raw value)") + } + if v.SourceString() != val { + t.Error("incorrect SourceString output for existing enum value") + } + if !v.Known() { + t.Error("known should return true for existing enum value") + } + if x, ok := v.Title(); x != v.String() || !ok { + t.Error("incorrect Title output for existing enum value") + } + } +} -- cgit v1.2.3