mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
Fix compare different node tag
This commit is contained in:
committed by
Dave Shanley
parent
fc7da1d38d
commit
d6031b5440
@@ -171,14 +171,8 @@ func CheckForAddition[T any](l, r *yaml.Node, label string, changes *[]*Change,
|
|||||||
//
|
//
|
||||||
// The Change is then added to the slice of []Change[T] instances provided as a pointer.
|
// The Change is then added to the slice of []Change[T] instances provided as a pointer.
|
||||||
func CheckForModification[T any](l, r *yaml.Node, label string, changes *[]*Change, breaking bool, orig, new T) {
|
func CheckForModification[T any](l, r *yaml.Node, label string, changes *[]*Change, breaking bool, orig, new T) {
|
||||||
if l != nil && l.Value != "" && r != nil && r.Value != "" && r.Value != l.Value && r.Tag == l.Tag {
|
if l != nil && l.Value != "" && r != nil && r.Value != "" && (r.Value != l.Value || r.Tag != l.Tag) {
|
||||||
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
|
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
|
||||||
return
|
|
||||||
}
|
|
||||||
// the values may have not changed, but the tag (node type) type may have
|
|
||||||
if l != nil && l.Value != "" && r != nil && r.Value != "" && r.Value != l.Value && r.Tag != l.Tag {
|
|
||||||
CreateChange(changes, Modified, label, l, r, breaking, orig, new)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
50
what-changed/model/comparison_functions_test.go
Normal file
50
what-changed/model/comparison_functions_test.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
// Copyright 2022 Hugo Stijns
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_CheckForModification(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
left string
|
||||||
|
right string
|
||||||
|
differ bool
|
||||||
|
}{
|
||||||
|
{"Same string quoted", `value`, `"value"`, false},
|
||||||
|
{"Same string", `value`, `value`, false},
|
||||||
|
{"Same boolean", `true`, `true`, false},
|
||||||
|
{"Different boolean", `true`, `false`, true},
|
||||||
|
{"Different string", `value_a`, `value_b`, true},
|
||||||
|
{"Different int", `123`, `"123"`, true},
|
||||||
|
{"Different float", `123.456`, `"123.456"`, true},
|
||||||
|
{"Different boolean quoted", `true`, `"true"`, true},
|
||||||
|
{"Different date", `2022-12-29`, `"2022-12-29"`, true},
|
||||||
|
{"Different value and tag", `2.0`, `2.0.0`, true},
|
||||||
|
{"From null to empty value", `null`, `""`, false},
|
||||||
|
{"From empty value to null", `""`, `null`, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var lNode, rNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(tt.left), &lNode)
|
||||||
|
_ = yaml.Unmarshal([]byte(tt.right), &rNode)
|
||||||
|
|
||||||
|
changes := []*Change{}
|
||||||
|
CheckForModification(lNode.Content[0], rNode.Content[0], "test", &changes, false, "old", "new")
|
||||||
|
|
||||||
|
if tt.differ {
|
||||||
|
assert.Len(t, changes, 1)
|
||||||
|
} else {
|
||||||
|
assert.Empty(t, changes)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,7 +72,7 @@ func CompareExamples(l, r *base.Example) *ExampleChanges {
|
|||||||
z := 0
|
z := 0
|
||||||
for k := range l.Value.ValueNode.Content {
|
for k := range l.Value.ValueNode.Content {
|
||||||
if k%2 == 0 {
|
if k%2 == 0 {
|
||||||
lKeys[z] = fmt.Sprintf("%v-%v", l.Value.ValueNode.Content[k].Value, l.Value.ValueNode.Content[k+1].Value)
|
lKeys[z] = fmt.Sprintf("%v-%v-%v", l.Value.ValueNode.Content[k].Value, l.Value.ValueNode.Content[k+1].Tag, l.Value.ValueNode.Content[k+1].Value)
|
||||||
z++
|
z++
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
@@ -81,7 +81,7 @@ func CompareExamples(l, r *base.Example) *ExampleChanges {
|
|||||||
z = 0
|
z = 0
|
||||||
for k := range r.Value.ValueNode.Content {
|
for k := range r.Value.ValueNode.Content {
|
||||||
if k%2 == 0 {
|
if k%2 == 0 {
|
||||||
rKeys[z] = fmt.Sprintf("%v-%v", r.Value.ValueNode.Content[k].Value, r.Value.ValueNode.Content[k+1].Value)
|
rKeys[z] = fmt.Sprintf("%v-%v-%v", r.Value.ValueNode.Content[k].Value, r.Value.ValueNode.Content[k+1].Tag, r.Value.ValueNode.Content[k+1].Value)
|
||||||
z++
|
z++
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -4,12 +4,14 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/pb33f/libopenapi/datamodel/low"
|
"github.com/pb33f/libopenapi/datamodel/low"
|
||||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||||
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
v3 "github.com/pb33f/libopenapi/datamodel/low/v3"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"testing"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCompareExamples_SummaryModified(t *testing.T) {
|
func TestCompareExamples_SummaryModified(t *testing.T) {
|
||||||
@@ -196,3 +198,27 @@ func TestCompareExamples_Identical(t *testing.T) {
|
|||||||
extChanges := CompareExamples(&lDoc, &rDoc)
|
extChanges := CompareExamples(&lDoc, &rDoc)
|
||||||
assert.Nil(t, extChanges)
|
assert.Nil(t, extChanges)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompareExamples_Date(t *testing.T) {
|
||||||
|
left := `value:
|
||||||
|
date: 2022-12-29`
|
||||||
|
|
||||||
|
right := `value:
|
||||||
|
date: "2022-12-29"`
|
||||||
|
|
||||||
|
var lNode, rNode yaml.Node
|
||||||
|
_ = yaml.Unmarshal([]byte(left), &lNode)
|
||||||
|
_ = yaml.Unmarshal([]byte(right), &rNode)
|
||||||
|
|
||||||
|
// create low level objects
|
||||||
|
var lDoc base.Example
|
||||||
|
var rDoc base.Example
|
||||||
|
_ = low.BuildModel(lNode.Content[0], &lDoc)
|
||||||
|
_ = low.BuildModel(rNode.Content[0], &rDoc)
|
||||||
|
_ = lDoc.Build(lNode.Content[0], nil)
|
||||||
|
_ = rDoc.Build(rNode.Content[0], nil)
|
||||||
|
|
||||||
|
changes := CompareExamples(&lDoc, &rDoc)
|
||||||
|
|
||||||
|
assert.Equal(t, 1, changes.TotalChanges())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user