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
|
package analysis_test
import (
"encoding/json"
"fmt"
"regexp"
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
rex = regexp.MustCompile(`"\$ref":\s*"(.*?)"`)
)
func assertRefResolve(t *testing.T, jazon, exclude string, root interface{}, opts ...*spec.ExpandOptions) {
assertRefWithFunc(t, "resolve", jazon, exclude, func(t *testing.T, match string) {
ref := spec.MustCreateRef(match)
var (
sch *spec.Schema
err error
)
if len(opts) > 0 {
options := *opts[0]
sch, err = spec.ResolveRefWithBase(root, &ref, &options)
} else {
sch, err = spec.ResolveRef(root, &ref)
}
require.NoErrorf(t, err, `%v: for "$ref": %q`, err, match)
require.NotNil(t, sch)
})
}
// assertNoRef ensures that no $ref is remaining in json doc
func assertNoRef(t testing.TB, jazon string) {
m := rex.FindAllStringSubmatch(jazon, -1)
require.Nil(t, m)
}
func assertRefInJSONRegexp(t testing.TB, jazon, match string) {
// assert a match in a references
m := rex.FindAllStringSubmatch(jazon, -1)
require.NotNil(t, m)
refMatch, err := regexp.Compile(match)
require.NoError(t, err)
for _, matched := range m {
subMatch := matched[1]
assert.True(t, refMatch.MatchString(subMatch),
"expected $ref to match %q, got: %s", match, matched[0])
}
}
// assertRefResolve ensures that all $ref in some json doc verify some asserting func.
//
// "exclude" is a regexp pattern to ignore certain $ref (e.g. some specs may embed $ref that are not processed, such as extensions).
func assertRefWithFunc(t *testing.T, name, jazon, exclude string, asserter func(*testing.T, string)) {
filterRex := regexp.MustCompile(exclude)
m := rex.FindAllStringSubmatch(jazon, -1)
require.NotNil(t, m)
allRefs := make(map[string]struct{}, len(m))
for _, toPin := range m {
matched := toPin
subMatch := matched[1]
if exclude != "" && filterRex.MatchString(subMatch) {
continue
}
_, ok := allRefs[subMatch]
if ok {
continue
}
allRefs[subMatch] = struct{}{}
t.Run(fmt.Sprintf("%s-%s-%s", t.Name(), name, subMatch), func(t *testing.T) {
t.Parallel()
asserter(t, subMatch)
})
}
}
func asJSON(t testing.TB, sp interface{}) string {
bbb, err := json.MarshalIndent(sp, "", " ")
require.NoError(t, err)
return string(bbb)
}
|