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 genopenapi
import (
"encoding/json"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
func newSpaceReplacer() *strings.Replacer {
return strings.NewReplacer(" ", "", "\n", "", "\t", "")
}
func TestRawExample(t *testing.T) {
t.Parallel()
testCases := [...]struct {
In RawExample
Exp string
}{{
In: RawExample(`1`),
Exp: `1`,
}, {
In: RawExample(`"1"`),
Exp: `"1"`,
}, {
In: RawExample(`{"hello":"worldr"}`),
Exp: `
hello:
worldr
`,
}}
sr := newSpaceReplacer()
for _, tc := range testCases {
tc := tc
t.Run(string(tc.In), func(t *testing.T) {
t.Parallel()
ex := tc.In
out, err := yaml.Marshal(ex)
switch {
case err != nil:
t.Fatalf("expect no yaml marshal error, got: %s", err)
case !json.Valid(tc.In):
t.Fatalf("json is invalid: %#q", tc.In)
case sr.Replace(tc.Exp) != sr.Replace(string(out)):
t.Fatalf("expected: %s, actual: %s", tc.Exp, out)
}
out, err = json.Marshal(tc.In)
switch {
case err != nil:
t.Fatalf("expect no json marshal error, got: %s", err)
case sr.Replace(string(tc.In)) != sr.Replace(string(out)):
t.Fatalf("expected: %s, actual: %s", tc.In, out)
}
})
}
}
func TestOpenapiSchemaObjectProperties(t *testing.T) {
t.Parallel()
v := map[string]interface{}{
"example": openapiSchemaObjectProperties{{
Key: "test1",
Value: 1,
}, {
Key: "test2",
Value: 2,
}},
}
t.Run("yaml", func(t *testing.T) {
t.Parallel()
const exp = `
example:
test1: 1
test2: 2
`
sr := newSpaceReplacer()
out, err := yaml.Marshal(v)
switch {
case err != nil:
t.Fatalf("expect no marshal error, got: %s", err)
case sr.Replace(exp) != sr.Replace(string(out)):
t.Fatalf("expected: %s, actual: %s", exp, out)
}
})
t.Run("json", func(t *testing.T) {
t.Parallel()
const exp = `{"example":{"test1":1,"test2":2}}`
got, err := json.Marshal(v)
switch {
case err != nil:
t.Fatalf("expect no marshal error, got: %s", err)
case exp != string(got):
t.Fatalf("expected: %s, actual: %s", exp, got)
}
})
}
|