...

Source file src/sigs.k8s.io/structured-merge-diff/v4/schema/equals_test.go

Documentation: sigs.k8s.io/structured-merge-diff/v4/schema

     1  /*
     2  Copyright 2019 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 schema
    18  
    19  import (
    20  	"math/rand"
    21  	"reflect"
    22  	"testing"
    23  	"testing/quick"
    24  
    25  	fuzz "github.com/google/gofuzz"
    26  )
    27  
    28  func fuzzInterface(i *interface{}, c fuzz.Continue) {
    29  	m := map[string]string{}
    30  	c.Fuzz(&m)
    31  	*i = &m
    32  }
    33  
    34  func (*Schema) Generate(rand *rand.Rand, size int) reflect.Value {
    35  	s := Schema{}
    36  	f := fuzz.New().RandSource(rand).MaxDepth(4)
    37  	f.Fuzz(&s)
    38  	return reflect.ValueOf(&s)
    39  }
    40  
    41  func (*Map) Generate(rand *rand.Rand, size int) reflect.Value {
    42  	m := Map{}
    43  	f := fuzz.New().RandSource(rand).MaxDepth(4).Funcs(fuzzInterface)
    44  	f.Fuzz(&m)
    45  	return reflect.ValueOf(&m)
    46  }
    47  
    48  func (TypeDef) Generate(rand *rand.Rand, size int) reflect.Value {
    49  	td := TypeDef{}
    50  	f := fuzz.New().RandSource(rand).MaxDepth(4)
    51  	f.Fuzz(&td)
    52  	return reflect.ValueOf(td)
    53  }
    54  
    55  func (Atom) Generate(rand *rand.Rand, size int) reflect.Value {
    56  	a := Atom{}
    57  	f := fuzz.New().RandSource(rand).MaxDepth(4)
    58  	f.Fuzz(&a)
    59  	return reflect.ValueOf(a)
    60  }
    61  
    62  func (StructField) Generate(rand *rand.Rand, size int) reflect.Value {
    63  	a := StructField{}
    64  	f := fuzz.New().RandSource(rand).MaxDepth(4).Funcs(fuzzInterface)
    65  	f.Fuzz(&a)
    66  	return reflect.ValueOf(a)
    67  }
    68  
    69  func TestEquals(t *testing.T) {
    70  	// In general this test will make sure people update things when they
    71  	// add a field.
    72  	//
    73  	// The "copy known fields" section of these function is to break if folks
    74  	// add new fields without fixing the Equals function and this test.
    75  	funcs := []interface{}{
    76  		func(x *Schema) bool {
    77  			if !x.Equals(x) {
    78  				return false
    79  			}
    80  			var y Schema
    81  			y.Types = x.Types
    82  			return x.Equals(&y) == reflect.DeepEqual(x, &y)
    83  		},
    84  		func(x TypeDef) bool {
    85  			if !x.Equals(&x) {
    86  				return false
    87  			}
    88  			var y TypeDef
    89  			y.Name = x.Name
    90  			y.Atom = x.Atom
    91  			return x.Equals(&y) == reflect.DeepEqual(x, y)
    92  		},
    93  		func(x TypeRef) bool {
    94  			if !x.Equals(&x) {
    95  				return false
    96  			}
    97  			var y TypeRef
    98  			y.NamedType = x.NamedType
    99  			y.Inlined = x.Inlined
   100  			return x.Equals(&y) == reflect.DeepEqual(x, y)
   101  		},
   102  		func(x Atom) bool {
   103  			if !x.Equals(&x) {
   104  				return false
   105  			}
   106  			var y Atom
   107  			y.Scalar = x.Scalar
   108  			y.List = x.List
   109  			y.Map = x.Map
   110  			return x.Equals(&y) == reflect.DeepEqual(x, y)
   111  		},
   112  		func(x *Map) bool {
   113  			if !x.Equals(x) {
   114  				return false
   115  			}
   116  			var y Map
   117  			y.ElementType = x.ElementType
   118  			y.ElementRelationship = x.ElementRelationship
   119  			y.Fields = x.Fields
   120  			y.Unions = x.Unions
   121  			return x.Equals(&y) == reflect.DeepEqual(x, &y)
   122  		},
   123  		func(x Union) bool {
   124  			if !x.Equals(&x) {
   125  				return false
   126  			}
   127  			var y Union
   128  			y.Discriminator = x.Discriminator
   129  			y.DeduceInvalidDiscriminator = x.DeduceInvalidDiscriminator
   130  			y.Fields = x.Fields
   131  			return x.Equals(&y) == reflect.DeepEqual(x, y)
   132  		},
   133  		func(x UnionField) bool {
   134  			if !x.Equals(&x) {
   135  				return false
   136  			}
   137  			var y UnionField
   138  			y.DiscriminatorValue = x.DiscriminatorValue
   139  			y.FieldName = x.FieldName
   140  			return x.Equals(&y) == reflect.DeepEqual(x, y)
   141  		},
   142  		func(x StructField) bool {
   143  			if !x.Equals(&x) {
   144  				return false
   145  			}
   146  			var y StructField
   147  			y.Name = x.Name
   148  			y.Type = x.Type
   149  			y.Default = x.Default
   150  			return x.Equals(&y) == reflect.DeepEqual(x, y)
   151  		},
   152  		func(x List) bool {
   153  			if !x.Equals(&x) {
   154  				return false
   155  			}
   156  			var y List
   157  			y.ElementType = x.ElementType
   158  			y.ElementRelationship = x.ElementRelationship
   159  			y.Keys = x.Keys
   160  			return x.Equals(&y) == reflect.DeepEqual(x, y)
   161  		},
   162  	}
   163  	for i, f := range funcs {
   164  		if err := quick.Check(f, nil); err != nil {
   165  			t.Errorf("%v: %v", i, err)
   166  		}
   167  	}
   168  }
   169  

View as plain text