/
sampler_test.go
112 lines (108 loc) · 2.17 KB
/
sampler_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package swagsampler_test
import (
"errors"
"fmt"
"strings"
"testing"
"github.com/breise/rstack"
"github.com/breise/swagsampler"
"gopkg.in/yaml.v2"
)
func TestSam(t *testing.T) {
var tests = []struct {
name string
inp string
exp func(interface{}) error
}{
{
"string", `
type: string
`,
func(x interface{}) error {
var errs []string
y := x.(string)
if y == "" {
errs = append(errs, "string is empty")
}
if len(y) > 255 {
errs = append(errs, fmt.Sprintf("'%s' is > 255 characters", y))
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "; "))
}
return nil
},
},
{
"string with min and max lengths", `
type: string
minLength: 10
maxLength: 20
`,
func(x interface{}) error {
var errs []string
y := x.(string)
if len(y) < 10 {
errs = append(errs, fmt.Sprintf("'%s' is < 10 characters", y))
}
if len(y) > 20 {
errs = append(errs, fmt.Sprintf("'%s' is > 20 characters", y))
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "; "))
}
return nil
},
},
// {
// "simple object", `
// type: object
// properties:
// thing:
// type: string
// `,
// "map[thing:string0001]",
// },
// {
// "array of numbers",
// `
// type: array
// items:
// type: number
// `,
// "[1.20]",
// },
// {
// "array of objects",
// `
// type: array
// items:
// type: object
// properties:
// id:
// type: integer
// name:
// type: string
// `,
// "[map[id:1 name:string0002]]",
// },
}
for i, tc := range tests {
desc := fmt.Sprintf("Test Case %d: %s", i, tc.name)
inp := tc.inp
exp := tc.exp
t.Run(desc, func(t *testing.T) {
var thing map[interface{}]interface{}
if err := yaml.Unmarshal([]byte(inp), &thing); err != nil {
t.Fatalf("cannot unmarshal '%s'. Error: %s", inp, err)
}
got, err := swagsampler.New().RenderSample(rstack.New().Push("Top"), thing)
if err != nil {
t.Fatalf("cannot mkSample(Top): %s", err)
}
if err := exp(got); err != nil {
t.Errorf("Test Case %d: %s: %s", i, desc, err)
}
})
}
}