forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generics_other_test.go
67 lines (55 loc) · 1.81 KB
/
generics_other_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
//go:build !go1.18
// +build !go1.18
package swag
import (
"fmt"
"github.com/stretchr/testify/assert"
"go/ast"
"testing"
)
type testLogger struct {
Messages []string
}
func (t *testLogger) Printf(format string, v ...interface{}) {
t.Messages = append(t.Messages, fmt.Sprintf(format, v...))
}
func TestParametrizeStruct(t *testing.T) {
t.Parallel()
pd := PackagesDefinitions{
packages: make(map[string]*PackageDefinitions),
}
tSpec := &TypeSpecDef{
TypeSpec: &ast.TypeSpec{
Name: &ast.Ident{Name: "Field"},
Type: &ast.StructType{Struct: 100, Fields: &ast.FieldList{Opening: 101, Closing: 102}},
},
}
tr := pd.parametrizeStruct(&ast.File{}, tSpec, "", false)
assert.Equal(t, tr, tSpec)
tr = pd.parametrizeStruct(&ast.File{}, tSpec, "", true)
assert.Equal(t, tr, tSpec)
}
func TestParseGenericTypeExpr(t *testing.T) {
t.Parallel()
parser := New()
logger := &testLogger{}
SetDebugger(logger)(parser)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.InterfaceType{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.StructType{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.Ident{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.StarExpr{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.SelectorExpr{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.ArrayType{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.MapType{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.FuncType{})
assert.Empty(t, logger.Messages)
_, _ = parser.parseGenericTypeExpr(&ast.File{}, &ast.BadExpr{})
assert.NotEmpty(t, logger.Messages)
}