4
4
. "github.com/antonmedv/expr/docgen"
5
5
"github.com/sanity-io/litter"
6
6
"github.com/stretchr/testify/assert"
7
+ "math"
7
8
"testing"
8
- "time"
9
9
)
10
10
11
11
type Tweet struct {
@@ -21,9 +21,14 @@ type Env struct {
21
21
Env map [string ]interface {}
22
22
}
23
23
24
- func (* Env ) Duration (s string ) time.Duration {
25
- d , _ := time .ParseDuration (s )
26
- return d
24
+ type Duration int
25
+
26
+ func (Duration ) String () string {
27
+ return ""
28
+ }
29
+
30
+ func (* Env ) Duration (s string ) Duration {
31
+ return Duration (0 )
27
32
}
28
33
29
34
func TestCreateDoc (t * testing.T ) {
@@ -67,70 +72,64 @@ func TestCreateDoc(t *testing.T) {
67
72
"Duration" : {
68
73
Kind : "struct" ,
69
74
Fields : map [Identifier ]* Type {
70
- "Hours" : {
71
- Kind : "func" ,
72
- Arguments : []* Type {},
73
- Return : & Type {
74
- Kind : "float" ,
75
- },
76
- },
77
- "Minutes" : {
78
- Kind : "func" ,
79
- Arguments : []* Type {},
80
- Return : & Type {
81
- Kind : "float" ,
82
- },
83
- },
84
- "Nanoseconds" : {
85
- Kind : "func" ,
86
- Arguments : []* Type {},
87
- Return : & Type {
88
- Kind : "int" ,
89
- },
90
- },
91
- "Round" : {
92
- Kind : "func" ,
93
- Arguments : []* Type {
94
- {
95
- Name : "Duration" ,
96
- Kind : "struct" ,
97
- },
98
- },
99
- Return : & Type {
100
- Name : "Duration" ,
101
- Kind : "struct" ,
102
- },
103
- },
104
- "Seconds" : {
105
- Kind : "func" ,
106
- Arguments : []* Type {},
107
- Return : & Type {
108
- Kind : "float" ,
109
- },
110
- },
111
75
"String" : {
112
76
Kind : "func" ,
113
77
Arguments : []* Type {},
114
78
Return : & Type {
115
79
Kind : "string" ,
116
80
},
117
81
},
118
- "Truncate" : {
119
- Kind : "func" ,
120
- Arguments : []* Type {
121
- {
122
- Name : "Duration" ,
123
- Kind : "struct" ,
124
- },
125
- },
126
- Return : & Type {
127
- Name : "Duration" ,
128
- Kind : "struct" ,
129
- },
130
- },
131
82
},
132
83
},
133
84
},
134
85
}
86
+
87
+ assert .Equal (t , litter .Sdump (expected ), litter .Sdump (doc ))
88
+ }
89
+
90
+ func TestCreateDoc_FromMap (t * testing.T ) {
91
+ env := map [string ]interface {}{
92
+ "Tweets" : []* Tweet {},
93
+ "Config" : struct {
94
+ MaxSize int
95
+ }{},
96
+ "Max" : math .Max ,
97
+ }
98
+ doc := CreateDoc (env )
99
+ expected := & Context {
100
+ Variables : map [Identifier ]* Type {
101
+ "Tweets" : {
102
+ Kind : "array" ,
103
+ Type : & Type {
104
+ Kind : "struct" ,
105
+ Name : "Tweet" ,
106
+ },
107
+ },
108
+ "Config" : {
109
+ Kind : "struct" ,
110
+ Fields : map [Identifier ]* Type {
111
+ "MaxSize" : {Kind : "int" },
112
+ },
113
+ },
114
+ "Max" : {
115
+ Kind : "func" ,
116
+ Arguments : []* Type {
117
+ {Kind : "float" },
118
+ {Kind : "float" },
119
+ },
120
+ Return : & Type {Kind : "float" },
121
+ },
122
+ },
123
+ Types : map [TypeName ]* Type {
124
+ "Tweet" : {
125
+ Kind : "struct" ,
126
+ Fields : map [Identifier ]* Type {
127
+ "Size" : {Kind : "int" },
128
+ "Message" : {Kind : "string" },
129
+ },
130
+ },
131
+ },
132
+ }
133
+
135
134
assert .Equal (t , litter .Sdump (expected ), litter .Sdump (doc ))
136
135
}
0 commit comments