mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 20:47:49 +00:00
fix: continued moving everything to orderedmaps plus cleaned up most the tests
This commit is contained in:
@@ -7,51 +7,52 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"golang.org/x/sync/syncmap"
|
||||
"gopkg.in/yaml.v3"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/sync/syncmap"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/pb33f/libopenapi/orderedmap"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFindItemInMap(t *testing.T) {
|
||||
v := make(map[KeyReference[string]]ValueReference[string])
|
||||
v[KeyReference[string]{
|
||||
func TestFindItemInOrderedMap(t *testing.T) {
|
||||
v := orderedmap.New[KeyReference[string], ValueReference[string]]()
|
||||
v.Set(KeyReference[string]{
|
||||
Value: "pizza",
|
||||
}] = ValueReference[string]{
|
||||
}, ValueReference[string]{
|
||||
Value: "pie",
|
||||
}
|
||||
assert.Equal(t, "pie", FindItemInMap("pizza", v).Value)
|
||||
})
|
||||
assert.Equal(t, "pie", FindItemInOrderedMap("pizza", v).Value)
|
||||
}
|
||||
|
||||
func TestFindItemInMap_WrongCase(t *testing.T) {
|
||||
v := make(map[KeyReference[string]]ValueReference[string])
|
||||
v[KeyReference[string]{
|
||||
func TestFindItemInOrderedMap_WrongCase(t *testing.T) {
|
||||
v := orderedmap.New[KeyReference[string], ValueReference[string]]()
|
||||
v.Set(KeyReference[string]{
|
||||
Value: "pizza",
|
||||
}] = ValueReference[string]{
|
||||
}, ValueReference[string]{
|
||||
Value: "pie",
|
||||
}
|
||||
assert.Equal(t, "pie", FindItemInMap("PIZZA", v).Value)
|
||||
})
|
||||
assert.Equal(t, "pie", FindItemInOrderedMap("PIZZA", v).Value)
|
||||
}
|
||||
|
||||
func TestFindItemInMap_Error(t *testing.T) {
|
||||
v := make(map[KeyReference[string]]ValueReference[string])
|
||||
v[KeyReference[string]{
|
||||
func TestFindItemInOrderedMap_Error(t *testing.T) {
|
||||
v := orderedmap.New[KeyReference[string], ValueReference[string]]()
|
||||
v.Set(KeyReference[string]{
|
||||
Value: "pizza",
|
||||
}] = ValueReference[string]{
|
||||
}, ValueReference[string]{
|
||||
Value: "pie",
|
||||
}
|
||||
assert.Nil(t, FindItemInMap("nuggets", v))
|
||||
})
|
||||
assert.Nil(t, FindItemInOrderedMap("nuggets", v))
|
||||
}
|
||||
|
||||
func TestLocateRefNode(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
cake:
|
||||
@@ -69,11 +70,9 @@ func TestLocateRefNode(t *testing.T) {
|
||||
|
||||
located, _, _ := LocateRefNode(cNode.Content[0], idx)
|
||||
assert.NotNil(t, located)
|
||||
|
||||
}
|
||||
|
||||
func TestLocateRefNode_BadNode(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
cake:
|
||||
@@ -94,11 +93,9 @@ func TestLocateRefNode_BadNode(t *testing.T) {
|
||||
// should both be empty.
|
||||
assert.Nil(t, located)
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestLocateRefNode_Path(t *testing.T) {
|
||||
|
||||
yml := `paths:
|
||||
/burger/time:
|
||||
description: hello`
|
||||
@@ -115,11 +112,9 @@ func TestLocateRefNode_Path(t *testing.T) {
|
||||
|
||||
located, _, _ := LocateRefNode(cNode.Content[0], idx)
|
||||
assert.NotNil(t, located)
|
||||
|
||||
}
|
||||
|
||||
func TestLocateRefNode_Path_NotFound(t *testing.T) {
|
||||
|
||||
yml := `paths:
|
||||
/burger/time:
|
||||
description: hello`
|
||||
@@ -137,7 +132,6 @@ func TestLocateRefNode_Path_NotFound(t *testing.T) {
|
||||
located, _, err := LocateRefNode(cNode.Content[0], idx)
|
||||
assert.Nil(t, located)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
type pizza struct {
|
||||
@@ -149,7 +143,6 @@ func (p *pizza) Build(_ context.Context, _, _ *yaml.Node, _ *index.SpecIndex) er
|
||||
}
|
||||
|
||||
func TestExtractObject(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -173,7 +166,6 @@ func TestExtractObject(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObject_Ref(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -197,7 +189,6 @@ func TestExtractObject_Ref(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObject_DoubleRef(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
cake:
|
||||
@@ -282,7 +273,6 @@ func TestExtractObject_DoubleRef_Circular_Fail(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObject_DoubleRef_Circular_Direct(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
loopy:
|
||||
@@ -312,7 +302,6 @@ func TestExtractObject_DoubleRef_Circular_Direct(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObject_DoubleRef_Circular_Direct_Fail(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
loopy:
|
||||
@@ -338,7 +327,6 @@ func TestExtractObject_DoubleRef_Circular_Direct_Fail(t *testing.T) {
|
||||
|
||||
_, err := ExtractObject[*pizza](context.Background(), "tags", cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
type test_borked struct {
|
||||
@@ -374,7 +362,6 @@ func (t *test_Good) Build(_ context.Context, _, root *yaml.Node, idx *index.Spec
|
||||
}
|
||||
|
||||
func TestExtractObject_BadLowLevelModel(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
hey:`
|
||||
@@ -391,11 +378,9 @@ func TestExtractObject_BadLowLevelModel(t *testing.T) {
|
||||
|
||||
_, err := ExtractObject[*test_noGood](context.Background(), "thing", &cNode, idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObject_BadBuild(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
hey:`
|
||||
@@ -412,11 +397,9 @@ func TestExtractObject_BadBuild(t *testing.T) {
|
||||
|
||||
_, err := ExtractObject[*test_almostGood](context.Background(), "thing", &cNode, idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObject_BadLabel(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
hey:`
|
||||
@@ -434,11 +417,9 @@ func TestExtractObject_BadLabel(t *testing.T) {
|
||||
res, err := ExtractObject[*test_almostGood](context.Background(), "ding", &cNode, idx)
|
||||
assert.Nil(t, res.Value)
|
||||
assert.NoError(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObject_PathIsCircular(t *testing.T) {
|
||||
|
||||
// first we need an index.
|
||||
yml := `paths:
|
||||
'/something/here':
|
||||
@@ -467,11 +448,9 @@ func TestExtractObject_PathIsCircular(t *testing.T) {
|
||||
res, err := ExtractObject[*test_Good](context.Background(), "thing", &rootNode, idx)
|
||||
assert.NotNil(t, res.Value)
|
||||
assert.Error(t, err) // circular error would have been thrown.
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObject_PathIsCircular_IgnoreErrors(t *testing.T) {
|
||||
|
||||
// first we need an index.
|
||||
yml := `paths:
|
||||
'/something/here':
|
||||
@@ -503,11 +482,9 @@ func TestExtractObject_PathIsCircular_IgnoreErrors(t *testing.T) {
|
||||
res, err := ExtractObject[*test_Good](context.Background(), "thing", &rootNode, idx)
|
||||
assert.NotNil(t, res.Value)
|
||||
assert.NoError(t, err) // circular error would have been thrown, but we're ignoring them.
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -530,7 +507,6 @@ func TestExtractObjectRaw(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_With_Ref(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -555,7 +531,6 @@ func TestExtractObjectRaw_With_Ref(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_Ref_Circular(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -579,11 +554,9 @@ func TestExtractObjectRaw_Ref_Circular(t *testing.T) {
|
||||
tag, err, _, _ := ExtractObjectRaw[*pizza](context.Background(), nil, cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.NotNil(t, tag)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_RefBroken(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -601,11 +574,9 @@ func TestExtractObjectRaw_RefBroken(t *testing.T) {
|
||||
tag, err, _, _ := ExtractObjectRaw[*pizza](context.Background(), nil, cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, tag)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_Ref_NonBuildable(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -622,11 +593,9 @@ func TestExtractObjectRaw_Ref_NonBuildable(t *testing.T) {
|
||||
|
||||
_, err, _, _ := ExtractObjectRaw[*test_noGood](context.Background(), nil, cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractObjectRaw_Ref_AlmostBuildable(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -643,11 +612,9 @@ func TestExtractObjectRaw_Ref_AlmostBuildable(t *testing.T) {
|
||||
|
||||
_, err, _, _ := ExtractObjectRaw[*test_almostGood](context.Background(), nil, cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
|
||||
}
|
||||
|
||||
func TestExtractArray(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -675,7 +642,6 @@ func TestExtractArray(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
things:
|
||||
@@ -702,7 +668,6 @@ func TestExtractArray_Ref(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Unbuildable(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
things:
|
||||
@@ -726,7 +691,6 @@ func TestExtractArray_Ref_Unbuildable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Circular(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -754,7 +718,6 @@ func TestExtractArray_Ref_Circular(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Bad(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -782,7 +745,6 @@ func TestExtractArray_Ref_Bad(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Nested(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -811,7 +773,6 @@ func TestExtractArray_Ref_Nested(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Nested_Circular(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -840,7 +801,6 @@ func TestExtractArray_Ref_Nested_Circular(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Nested_BadRef(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -867,7 +827,6 @@ func TestExtractArray_Ref_Nested_BadRef(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_Ref_Nested_CircularFlat(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -896,7 +855,6 @@ func TestExtractArray_Ref_Nested_CircularFlat(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_BadBuild(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:`
|
||||
@@ -918,7 +876,6 @@ func TestExtractArray_BadBuild(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractArray_BadRefPropsTupe(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
parameters:
|
||||
cakes:
|
||||
@@ -940,45 +897,7 @@ func TestExtractArray_BadRefPropsTupe(t *testing.T) {
|
||||
assert.Len(t, things, 0)
|
||||
}
|
||||
|
||||
func TestExtractExample_String(t *testing.T) {
|
||||
yml := `hi`
|
||||
var e yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &e)
|
||||
|
||||
exp := ExtractExample(e.Content[0], e.Content[0])
|
||||
assert.NotNil(t, exp.Value)
|
||||
assert.Equal(t, "hi", exp.Value)
|
||||
}
|
||||
func TestExtractExample_Map(t *testing.T) {
|
||||
yml := `one: two`
|
||||
var e yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &e)
|
||||
|
||||
exp := ExtractExample(e.Content[0], e.Content[0])
|
||||
assert.NotNil(t, exp.Value)
|
||||
if n, ok := exp.Value.(map[string]interface{}); ok {
|
||||
assert.Equal(t, "two", n["one"])
|
||||
} else {
|
||||
panic("example unpacked incorrectly.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractExample_Array(t *testing.T) {
|
||||
yml := `- hello`
|
||||
var e yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(yml), &e)
|
||||
|
||||
exp := ExtractExample(e.Content[0], e.Content[0])
|
||||
assert.NotNil(t, exp.Value)
|
||||
if n, ok := exp.Value.([]interface{}); ok {
|
||||
assert.Equal(t, "hello", n[0])
|
||||
} else {
|
||||
panic("example unpacked incorrectly.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractMapFlatNoLookup(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -997,11 +916,9 @@ one:
|
||||
things, err := ExtractMapNoLookup[*test_Good](context.Background(), cNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMap_NoLookupWithExtensions(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -1031,7 +948,6 @@ one:
|
||||
}
|
||||
|
||||
func TestExtractMap_NoLookupWithExtensions_UsingMerge(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -1053,11 +969,9 @@ one:
|
||||
things, err := ExtractMapNoLookupExtensions[*test_Good](context.Background(), cNode.Content[0], idx, true)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 4, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMap_NoLookupWithoutExtensions(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -1083,7 +997,6 @@ one:
|
||||
}
|
||||
|
||||
func TestExtractMap_WithExtensions(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -1105,7 +1018,6 @@ one:
|
||||
}
|
||||
|
||||
func TestExtractMap_WithoutExtensions(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -1127,7 +1039,6 @@ one:
|
||||
}
|
||||
|
||||
func TestExtractMapFlatNoLookup_Ref(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -1149,11 +1060,9 @@ one:
|
||||
things, err := ExtractMapNoLookup[*test_Good](context.Background(), cNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlatNoLookup_Ref_Bad(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -1175,11 +1084,9 @@ one:
|
||||
things, err := ExtractMapNoLookup[*test_Good](context.Background(), cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Zero(t, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlatNoLookup_Ref_Circular(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
thongs:
|
||||
@@ -1207,11 +1114,9 @@ one:
|
||||
things, err := ExtractMapNoLookup[*test_Good](context.Background(), cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 1, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlatNoLookup_Ref_BadBuild(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -1233,11 +1138,9 @@ hello:
|
||||
things, err := ExtractMapNoLookup[*test_noGood](context.Background(), cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Zero(t, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlatNoLookup_Ref_AlmostBuild(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
pizza:
|
||||
@@ -1259,11 +1162,9 @@ one:
|
||||
things, err := ExtractMapNoLookup[*test_almostGood](context.Background(), cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Zero(t, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat(t *testing.T) {
|
||||
|
||||
yml := `components:`
|
||||
|
||||
var idxNode yaml.Node
|
||||
@@ -1282,11 +1183,9 @@ one:
|
||||
things, _, _, err := ExtractMap[*test_Good](context.Background(), "one", cNode.Content[0], idx)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_Ref(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stank:
|
||||
@@ -1313,11 +1212,9 @@ one:
|
||||
for pair := orderedmap.First(things); pair != nil; pair = pair.Next() {
|
||||
assert.Equal(t, 99, pair.Value().Value.AlmostWork.Value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_DoubleRef(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stank:
|
||||
@@ -1346,7 +1243,6 @@ func TestExtractMapFlat_DoubleRef(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_DoubleRef_Error(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stank:
|
||||
@@ -1369,11 +1265,9 @@ func TestExtractMapFlat_DoubleRef_Error(t *testing.T) {
|
||||
things, _, _, err := ExtractMap[*test_almostGood](context.Background(), "one", cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Zero(t, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_DoubleRef_Error_NotFound(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stank:
|
||||
@@ -1396,11 +1290,9 @@ func TestExtractMapFlat_DoubleRef_Error_NotFound(t *testing.T) {
|
||||
things, _, _, err := ExtractMap[*test_almostGood](context.Background(), "one", cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Zero(t, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_DoubleRef_Circles(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stonk:
|
||||
@@ -1428,11 +1320,9 @@ func TestExtractMapFlat_DoubleRef_Circles(t *testing.T) {
|
||||
things, _, _, err := ExtractMap[*test_Good](context.Background(), "one", cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, 1, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_Ref_Error(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stank:
|
||||
@@ -1455,11 +1345,9 @@ func TestExtractMapFlat_Ref_Error(t *testing.T) {
|
||||
things, _, _, err := ExtractMap[*test_almostGood](context.Background(), "one", cNode.Content[0], idx)
|
||||
assert.Error(t, err)
|
||||
assert.Zero(t, orderedmap.Len(things))
|
||||
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_Ref_Circ_Error(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stink:
|
||||
@@ -1488,7 +1376,6 @@ func TestExtractMapFlat_Ref_Circ_Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_Ref_Nested_Circ_Error(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stink:
|
||||
@@ -1518,7 +1405,6 @@ func TestExtractMapFlat_Ref_Nested_Circ_Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_Ref_Nested_Error(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stink:
|
||||
@@ -1544,7 +1430,6 @@ func TestExtractMapFlat_Ref_Nested_Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_BadKey_Ref_Nested_Error(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stink:
|
||||
@@ -1570,7 +1455,6 @@ func TestExtractMapFlat_BadKey_Ref_Nested_Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractMapFlat_Ref_Bad(t *testing.T) {
|
||||
|
||||
yml := `components:
|
||||
schemas:
|
||||
stink:
|
||||
@@ -1599,7 +1483,6 @@ func TestExtractMapFlat_Ref_Bad(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestExtractExtensions(t *testing.T) {
|
||||
|
||||
yml := `x-bing: ding
|
||||
x-bong: 1
|
||||
x-ling: true
|
||||
@@ -1612,25 +1495,27 @@ x-tacos: [1,2,3]`
|
||||
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
||||
|
||||
r := ExtractExtensions(idxNode.Content[0])
|
||||
assert.Len(t, r, 6)
|
||||
for i := range r {
|
||||
switch i.Value {
|
||||
assert.Equal(t, 6, orderedmap.Len(r))
|
||||
for pair := orderedmap.First(r); pair != nil; pair = pair.Next() {
|
||||
var v any
|
||||
_ = pair.Value().Value.Decode(&v)
|
||||
|
||||
switch pair.Key().Value {
|
||||
case "x-bing":
|
||||
assert.Equal(t, "ding", r[i].Value)
|
||||
assert.Equal(t, "ding", v)
|
||||
case "x-bong":
|
||||
assert.Equal(t, int64(1), r[i].Value)
|
||||
assert.Equal(t, 1, v)
|
||||
case "x-ling":
|
||||
assert.Equal(t, true, r[i].Value)
|
||||
assert.Equal(t, true, v)
|
||||
case "x-long":
|
||||
assert.Equal(t, 0.99, r[i].Value)
|
||||
assert.Equal(t, 0.99, v)
|
||||
case "x-fish":
|
||||
if a, ok := r[i].Value.(map[string]interface{}); ok {
|
||||
assert.Equal(t, "yeah", a["woo"])
|
||||
} else {
|
||||
panic("should not fail casting")
|
||||
}
|
||||
var m map[string]any
|
||||
err := pair.Value().Value.Decode(&m)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "yeah", m["woo"])
|
||||
case "x-tacos":
|
||||
assert.Len(t, r[i].Value, 3)
|
||||
assert.Len(t, v, 3)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1650,8 +1535,8 @@ func (f test_fresh) Hash() [32]byte {
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(data, "|")))
|
||||
}
|
||||
func TestAreEqual(t *testing.T) {
|
||||
|
||||
func TestAreEqual(t *testing.T) {
|
||||
var hey *test_fresh
|
||||
|
||||
assert.True(t, AreEqual(test_fresh{val: "hello"}, test_fresh{val: "hello"}))
|
||||
@@ -1664,7 +1549,6 @@ func TestAreEqual(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGenerateHashString(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
|
||||
GenerateHashString(test_fresh{val: "hello"}))
|
||||
|
||||
@@ -1676,46 +1560,39 @@ func TestGenerateHashString(t *testing.T) {
|
||||
|
||||
assert.Equal(t, "",
|
||||
GenerateHashString(nil))
|
||||
|
||||
}
|
||||
|
||||
func TestGenerateHashString_Pointer(t *testing.T) {
|
||||
|
||||
val := true
|
||||
assert.Equal(t, "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b",
|
||||
GenerateHashString(test_fresh{thang: &val}))
|
||||
|
||||
assert.Equal(t, "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b",
|
||||
GenerateHashString(&val))
|
||||
|
||||
}
|
||||
|
||||
func TestSetReference(t *testing.T) {
|
||||
|
||||
type testObj struct {
|
||||
*Reference
|
||||
}
|
||||
|
||||
n := testObj{Reference: &Reference{}}
|
||||
SetReference(&n, "#/pigeon/street")
|
||||
SetReference(&n, "#/pigeon/street", nil)
|
||||
|
||||
assert.Equal(t, "#/pigeon/street", n.GetReference())
|
||||
|
||||
}
|
||||
|
||||
func TestSetReference_nil(t *testing.T) {
|
||||
|
||||
type testObj struct {
|
||||
*Reference
|
||||
}
|
||||
|
||||
n := testObj{Reference: &Reference{}}
|
||||
SetReference(nil, "#/pigeon/street")
|
||||
SetReference(nil, "#/pigeon/street", nil)
|
||||
assert.NotEqual(t, "#/pigeon/street", n.GetReference())
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_HttpLink(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1741,7 +1618,6 @@ func TestLocateRefNode_CurrentPathKey_HttpLink(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_HttpLink_Local(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1767,7 +1643,6 @@ func TestLocateRefNode_CurrentPathKey_HttpLink_Local(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1792,7 +1667,6 @@ func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx_WithPath(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1817,7 +1691,6 @@ func TestLocateRefNode_CurrentPathKey_HttpLink_RemoteCtx_WithPath(t *testing.T)
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_Path_Link(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1842,7 +1715,6 @@ func TestLocateRefNode_CurrentPathKey_Path_Link(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_Path_URL(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1869,7 +1741,6 @@ func TestLocateRefNode_CurrentPathKey_Path_URL(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_CurrentPathKey_DeeperPath_URL(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1896,7 +1767,6 @@ func TestLocateRefNode_CurrentPathKey_DeeperPath_URL(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_NoExplode(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1923,7 +1793,6 @@ func TestLocateRefNode_NoExplode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_NoExplode_HTTP(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1951,7 +1820,6 @@ func TestLocateRefNode_NoExplode_HTTP(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_NoExplode_NoSpecPath(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -1979,7 +1847,6 @@ func TestLocateRefNode_NoExplode_NoSpecPath(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefNode_DoARealLookup(t *testing.T) {
|
||||
|
||||
no := yaml.Node{
|
||||
Kind: yaml.MappingNode,
|
||||
Content: []*yaml.Node{
|
||||
@@ -2020,7 +1887,6 @@ func TestLocateRefNode_DoARealLookup(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefEndNoRef_NoName(t *testing.T) {
|
||||
|
||||
r := &yaml.Node{Content: []*yaml.Node{{Kind: yaml.ScalarNode, Value: "$ref"}, {Kind: yaml.ScalarNode, Value: ""}}}
|
||||
n, i, e, c := LocateRefEnd(nil, r, nil, 0)
|
||||
assert.Nil(t, n)
|
||||
@@ -2030,7 +1896,6 @@ func TestLocateRefEndNoRef_NoName(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefEndNoRef(t *testing.T) {
|
||||
|
||||
r := &yaml.Node{Content: []*yaml.Node{{Kind: yaml.ScalarNode, Value: "$ref"}, {Kind: yaml.ScalarNode, Value: "cake"}}}
|
||||
n, i, e, c := LocateRefEnd(context.Background(), r, index.NewSpecIndexWithConfig(r, index.CreateClosedAPIIndexConfig()), 0)
|
||||
assert.Nil(t, n)
|
||||
@@ -2049,7 +1914,6 @@ func TestLocateRefEnd_TooDeep(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefEnd_Loop(t *testing.T) {
|
||||
|
||||
yml, _ := os.ReadFile("../../test_specs/first.yaml")
|
||||
var bsn yaml.Node
|
||||
_ = yaml.Unmarshal(yml, &bsn)
|
||||
@@ -2094,7 +1958,6 @@ func TestLocateRefEnd_Loop(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefEnd_Loop_WithResolve(t *testing.T) {
|
||||
|
||||
yml, _ := os.ReadFile("../../test_specs/first.yaml")
|
||||
var bsn yaml.Node
|
||||
_ = yaml.Unmarshal(yml, &bsn)
|
||||
@@ -2139,7 +2002,6 @@ func TestLocateRefEnd_Loop_WithResolve(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLocateRefEnd_Empty(t *testing.T) {
|
||||
|
||||
yml, _ := os.ReadFile("../../test_specs/first.yaml")
|
||||
var bsn yaml.Node
|
||||
_ = yaml.Unmarshal(yml, &bsn)
|
||||
@@ -2184,7 +2046,6 @@ func TestLocateRefEnd_Empty(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestArray_NotRefNotArray(t *testing.T) {
|
||||
|
||||
yml := ``
|
||||
var idxNode yaml.Node
|
||||
mErr := yaml.Unmarshal([]byte(yml), &idxNode)
|
||||
@@ -2201,5 +2062,4 @@ func TestArray_NotRefNotArray(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, err.Error(), "array build failed, input is not an array, line 2, column 3")
|
||||
assert.Len(t, things, 0)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user