File: equals_test.go

package info (click to toggle)
golang-k8s-sigs-structured-merge-diff 4.1.2%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,580 kB
  • sloc: makefile: 4
file content (168 lines) | stat: -rw-r--r-- 4,013 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package schema

import (
	"math/rand"
	"reflect"
	"testing"
	"testing/quick"

	fuzz "github.com/google/gofuzz"
)

func fuzzInterface(i *interface{}, c fuzz.Continue) {
	m := map[string]string{}
	c.Fuzz(&m)
	*i = &m
}

func (Schema) Generate(rand *rand.Rand, size int) reflect.Value {
	s := Schema{}
	f := fuzz.New().RandSource(rand).MaxDepth(4)
	f.Fuzz(&s)
	return reflect.ValueOf(s)
}

func (Map) Generate(rand *rand.Rand, size int) reflect.Value {
	m := Map{}
	f := fuzz.New().RandSource(rand).MaxDepth(4).Funcs(fuzzInterface)
	f.Fuzz(&m)
	return reflect.ValueOf(m)
}

func (TypeDef) Generate(rand *rand.Rand, size int) reflect.Value {
	td := TypeDef{}
	f := fuzz.New().RandSource(rand).MaxDepth(4)
	f.Fuzz(&td)
	return reflect.ValueOf(td)
}

func (Atom) Generate(rand *rand.Rand, size int) reflect.Value {
	a := Atom{}
	f := fuzz.New().RandSource(rand).MaxDepth(4)
	f.Fuzz(&a)
	return reflect.ValueOf(a)
}

func (StructField) Generate(rand *rand.Rand, size int) reflect.Value {
	a := StructField{}
	f := fuzz.New().RandSource(rand).MaxDepth(4).Funcs(fuzzInterface)
	f.Fuzz(&a)
	return reflect.ValueOf(a)
}

func TestEquals(t *testing.T) {
	// In general this test will make sure people update things when they
	// add a field.
	//
	// The "copy known fields" section of these function is to break if folks
	// add new fields without fixing the Equals function and this test.
	funcs := []interface{}{
		func(x Schema) bool {
			if !x.Equals(&x) {
				return false
			}
			var y Schema
			y.Types = x.Types
			return x.Equals(&y) == reflect.DeepEqual(&x, &y)
		},
		func(x TypeDef) bool {
			if !x.Equals(&x) {
				return false
			}
			var y TypeDef
			y.Name = x.Name
			y.Atom = x.Atom
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
		func(x TypeRef) bool {
			if !x.Equals(&x) {
				return false
			}
			var y TypeRef
			y.NamedType = x.NamedType
			y.Inlined = x.Inlined
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
		func(x Atom) bool {
			if !x.Equals(&x) {
				return false
			}
			var y Atom
			y.Scalar = x.Scalar
			y.List = x.List
			y.Map = x.Map
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
		func(x Map) bool {
			if !x.Equals(&x) {
				return false
			}
			var y Map
			y.ElementType = x.ElementType
			y.ElementRelationship = x.ElementRelationship
			y.Fields = x.Fields
			y.Unions = x.Unions
			return x.Equals(&y) == reflect.DeepEqual(&x, &y)
		},
		func(x Union) bool {
			if !x.Equals(&x) {
				return false
			}
			var y Union
			y.Discriminator = x.Discriminator
			y.DeduceInvalidDiscriminator = x.DeduceInvalidDiscriminator
			y.Fields = x.Fields
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
		func(x UnionField) bool {
			if !x.Equals(&x) {
				return false
			}
			var y UnionField
			y.DiscriminatorValue = x.DiscriminatorValue
			y.FieldName = x.FieldName
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
		func(x StructField) bool {
			if !x.Equals(&x) {
				return false
			}
			var y StructField
			y.Name = x.Name
			y.Type = x.Type
			y.Default = x.Default
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
		func(x List) bool {
			if !x.Equals(&x) {
				return false
			}
			var y List
			y.ElementType = x.ElementType
			y.ElementRelationship = x.ElementRelationship
			y.Keys = x.Keys
			return x.Equals(&y) == reflect.DeepEqual(x, y)
		},
	}
	for i, f := range funcs {
		if err := quick.Check(f, nil); err != nil {
			t.Errorf("%v: %v", i, err)
		}
	}
}