diff --git a/what-changed/model/comparison_functions.go b/what-changed/model/comparison_functions.go index 6095d15..e1e323b 100644 --- a/what-changed/model/comparison_functions.go +++ b/what-changed/model/comparison_functions.go @@ -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. 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) - 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 } } diff --git a/what-changed/model/comparison_functions_test.go b/what-changed/model/comparison_functions_test.go new file mode 100644 index 0000000..1243b73 --- /dev/null +++ b/what-changed/model/comparison_functions_test.go @@ -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) + } + }) + } +} diff --git a/what-changed/model/example.go b/what-changed/model/example.go index 42ec85e..14385c8 100644 --- a/what-changed/model/example.go +++ b/what-changed/model/example.go @@ -72,7 +72,7 @@ func CompareExamples(l, r *base.Example) *ExampleChanges { z := 0 for k := range l.Value.ValueNode.Content { 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++ } else { continue @@ -81,7 +81,7 @@ func CompareExamples(l, r *base.Example) *ExampleChanges { z = 0 for k := range r.Value.ValueNode.Content { 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++ } else { continue diff --git a/what-changed/model/example_test.go b/what-changed/model/example_test.go index 4e620a6..c2889fa 100644 --- a/what-changed/model/example_test.go +++ b/what-changed/model/example_test.go @@ -4,12 +4,14 @@ package model import ( + "testing" + + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "github.com/pb33f/libopenapi/datamodel/low" "github.com/pb33f/libopenapi/datamodel/low/base" v3 "github.com/pb33f/libopenapi/datamodel/low/v3" - "github.com/stretchr/testify/assert" - "gopkg.in/yaml.v3" - "testing" ) func TestCompareExamples_SummaryModified(t *testing.T) { @@ -196,3 +198,27 @@ func TestCompareExamples_Identical(t *testing.T) { extChanges := CompareExamples(&lDoc, &rDoc) 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()) +}