Skip to content

Commit f56eaed

Browse files
committed
Add docgen from map
1 parent 51ea1bf commit f56eaed

File tree

1 file changed

+57
-58
lines changed

1 file changed

+57
-58
lines changed

docgen/docgen_test.go

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
. "github.com/antonmedv/expr/docgen"
55
"github.com/sanity-io/litter"
66
"github.com/stretchr/testify/assert"
7+
"math"
78
"testing"
8-
"time"
99
)
1010

1111
type Tweet struct {
@@ -21,9 +21,14 @@ type Env struct {
2121
Env map[string]interface{}
2222
}
2323

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)
2732
}
2833

2934
func TestCreateDoc(t *testing.T) {
@@ -67,70 +72,64 @@ func TestCreateDoc(t *testing.T) {
6772
"Duration": {
6873
Kind: "struct",
6974
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-
},
11175
"String": {
11276
Kind: "func",
11377
Arguments: []*Type{},
11478
Return: &Type{
11579
Kind: "string",
11680
},
11781
},
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-
},
13182
},
13283
},
13384
},
13485
}
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+
135134
assert.Equal(t, litter.Sdump(expected), litter.Sdump(doc))
136135
}

0 commit comments

Comments
 (0)