diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-12 22:16:41 -0400 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-12 22:16:41 -0400 |
commit | b204d86a0a623b3636c7fbafb59a42e3d12a82c7 (patch) | |
tree | 1fa6e452da2b435d527873d4fab2392c55b43415 /pkg/nstypes/nstypes_test.go | |
parent | 27d3a7a684a00d729db630d3901c6ecf8fed8550 (diff) | |
download | Atlas-b204d86a0a623b3636c7fbafb59a42e3d12a82c7.tar.gz Atlas-b204d86a0a623b3636c7fbafb59a42e3d12a82c7.zip |
pkg/nstypes: Add basic consistency tests
Diffstat (limited to 'pkg/nstypes/nstypes_test.go')
-rw-r--r-- | pkg/nstypes/nstypes_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
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") + } + } +} |