github.com/openconfig/goyang@v1.4.5/pkg/yang/yangtype_test.go (about) 1 // Copyright 2021 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package yang 16 17 import ( 18 "testing" 19 ) 20 21 func TestYangTypeEqual(t *testing.T) { 22 23 tests := []struct { 24 name string 25 inLeft *YangType 26 inRight *YangType 27 wantEqual bool 28 }{{ 29 name: "both-nil", 30 inLeft: nil, 31 inRight: nil, 32 wantEqual: true, 33 }, { 34 name: "one-nil", 35 inLeft: &YangType{ 36 Kind: Ydecimal64, 37 FractionDigits: 5, 38 }, 39 inRight: nil, 40 wantEqual: false, 41 }, { 42 name: "name-unequal", 43 inLeft: &YangType{ 44 Name: "foo", 45 Kind: Ydecimal64, 46 FractionDigits: 5, 47 }, 48 inRight: &YangType{ 49 Name: "bar", 50 Kind: Ydecimal64, 51 FractionDigits: 5, 52 }, 53 wantEqual: true, 54 }, { 55 name: "fraction-digits-unequal", 56 inLeft: &YangType{ 57 Name: "foo", 58 Kind: Ydecimal64, 59 FractionDigits: 5, 60 }, 61 inRight: &YangType{ 62 Name: "foo", 63 Kind: Ydecimal64, 64 FractionDigits: 4, 65 }, 66 wantEqual: false, 67 }, { 68 name: "types-unequal", 69 inLeft: &YangType{ 70 Name: "foo", 71 Kind: Ydecimal64, 72 FractionDigits: 5, 73 }, 74 inRight: &YangType{ 75 Name: "foo", 76 Kind: Yint64, 77 }, 78 wantEqual: false, 79 }, { 80 name: "defaults-equal", 81 inLeft: &YangType{ 82 Name: "foo", 83 Kind: Ystring, 84 Default: "bar", 85 HasDefault: true, 86 }, 87 inRight: &YangType{ 88 Name: "foo", 89 Kind: Ystring, 90 Default: "bar", 91 HasDefault: true, 92 }, 93 wantEqual: true, 94 }, { 95 name: "defaults-unequal", 96 inLeft: &YangType{ 97 Name: "foo", 98 Kind: Ystring, 99 Default: "bar", 100 HasDefault: true, 101 }, 102 inRight: &YangType{ 103 Name: "foo", 104 Kind: Ystring, 105 Default: "baz", 106 HasDefault: true, 107 }, 108 wantEqual: false, 109 }, { 110 name: "has-default-unequal", 111 inLeft: &YangType{ 112 Name: "foo", 113 Kind: Ystring, 114 Default: "", 115 }, 116 inRight: &YangType{ 117 Name: "foo", 118 Kind: Ystring, 119 Default: "", 120 HasDefault: true, 121 }, 122 wantEqual: false, 123 }} 124 125 for _, tt := range tests { 126 t.Run(tt.name, func(t *testing.T) { 127 if gotEqual := tt.inLeft.Equal(tt.inRight); gotEqual != tt.wantEqual { 128 t.Errorf("gotEqual: %v, wantEqual: %v", gotEqual, tt.wantEqual) 129 } 130 // Must be symmetric 131 if reverseEqual := tt.inRight.Equal(tt.inLeft); reverseEqual != tt.wantEqual { 132 t.Errorf("got reverseEqual: %v, wantEqual: %v", reverseEqual, tt.wantEqual) 133 } 134 }) 135 } 136 }