aboutsummaryrefslogtreecommitdiff
path: root/pkg/nstypes/nstypes_test.go
blob: 27e5053fe9ee7d2a9015d788a649efef1e5fafb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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")
		}
	}
}