fix: continued moving everything to orderedmaps plus cleaned up most the tests

This commit is contained in:
Tristan Cartledge
2023-12-01 17:37:07 +00:00
parent 0f3d0cb28f
commit a4ad09aab3
169 changed files with 3435 additions and 3764 deletions

View File

@@ -5,15 +5,17 @@ package base
import (
"context"
"testing"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"testing"
)
func TestExample_Build_Success_NoValue(t *testing.T) {
yml := `summary: hot
description: cakes
x-cake: hot`
@@ -32,13 +34,13 @@ x-cake: hot`
assert.Equal(t, "hot", n.Summary.Value)
assert.Equal(t, "cakes", n.Description.Value)
assert.Nil(t, n.Value.Value)
ext := n.FindExtension("x-cake")
assert.NotNil(t, ext)
assert.Equal(t, "hot", ext.Value)
var xCake string
_ = n.FindExtension("x-cake").Value.Decode(&xCake)
assert.Equal(t, "hot", xCake)
}
func TestExample_Build_Success_Simple(t *testing.T) {
yml := `summary: hot
description: cakes
value: a string example
@@ -57,14 +59,17 @@ x-cake: hot`
assert.NoError(t, err)
assert.Equal(t, "hot", n.Summary.Value)
assert.Equal(t, "cakes", n.Description.Value)
assert.Equal(t, "a string example", n.Value.Value)
ext := n.FindExtension("x-cake")
assert.NotNil(t, ext)
assert.Equal(t, "hot", ext.Value)
var example string
err = n.Value.Value.Decode(&example)
assert.Equal(t, "a string example", example)
var xCake string
_ = n.FindExtension("x-cake").Value.Decode(&xCake)
assert.Equal(t, "hot", xCake)
}
func TestExample_Build_Success_Object(t *testing.T) {
yml := `summary: hot
description: cakes
value:
@@ -85,17 +90,15 @@ value:
assert.Equal(t, "hot", n.Summary.Value)
assert.Equal(t, "cakes", n.Description.Value)
if v, ok := n.Value.Value.(map[string]interface{}); ok {
assert.Equal(t, "oven", v["pizza"])
assert.Equal(t, "pizza", v["yummy"])
} else {
assert.Fail(t, "failed to decode correctly.")
}
var m map[string]interface{}
err = n.Value.Value.Decode(&m)
require.NoError(t, err)
assert.Equal(t, "oven", m["pizza"])
assert.Equal(t, "pizza", m["yummy"])
}
func TestExample_Build_Success_Array(t *testing.T) {
yml := `summary: hot
description: cakes
value:
@@ -116,16 +119,15 @@ value:
assert.Equal(t, "hot", n.Summary.Value)
assert.Equal(t, "cakes", n.Description.Value)
if v, ok := n.Value.Value.([]interface{}); ok {
assert.Equal(t, "wow", v[0])
assert.Equal(t, "such array", v[1])
} else {
assert.Fail(t, "failed to decode correctly.")
}
var a []any
err = n.Value.Value.Decode(&a)
require.NoError(t, err)
assert.Equal(t, "wow", a[0])
assert.Equal(t, "such array", a[1])
}
func TestExample_Build_Success_MergeNode(t *testing.T) {
yml := `x-things: &things
summary: hot
description: cakes
@@ -148,71 +150,15 @@ func TestExample_Build_Success_MergeNode(t *testing.T) {
assert.Equal(t, "hot", n.Summary.Value)
assert.Equal(t, "cakes", n.Description.Value)
if v, ok := n.Value.Value.([]interface{}); ok {
assert.Equal(t, "wow", v[0])
assert.Equal(t, "such array", v[1])
} else {
assert.Fail(t, "failed to decode correctly.")
}
var a []any
err = n.Value.GetValue().Decode(&a)
require.NoError(t, err)
}
func TestExample_ExtractExampleValue_Map(t *testing.T) {
yml := `hot:
summer: nights
pizza: oven`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
val := ExtractExampleValue(idxNode.Content[0])
if v, ok := val.(map[string]interface{}); ok {
if r, rok := v["hot"].(map[string]interface{}); rok {
assert.Equal(t, "nights", r["summer"])
assert.Equal(t, "oven", r["pizza"])
} else {
assert.Fail(t, "failed to decode correctly.")
}
} else {
assert.Fail(t, "failed to decode correctly.")
}
}
func TestExample_ExtractExampleValue_Slice(t *testing.T) {
yml := `- hot:
summer: nights
- hotter:
pizza: oven`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
val := ExtractExampleValue(idxNode.Content[0])
if v, ok := val.([]interface{}); ok {
for w := range v {
if r, rok := v[w].(map[string]interface{}); rok {
for k := range r {
if k == "hotter" {
assert.Equal(t, "oven", r[k].(map[string]interface{})["pizza"])
}
if k == "hot" {
assert.Equal(t, "nights", r[k].(map[string]interface{})["summer"])
}
}
} else {
assert.Fail(t, "failed to decode correctly.")
}
}
} else {
assert.Fail(t, "failed to decode correctly.")
}
assert.Equal(t, "wow", a[0])
assert.Equal(t, "such array", a[1])
}
func TestExample_Hash(t *testing.T) {
left := `summary: hot
description: cakes
x-burger: nice
@@ -242,13 +188,5 @@ x-burger: nice`
_ = rDoc.Build(context.Background(), nil, rNode.Content[0], nil)
assert.Equal(t, lDoc.Hash(), rDoc.Hash())
assert.Len(t, lDoc.GetExtensions(), 1)
}
func TestExtractExampleValue(t *testing.T) {
assert.True(t, ExtractExampleValue(&yaml.Node{Tag: "!!bool", Value: "true"}).(bool))
assert.Equal(t, int64(10), ExtractExampleValue(&yaml.Node{Tag: "!!int", Value: "10"}).(int64))
assert.Equal(t, 33.2, ExtractExampleValue(&yaml.Node{Tag: "!!float", Value: "33.2"}).(float64))
assert.Equal(t, "WHAT A NICE COW", ExtractExampleValue(&yaml.Node{Tag: "!!str", Value: "WHAT A NICE COW"}))
assert.Equal(t, 1, orderedmap.Len(lDoc.GetExtensions()))
}