mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
This is a large update, I realized that extensions are not being hashed correctly, and because I have the same code everywhere, it means running back through the stack and cleaning up the invalid code that will break if multiple extensions are used in different positions in the raw spec. At the same time, I realized that the v2 model has the same primitive/enum issues that are part cleaned up in v3. This is a breaking changhe because enums are now []any and not []string, as well as primitives for bool, int etc are all pointers now instead of the copied values. This will break any consumers.
113 lines
2.4 KiB
Go
113 lines
2.4 KiB
Go
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package base
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/pb33f/libopenapi/datamodel/low"
|
|
"github.com/pb33f/libopenapi/index"
|
|
"github.com/stretchr/testify/assert"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestInfo_Build(t *testing.T) {
|
|
|
|
yml := `title: pizza
|
|
description: pie
|
|
termsOfService: yes indeed.
|
|
contact:
|
|
name: buckaroo
|
|
url: https://pb33f.io
|
|
email: buckaroo@pb33f.io
|
|
license:
|
|
name: magic
|
|
url: https://pb33f.io/license
|
|
x-cli-name: pizza cli`
|
|
|
|
var idxNode yaml.Node
|
|
_ = yaml.Unmarshal([]byte(yml), &idxNode)
|
|
idx := index.NewSpecIndex(&idxNode)
|
|
|
|
var n Info
|
|
err := low.BuildModel(idxNode.Content[0], &n)
|
|
assert.NoError(t, err)
|
|
|
|
err = n.Build(idxNode.Content[0], idx)
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "pizza", n.Title.Value)
|
|
assert.Equal(t, "pie", n.Description.Value)
|
|
assert.Equal(t, "yes indeed.", n.TermsOfService.Value)
|
|
|
|
con := n.Contact.Value
|
|
assert.NotNil(t, con)
|
|
assert.Equal(t, "buckaroo", con.Name.Value)
|
|
assert.Equal(t, "https://pb33f.io", con.URL.Value)
|
|
assert.Equal(t, "buckaroo@pb33f.io", con.Email.Value)
|
|
|
|
lic := n.License.Value
|
|
assert.NotNil(t, lic)
|
|
assert.Equal(t, "magic", lic.Name.Value)
|
|
assert.Equal(t, "https://pb33f.io/license", lic.URL.Value)
|
|
|
|
cliName := n.FindExtension("x-cli-name")
|
|
assert.NotNil(t, cliName)
|
|
assert.Equal(t, "pizza cli", cliName.Value)
|
|
}
|
|
|
|
func TestContact_Build(t *testing.T) {
|
|
n := &Contact{}
|
|
k := n.Build(nil, nil)
|
|
assert.Nil(t, k)
|
|
}
|
|
|
|
func TestLicense_Build(t *testing.T) {
|
|
n := &License{}
|
|
k := n.Build(nil, nil)
|
|
assert.Nil(t, k)
|
|
}
|
|
|
|
func TestInfo_Hash(t *testing.T) {
|
|
|
|
left := `title: princess b33f
|
|
description: a thing
|
|
termsOfService: https://pb33f.io
|
|
x-princess: b33f
|
|
contact:
|
|
name: buckaroo
|
|
url: https://pb33f.io
|
|
license:
|
|
name: magic beans
|
|
version: 1.2.3
|
|
x-b33f: princess`
|
|
|
|
right := `title: princess b33f
|
|
description: a thing
|
|
termsOfService: https://pb33f.io
|
|
x-princess: b33f
|
|
contact:
|
|
name: buckaroo
|
|
url: https://pb33f.io
|
|
license:
|
|
name: magic beans
|
|
version: 1.2.3
|
|
x-b33f: princess`
|
|
|
|
var lNode, rNode yaml.Node
|
|
_ = yaml.Unmarshal([]byte(left), &lNode)
|
|
_ = yaml.Unmarshal([]byte(right), &rNode)
|
|
|
|
// create low level objects
|
|
var lDoc Info
|
|
var rDoc Info
|
|
_ = low.BuildModel(lNode.Content[0], &lDoc)
|
|
_ = low.BuildModel(rNode.Content[0], &rDoc)
|
|
_ = lDoc.Build(lNode.Content[0], nil)
|
|
_ = rDoc.Build(rNode.Content[0], nil)
|
|
|
|
assert.Equal(t, lDoc.Hash(), rDoc.Hash())
|
|
|
|
}
|