mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
bumping test coverage, adding more tests.
Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
@@ -57,3 +57,42 @@ url: https://pb33f.io/not-real
|
||||
assert.Equal(t, yml, string(bytes))
|
||||
|
||||
}
|
||||
|
||||
func TestLicense_Render_Identifier(t *testing.T) {
|
||||
|
||||
highL := &License{Name: "MIT", Identifier: "MIT"}
|
||||
dat, _ := highL.Render()
|
||||
|
||||
// unmarshal yaml into a *yaml.Node instance
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal(dat, &cNode)
|
||||
|
||||
// build low
|
||||
var lowLicense lowbase.License
|
||||
_ = lowmodel.BuildModel(cNode.Content[0], &lowLicense)
|
||||
|
||||
// build high
|
||||
highLicense := NewLicense(&lowLicense)
|
||||
|
||||
assert.Equal(t, "MIT", highLicense.Name)
|
||||
assert.Equal(t, "MIT", highLicense.Identifier)
|
||||
|
||||
}
|
||||
|
||||
func TestLicense_Render_IdentifierAndURL_Error(t *testing.T) {
|
||||
|
||||
// this should fail because you can't have both an identifier and a URL
|
||||
highL := &License{Name: "MIT", Identifier: "MIT", URL: "https://pb33f.io"}
|
||||
dat, _ := highL.Render()
|
||||
|
||||
// unmarshal yaml into a *yaml.Node instance
|
||||
var cNode yaml.Node
|
||||
_ = yaml.Unmarshal(dat, &cNode)
|
||||
|
||||
// build low
|
||||
var lowLicense lowbase.License
|
||||
_ = lowmodel.BuildModel(cNode.Content[0], &lowLicense)
|
||||
err := lowLicense.Build(cNode.Content[0], nil)
|
||||
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
@@ -1597,6 +1597,9 @@ func TestSchema_UnevaluatedPropertiesAsBool_DefinedAsTrue(t *testing.T) {
|
||||
assert.True(t, res.Value.Schema().UnevaluatedProperties.Value.IsB())
|
||||
assert.True(t, *res.Value.Schema().UnevaluatedProperties.Value.B)
|
||||
|
||||
assert.Equal(t, "571bd1853c22393131e2dcadce86894da714ec14968895c8b7ed18154b2be8cd",
|
||||
low.GenerateHashString(res.Value.Schema().UnevaluatedProperties.Value))
|
||||
|
||||
}
|
||||
|
||||
func TestSchema_UnevaluatedPropertiesAsBool_DefinedAsFalse(t *testing.T) {
|
||||
|
||||
@@ -6,6 +6,7 @@ package low
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -685,5 +686,9 @@ func GenerateHashString(v any) string {
|
||||
return fmt.Sprintf(HASH, h.Hash())
|
||||
}
|
||||
}
|
||||
// if we get here, we're a primitive, check if we're a pointer and de-point
|
||||
if reflect.TypeOf(v).Kind() == reflect.Ptr {
|
||||
v = reflect.ValueOf(v).Elem().Interface()
|
||||
}
|
||||
return fmt.Sprintf(HASH, sha256.Sum256([]byte(fmt.Sprint(v))))
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
@@ -1615,11 +1616,19 @@ x-tacos: [1,2,3]`
|
||||
}
|
||||
|
||||
type test_fresh struct {
|
||||
val string
|
||||
val string
|
||||
thang *bool
|
||||
}
|
||||
|
||||
func (f test_fresh) Hash() [32]byte {
|
||||
return sha256.Sum256([]byte(f.val))
|
||||
var data []string
|
||||
if f.val != "" {
|
||||
data = append(data, f.val)
|
||||
}
|
||||
if f.thang != nil {
|
||||
data = append(data, fmt.Sprintf("%v", *f.thang))
|
||||
}
|
||||
return sha256.Sum256([]byte(strings.Join(data, "|")))
|
||||
}
|
||||
func TestAreEqual(t *testing.T) {
|
||||
assert.True(t, AreEqual(test_fresh{val: "hello"}, test_fresh{val: "hello"}))
|
||||
@@ -1637,6 +1646,17 @@ func TestGenerateHashString(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -797,3 +797,9 @@ func TestCheckEnumForDuplicates_FailMultiple(t *testing.T) {
|
||||
yaml.Unmarshal([]byte(yml), &rootNode)
|
||||
assert.Len(t, CheckEnumForDuplicates(rootNode.Content[0].Content), 3)
|
||||
}
|
||||
|
||||
func TestConvertComponentIdIntoFriendlyPathSearch_Brackets(t *testing.T) {
|
||||
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/OhNoWhy[HaveYouDoneThis]")
|
||||
assert.Equal(t, "$.components.schemas['OhNoWhy[HaveYouDoneThis]']", path)
|
||||
assert.Equal(t, "OhNoWhy[HaveYouDoneThis]", segment)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user