fix: cleanup tests

This commit is contained in:
Tristan Cartledge
2023-12-03 20:41:54 +00:00
parent d669ada7b6
commit 6904f4d459
7 changed files with 189 additions and 165 deletions

View File

@@ -55,16 +55,8 @@ func (o *Map[K, V]) ToYamlNode(n NodeBuilder, l any) *yaml.Node {
for pair := First(o); pair != nil; pair = pair.Next() {
var k any = pair.Key()
if m, ok := k.(marshaler); ok { // TODO marshal inline?
k, _ = m.MarshalYAML()
}
var y any
y, ok := k.(yaml.Node)
if !ok {
y, ok = k.(*yaml.Node)
}
if ok {
b, _ := yaml.Marshal(y)
mk, _ := m.MarshalYAML()
b, _ := yaml.Marshal(mk)
k = strings.TrimSpace(string(b))
}

140
orderedmap/builder_test.go Normal file
View File

@@ -0,0 +1,140 @@
package orderedmap_test
import (
"testing"
"github.com/pb33f/libopenapi/datamodel/high"
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/orderedmap"
"github.com/pb33f/libopenapi/utils"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestOrderedMap_ToYamlNode(t *testing.T) {
type args struct {
om any
low any
}
tests := []struct {
name string
args args
want string
}{
{
name: "simple ordered map",
args: args{
om: orderedmap.ToOrderedMap(map[string]string{
"one": "two",
"three": "four",
}),
},
want: `one: two
three: four
`,
},
{
name: "simple ordered map with low representation",
args: args{
om: orderedmap.ToOrderedMap(map[string]string{
"one": "two",
"three": "four",
}),
low: low.NodeReference[*orderedmap.Map[*low.KeyReference[string], *low.ValueReference[string]]]{
Value: orderedmap.ToOrderedMap(map[*low.KeyReference[string]]*low.ValueReference[string]{
{Value: "one", KeyNode: utils.CreateStringNode("one")}: {Value: "two", ValueNode: utils.CreateStringNode("two")},
}),
ValueNode: utils.CreateYamlNode(orderedmap.ToOrderedMap(map[string]string{
"one": "two",
"three": "four",
})),
},
},
want: `one: two
three: four
`,
},
{
name: "ordered map with KeyReference",
args: args{
om: orderedmap.ToOrderedMap(map[*low.KeyReference[string]]string{
{
KeyNode: utils.CreateStringNode("one"),
}: "two",
{
KeyNode: utils.CreateStringNode("three"),
}: "four",
}),
},
want: `one: two
three: four
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nb := new(high.NodeBuilder)
node := tt.args.om.(orderedmap.MapToYamlNoder).ToYamlNode(nb, tt.args.low)
b, err := yaml.Marshal(node)
require.NoError(t, err)
require.Equal(t, tt.want, string(b))
})
}
}
type findValueUntyped interface {
FindValueUntyped(k string) any
}
func TestOrderedMap_FindValueUntyped(t *testing.T) {
type args struct {
om any
key string
}
tests := []struct {
name string
args args
want any
}{
{
name: "find value in simple ordered map",
args: args{
om: orderedmap.ToOrderedMap(map[string]string{
"one": "two",
"three": "four",
}),
key: "one",
},
want: "two",
},
{
name: "unable to find value in simple ordered map",
args: args{
om: orderedmap.ToOrderedMap(map[string]string{
"one": "two",
"three": "four",
}),
key: "five",
},
want: nil,
},
{
name: "find value in ordered map with KeyReference",
args: args{
om: orderedmap.ToOrderedMap(map[*low.KeyReference[string]]string{
{Value: "one"}: "two",
{Value: "three"}: "four",
}),
key: "three",
},
want: "four",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value := tt.args.om.(findValueUntyped).FindValueUntyped(tt.args.key)
require.Equal(t, tt.want, value)
})
}
}

View File

@@ -157,7 +157,7 @@ func ToOrderedMap[K comparable, V any](m map[K]V) *Map[K, V] {
for k, v := range m {
om.Set(k, v)
}
return om
return SortAlpha(om)
}
// First returns map's first pair for iteration.