Skip to content

Commit c3b5bf5

Browse files
committed
fieldpath: convert to blackbox testing
The tests were defined in the "fieldpath" package, which prevented using typed.NewParser (import cycle!). Defining them in "fieldpath_test" allows that. To avoid changing all references to the package's exported symbols, "fieldpath_test.go" defines aliases. typed.NewParser is more strict and finds a typo in the test schema.
1 parent 630be75 commit c3b5bf5

File tree

4 files changed

+126
-37
lines changed

4 files changed

+126
-37
lines changed

fieldpath/fieldpath_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package fieldpath_test
18+
19+
import (
20+
"sigs.k8s.io/structured-merge-diff/v6/fieldpath"
21+
"sigs.k8s.io/structured-merge-diff/v6/value"
22+
)
23+
24+
// Type and function aliases to avoid typing...
25+
26+
type (
27+
Filter = fieldpath.Filter
28+
Path = fieldpath.Path
29+
PathElement = fieldpath.PathElement
30+
Set = fieldpath.Set
31+
SetNodeMap = fieldpath.SetNodeMap
32+
)
33+
34+
var (
35+
KeyByFields = fieldpath.KeyByFields
36+
MakePathOrDie = fieldpath.MakePathOrDie
37+
MakePrefixMatcherOrDie = fieldpath.MakePrefixMatcherOrDie
38+
MatchAnyPathElement = fieldpath.MatchAnyPathElement
39+
NewIncludeMatcherFilter = fieldpath.NewIncludeMatcherFilter
40+
NewSet = fieldpath.NewSet
41+
42+
// Short names for readable test cases.
43+
_V = value.NewValueInterface
44+
_NS = fieldpath.NewSet
45+
_P = fieldpath.MakePathOrDie
46+
)

fieldpath/managers_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ import (
2323
"sigs.k8s.io/structured-merge-diff/v6/fieldpath"
2424
)
2525

26-
var (
27-
// Short names for readable test cases.
28-
_NS = fieldpath.NewSet
29-
_P = fieldpath.MakePathOrDie
30-
)
31-
3226
func TestManagersEquals(t *testing.T) {
3327
tests := []struct {
3428
name string

fieldpath/serialize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package fieldpath
17+
package fieldpath_test
1818

1919
import (
2020
"bytes"

fieldpath/set_test.go

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package fieldpath
17+
package fieldpath_test
1818

1919
import (
2020
"bytes"
2121
"fmt"
2222
"math/rand"
2323
"testing"
2424

25-
"sigs.k8s.io/structured-merge-diff/v6/value"
26-
27-
yaml "go.yaml.in/yaml/v2"
2825
"sigs.k8s.io/structured-merge-diff/v6/schema"
26+
"sigs.k8s.io/structured-merge-diff/v6/typed"
27+
"sigs.k8s.io/structured-merge-diff/v6/value"
2928
)
3029

3130
type randomPathAlphabet []PathElement
@@ -664,80 +663,130 @@ func TestSetDifference(t *testing.T) {
664663
}
665664
}
666665

667-
var nestedSchema = func() (*schema.Schema, schema.TypeRef) {
668-
sc := &schema.Schema{}
666+
var nestedSchema = func() (*typed.Parser, string) {
669667
name := "type"
670-
err := yaml.Unmarshal([]byte(`types:
668+
parser := mustParse(`types:
671669
- name: type
672670
map:
673671
elementType:
674672
namedType: type
675673
fields:
674+
- name: keyAStr
675+
type:
676+
scalar: string
677+
- name: keyBInt
678+
type:
679+
scalar: numeric
676680
- name: named
677681
type:
678682
namedType: type
679683
- name: list
680684
type:
681685
list:
682-
elementRelationShip: associative
683-
keys: ["name"]
686+
elementRelationship: associative
687+
keys: ["keyAStr", "keyBInt"]
684688
elementType:
685689
namedType: type
690+
- name: a
691+
type:
692+
namedType: type
686693
- name: value
687694
type:
688695
scalar: numeric
689-
`), &sc)
696+
`)
697+
return parser, name
698+
}
699+
700+
func mustParse(schema typed.YAMLObject) *typed.Parser {
701+
parser, err := typed.NewParser(schema)
690702
if err != nil {
691703
panic(err)
692704
}
693-
return sc, schema.TypeRef{NamedType: &name}
705+
return parser
694706
}
695707

696-
var _P = MakePathOrDie
697-
698708
func TestEnsureNamedFieldsAreMembers(t *testing.T) {
699709
table := []struct {
700-
set, expected *Set
710+
value typed.YAMLObject
711+
expectedBefore *Set
712+
expectedAfter *Set
701713
}{
702714
{
703-
set: NewSet(_P("named", "named", "value")),
704-
expected: NewSet(
715+
value: `{"named": {"named": {"value": 0}}}`,
716+
expectedBefore: NewSet(
717+
_P("named", "named", "value"),
718+
),
719+
expectedAfter: NewSet(
705720
_P("named", "named", "value"),
706721
_P("named", "named"),
707722
_P("named"),
708723
),
709724
},
710725
{
711-
set: NewSet(_P("named", "a", "named", "value"), _P("a", "named", "value"), _P("a", "b", "value")),
712-
expected: NewSet(
726+
value: `{"named": {"a": {"named": {"value": 42}}}, "a": {"named": {"value": 1}}}`,
727+
expectedBefore: NewSet(
728+
_P("named", "a", "named", "value"),
729+
_P("a", "named", "value"),
730+
),
731+
expectedAfter: NewSet(
713732
_P("named", "a", "named", "value"),
714733
_P("named", "a", "named"),
734+
_P("named", "a"),
715735
_P("named"),
716736
_P("a", "named", "value"),
717737
_P("a", "named"),
718-
_P("a", "b", "value"),
738+
_P("a"),
719739
),
720740
},
721741
{
722-
set: NewSet(_P("named", "list", KeyByFields("name", "a"), "named", "a", "value")),
723-
expected: NewSet(
724-
_P("named", "list", KeyByFields("name", "a"), "named", "a", "value"),
725-
_P("named", "list", KeyByFields("name", "a"), "named"),
742+
value: `{"named": {"list": [{"keyAStr": "a", "keyBInt": 1, "named": {"value": 0}}]}}`,
743+
expectedBefore: NewSet(
744+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "keyAStr"),
745+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "keyBInt"),
746+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "named", "value"),
747+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1)),
748+
),
749+
expectedAfter: NewSet(
750+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "keyAStr"),
751+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "keyBInt"),
752+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "named", "value"),
753+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1), "named"),
754+
_P("named", "list", KeyByFields("keyAStr", "a", "keyBInt", 1)),
726755
_P("named", "list"),
727756
_P("named"),
728757
),
729758
},
730759
}
731760

732761
for _, test := range table {
733-
t.Run(fmt.Sprintf("%v", test.set), func(t *testing.T) {
734-
got := test.set.EnsureNamedFieldsAreMembers(nestedSchema())
735-
if !got.Equals(test.expected) {
736-
t.Errorf("expected %v, got %v (missing: %v/superfluous: %v)",
737-
test.expected,
762+
t.Run(string(test.value), func(t *testing.T) {
763+
parser, typeName := nestedSchema()
764+
typeRef := schema.TypeRef{NamedType: &typeName}
765+
typedValue, err := parser.Type(typeName).FromYAML(test.value)
766+
if err != nil {
767+
t.Fatalf("unexpected error parsing test value: %v", err)
768+
}
769+
set, err := typedValue.ToFieldSet()
770+
if err != nil {
771+
t.Fatalf("unexpected error converting test value to set: %v", err)
772+
}
773+
if !set.Equals(test.expectedBefore) {
774+
t.Errorf("expected before EnsureNamedFieldsAreMembers:\n%v\n\ngot:\n%v\n\nmissing:\n%v\n\nsuperfluous:\n\n%v",
775+
test.expectedBefore,
776+
set,
777+
test.expectedAfter.Difference(set),
778+
set.Difference(test.expectedAfter),
779+
)
780+
}
781+
782+
schema := &parser.Schema
783+
got := set.EnsureNamedFieldsAreMembers(schema, typeRef)
784+
if !got.Equals(test.expectedAfter) {
785+
t.Errorf("expected after EnsureNamedFieldsAreMembers:\n%v\n\ngot:\n%v\n\nmissing:\n%v\n\nsuperfluous:\n\n%v",
786+
test.expectedAfter,
738787
got,
739-
test.expected.Difference(got),
740-
got.Difference(test.expected),
788+
test.expectedAfter.Difference(got),
789+
got.Difference(test.expectedAfter),
741790
)
742791
}
743792
})

0 commit comments

Comments
 (0)