-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_test.go
79 lines (75 loc) · 2.5 KB
/
type_test.go
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
71
72
73
74
75
76
77
78
79
package generic
import (
"fmt"
"testing"
)
func TestStringMethods(t *testing.T) {
tv := &TypeVariable{Name: "T"}
tc := &TypeConstant{Name: "int"}
ft := &FunctionType{
ParamTypes: []Type{&TypeVariable{Name: "A"}, &TypeConstant{Name: "string"}},
ReturnType: &TypeConstant{Name: "bool"},
IsVariadic: true,
}
tt := &TupleType{
Types: []Type{&TypeConstant{Name: "int"}, &TypeVariable{Name: "T"}},
}
it := &Interface{Name: "Reader"}
itt := &InterfaceType{
Name: "io.Reader",
Methods: MethodSet{"Read": {Name: "Read", Params: []Type{}, Results: []Type{&TypeConstant{Name: "int"}}}},
IsEmpty: false,
}
emtIface := InterfaceType{IsEmpty: true}
pt := &PointerType{Base: &TypeConstant{Name: "int"}}
st := &StructType{Name: "Person"}
slt := &SliceType{ElementType: &TypeConstant{Name: "string"}}
at := &ArrayType{ElementType: &TypeVariable{Name: "T"}, Len: 10}
mt := &MapType{KeyType: &TypeConstant{Name: "string"}, ValueType: &TypeConstant{Name: "int"}}
tcst := &TypeConstraint{
Interfaces: []Interface{{Name: "Stringer"}},
Types: []Type{&TypeConstant{Name: "int"}},
}
union := &TypeConstraint{
Types: []Type{&TypeConstant{Name: "int"}, &TypeConstant{Name: "string"}},
Union: true,
}
gt := &GenericType{
Name: "Stack",
TypeParams: []Type{&TypeVariable{Name: "T"}},
Constraints: map[string]TypeConstraint{
"Stack": {Interfaces: []Interface{{Name: "Container"}}, Types: []Type{&TypeConstant{Name: "int"}}},
},
Fields: map[string]Type{"items": &SliceType{ElementType: &TypeVariable{Name: "T"}}},
}
ta := &TypeAlias{Name: "RuneReader", AliasedTo: &InterfaceType{Name: "io.RuneReader"}}
tests := []struct {
typ Type
expected string
}{
{tv, "TypeVar(T)"},
{tc, "TypeConst(int)"},
{ft, "func(TypeVar(A), TypeConst(string)...) TypeConst(bool)"},
{tt, "(TypeConst(int), TypeVar(T))"},
{it, "Interface(Reader)"},
{itt, "InterfaceType(io.Reader)"},
{&emtIface, "interface{}"},
{pt, "*TypeConst(int)"},
{st, "Struct(Person)"},
{slt, "Slice(TypeConst(string))"},
{at, "Arr[10]TypeVar(T)"},
{mt, "Map[TypeConst(string)]TypeConst(int)"},
{tcst, "Constraint([Stringer], [TypeConst(int)])"},
{union, "Union([], [TypeConst(int) | TypeConst(string)])"},
{gt, "Generic(Stack, [TypeVar(T)])"},
{ta, "TypeAlias(RuneReader = InterfaceType(io.RuneReader))"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%T", tt.typ), func(t *testing.T) {
result := tt.typ.String()
if result != tt.expected {
t.Errorf("Expected %q, got %q", tt.expected, result)
}
})
}
}