diff now does sort first

This commit is contained in:
2026-10-19 20:12:33 +02:00
parent 67ce736fd0
commit 5137646157
5 changed files with 105 additions and 3 deletions
+76
View File
@@ -0,0 +1,76 @@
package main
import (
"testing"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type DiffSuite struct {
suite.Suite
}
func TestDiffSuite(t *testing.T) {
suite.Run(t, new(DiffSuite))
}
func (s *DiffSuite) TestCompareIgnoresTopLevelMapKeyOrder() {
diff1, diff2 := s.compareDocuments(`
first: 1
second: 2
`, `
second: 2
first: 1
`)
require.Empty(s.T(), diff1)
require.Empty(s.T(), diff2)
}
func (s *DiffSuite) TestCompareIgnoresMapKeyOrderInArray() {
diff1, diff2 := s.compareDocuments(`
items:
- name: alpha
value: 1
- name: beta
value: 2
`, `
items:
- value: 1
name: alpha
- value: 2
name: beta
`)
require.Empty(s.T(), diff1)
require.Empty(s.T(), diff2)
}
func (s *DiffSuite) TestCompareStillDetectsChangedMapValue() {
diff1, diff2 := s.compareDocuments(`
items:
- name: alpha
value: 1
`, `
items:
- value: 2
name: alpha
`)
require.NotEmpty(s.T(), diff1)
require.NotEmpty(s.T(), diff2)
}
func (s *DiffSuite) compareDocuments(yaml1Data, yaml2Data string) (yaml.MapSlice, yaml.MapSlice) {
s.T().Helper()
yaml1, err := parse([]byte(yaml1Data))
require.NoError(s.T(), err)
yaml2, err := parse([]byte(yaml2Data))
require.NoError(s.T(), err)
diff1, diff2 := compare(yaml1, yaml2)
return diff1, diff2
}