...
1
16
17 package internal
18
19 import (
20 "github.com/go-openapi/jsonreference"
21 "reflect"
22 "testing"
23 )
24
25 func TestJSONRefFromMap(t *testing.T) {
26 testcases := []struct {
27 name string
28 fromMap map[string]interface{}
29 expectNil bool
30 expectRefString string
31 }{
32 {
33 name: "nil map",
34 expectRefString: "",
35 }, {
36 name: "map with no $ref",
37 fromMap: map[string]interface{}{"a": "b"},
38 expectRefString: "",
39 }, {
40 name: "ref path",
41 fromMap: map[string]interface{}{"$ref": "#/path"},
42 expectRefString: "#/path",
43 },
44 }
45
46 for _, tc := range testcases {
47 var tmp jsonreference.Ref
48 err := JSONRefFromMap(&tmp, tc.fromMap)
49 if err != nil {
50 t.Errorf("Expect no error from JSONRefMap %s, got error %v", tc.name, err)
51 }
52
53 if tmp.String() != tc.expectRefString {
54 t.Errorf("Expect jsonRef to be %s, got %s for %s", tc.expectRefString, tmp.String(), tc.name)
55 }
56
57 }
58 }
59
60 func TestSanitizeExtensions(t *testing.T) {
61 testcases := []struct {
62 in map[string]interface{}
63 expected map[string]interface{}
64 }{
65 {
66 in: map[string]interface{}{"a": "b", "x-extension": "foo"},
67 expected: map[string]interface{}{"x-extension": "foo"},
68 },
69 {
70 in: map[string]interface{}{"a": "b"},
71 expected: nil,
72 },
73 {
74 in: map[string]interface{}{},
75 expected: nil,
76 },
77 }
78
79 for _, tc := range testcases {
80 e := SanitizeExtensions(tc.in)
81 if !reflect.DeepEqual(tc.expected, e) {
82 t.Errorf("Error: sanitize extensions does not match expected")
83 }
84 }
85 }
86
View as plain text