Bumped low level v2 examples test coverage

This commit is contained in:
Dave Shanley
2022-11-05 09:55:13 -04:00
parent 77ecbd418f
commit 127c21b55e
2 changed files with 55 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"gopkg.in/yaml.v3"
"sort"
"strings"
)
@@ -77,8 +78,15 @@ func (e *Examples) Build(root *yaml.Node, _ *index.SpecIndex) error {
// Hash will return a consistent SHA256 Hash of the Examples object
func (e *Examples) Hash() [32]byte {
var f []string
keys := make([]string, len(e.Values))
z := 0
for k := range e.Values {
f = append(f, fmt.Sprint(e.Values[k].Value))
keys[z] = k.Value
z++
}
sort.Strings(keys)
for k := range keys {
f = append(f, fmt.Sprintf("%v", e.FindExample(keys[k]).Value))
}
return sha256.Sum256([]byte(strings.Join(f, "|")))
}

View File

@@ -2,3 +2,49 @@
// SPDX-License-Identifier: MIT
package v2
import (
"github.com/pb33f/libopenapi/datamodel/low"
"github.com/pb33f/libopenapi/index"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
)
func TestExamples_Hash(t *testing.T) {
yml := `something: string
yes:
- more
- water
anything:
cake: burger
nothing: int`
var idxNode yaml.Node
_ = yaml.Unmarshal([]byte(yml), &idxNode)
idx := index.NewSpecIndex(&idxNode)
var n Examples
_ = low.BuildModel(idxNode.Content[0], &n)
_ = n.Build(idxNode.Content[0], idx)
yml2 := `anything:
cake: burger
something: string
nothing: int
yes:
- more
- water`
var idxNode2 yaml.Node
_ = yaml.Unmarshal([]byte(yml2), &idxNode2)
idx2 := index.NewSpecIndex(&idxNode2)
var n2 Examples
_ = low.BuildModel(idxNode2.Content[0], &n2)
_ = n2.Build(idxNode2.Content[0], idx2)
assert.Equal(t, n.Hash(), n2.Hash())
}