1// Copyright 2017, The Go Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style 3// license that can be found in the LICENSE file. 4 5package teststructs 6 7import ( 8 "time" 9 10 pb "github.com/google/go-cmp/cmp/internal/testprotos" 11) 12 13// This is an sanitized example of equality from a real use-case. 14// The original equality function was as follows: 15/* 16func equalCartel(x, y Cartel) bool { 17 if !(equalHeadquarter(x.Headquarter, y.Headquarter) && 18 x.Source() == y.Source() && 19 x.CreationDate().Equal(y.CreationDate()) && 20 x.Boss() == y.Boss() && 21 x.LastCrimeDate().Equal(y.LastCrimeDate())) { 22 return false 23 } 24 if len(x.Poisons()) != len(y.Poisons()) { 25 return false 26 } 27 for i := range x.Poisons() { 28 if !equalPoison(*x.Poisons()[i], *y.Poisons()[i]) { 29 return false 30 } 31 } 32 return true 33} 34func equalHeadquarter(x, y Headquarter) bool { 35 xr, yr := x.Restrictions(), y.Restrictions() 36 return x.ID() == y.ID() && 37 x.Location() == y.Location() && 38 reflect.DeepEqual(x.SubDivisions(), y.SubDivisions()) && 39 x.IncorporatedDate().Equal(y.IncorporatedDate()) && 40 pb.Equal(x.MetaData(), y.MetaData()) && 41 bytes.Equal(x.PrivateMessage(), y.PrivateMessage()) && 42 bytes.Equal(x.PublicMessage(), y.PublicMessage()) && 43 x.HorseBack() == y.HorseBack() && 44 x.Rattle() == y.Rattle() && 45 x.Convulsion() == y.Convulsion() && 46 x.Expansion() == y.Expansion() && 47 x.Status() == y.Status() && 48 pb.Equal(&xr, &yr) && 49 x.CreationTime().Equal(y.CreationTime()) 50} 51func equalPoison(x, y Poison) bool { 52 return x.PoisonType() == y.PoisonType() && 53 x.Expiration().Equal(y.Expiration()) && 54 x.Manufacturer() == y.Manufacturer() && 55 x.Potency() == y.Potency() 56} 57*/ 58 59type Cartel struct { 60 Headquarter 61 source string 62 creationDate time.Time 63 boss string 64 lastCrimeDate time.Time 65 poisons []*Poison 66} 67 68func (p Cartel) Source() string { return p.source } 69func (p Cartel) CreationDate() time.Time { return p.creationDate } 70func (p Cartel) Boss() string { return p.boss } 71func (p Cartel) LastCrimeDate() time.Time { return p.lastCrimeDate } 72func (p Cartel) Poisons() []*Poison { return p.poisons } 73 74func (p *Cartel) SetSource(x string) { p.source = x } 75func (p *Cartel) SetCreationDate(x time.Time) { p.creationDate = x } 76func (p *Cartel) SetBoss(x string) { p.boss = x } 77func (p *Cartel) SetLastCrimeDate(x time.Time) { p.lastCrimeDate = x } 78func (p *Cartel) SetPoisons(x []*Poison) { p.poisons = x } 79 80type Headquarter struct { 81 id uint64 82 location string 83 subDivisions []string 84 incorporatedDate time.Time 85 metaData *pb.MetaData 86 privateMessage []byte 87 publicMessage []byte 88 horseBack string 89 rattle string 90 convulsion bool 91 expansion uint64 92 status pb.HoneyStatus 93 restrictions pb.Restrictions 94 creationTime time.Time 95} 96 97func (hq Headquarter) ID() uint64 { return hq.id } 98func (hq Headquarter) Location() string { return hq.location } 99func (hq Headquarter) SubDivisions() []string { return hq.subDivisions } 100func (hq Headquarter) IncorporatedDate() time.Time { return hq.incorporatedDate } 101func (hq Headquarter) MetaData() *pb.MetaData { return hq.metaData } 102func (hq Headquarter) PrivateMessage() []byte { return hq.privateMessage } 103func (hq Headquarter) PublicMessage() []byte { return hq.publicMessage } 104func (hq Headquarter) HorseBack() string { return hq.horseBack } 105func (hq Headquarter) Rattle() string { return hq.rattle } 106func (hq Headquarter) Convulsion() bool { return hq.convulsion } 107func (hq Headquarter) Expansion() uint64 { return hq.expansion } 108func (hq Headquarter) Status() pb.HoneyStatus { return hq.status } 109func (hq Headquarter) Restrictions() pb.Restrictions { return hq.restrictions } 110func (hq Headquarter) CreationTime() time.Time { return hq.creationTime } 111 112func (hq *Headquarter) SetID(x uint64) { hq.id = x } 113func (hq *Headquarter) SetLocation(x string) { hq.location = x } 114func (hq *Headquarter) SetSubDivisions(x []string) { hq.subDivisions = x } 115func (hq *Headquarter) SetIncorporatedDate(x time.Time) { hq.incorporatedDate = x } 116func (hq *Headquarter) SetMetaData(x *pb.MetaData) { hq.metaData = x } 117func (hq *Headquarter) SetPrivateMessage(x []byte) { hq.privateMessage = x } 118func (hq *Headquarter) SetPublicMessage(x []byte) { hq.publicMessage = x } 119func (hq *Headquarter) SetHorseBack(x string) { hq.horseBack = x } 120func (hq *Headquarter) SetRattle(x string) { hq.rattle = x } 121func (hq *Headquarter) SetConvulsion(x bool) { hq.convulsion = x } 122func (hq *Headquarter) SetExpansion(x uint64) { hq.expansion = x } 123func (hq *Headquarter) SetStatus(x pb.HoneyStatus) { hq.status = x } 124func (hq *Headquarter) SetRestrictions(x pb.Restrictions) { hq.restrictions = x } 125func (hq *Headquarter) SetCreationTime(x time.Time) { hq.creationTime = x } 126 127type Poison struct { 128 poisonType pb.PoisonType 129 expiration time.Time 130 manufacturer string 131 potency int 132} 133 134func (p Poison) PoisonType() pb.PoisonType { return p.poisonType } 135func (p Poison) Expiration() time.Time { return p.expiration } 136func (p Poison) Manufacturer() string { return p.manufacturer } 137func (p Poison) Potency() int { return p.potency } 138 139func (p *Poison) SetPoisonType(x pb.PoisonType) { p.poisonType = x } 140func (p *Poison) SetExpiration(x time.Time) { p.expiration = x } 141func (p *Poison) SetManufacturer(x string) { p.manufacturer = x } 142func (p *Poison) SetPotency(x int) { p.potency = x } 143