Skip to content

Commit 8aa2f9f

Browse files
author
Antoine Pelisse
committed
value.Equals should check types in both directions
Currently, value.Equals doesn't check that the types are different in both direction: it checks that if lhs is of type A, then rhs must also be of type A), but it doesn't check that if lhs is not of type A but rhs is, then they must be different. Make it more obvious in the test that the types must always match, and fix the test by checking in both direction.
1 parent 446b504 commit 8aa2f9f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

value/less_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,19 @@ func TestValueLess(t *testing.T) {
261261
if !Equals(a, b) {
262262
t.Errorf("oops, a != b: %#v, %#v", tt.a, tt.b)
263263
}
264+
if !Equals(b, a) {
265+
t.Errorf("oops, b != a: %#v, %#v", tt.b, tt.a)
266+
}
264267
if Less(a, b) {
265268
t.Errorf("oops, a < b: %#v, %#v", tt.a, tt.b)
266269
}
267270
} else {
271+
if Equals(a, b) {
272+
t.Errorf("oops, a == b: %#v, %#v", tt.a, tt.b)
273+
}
274+
if Equals(b, a) {
275+
t.Errorf("oops, b == a: %#v, %#v", tt.b, tt.a)
276+
}
268277
if !Less(a, b) {
269278
t.Errorf("oops, a >= b: %#v, %#v", tt.a, tt.b)
270279
}

value/value.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,48 @@ func Equals(lhs, rhs Value) bool {
152152
return lhs.AsInt() == rhs.AsInt()
153153
}
154154
return false
155+
} else if rhs.IsInt() {
156+
return false
155157
}
156158
if lhs.IsString() {
157159
if rhs.IsString() {
158160
return lhs.AsString() == rhs.AsString()
159161
}
160162
return false
163+
} else if rhs.IsString() {
164+
return false
161165
}
162166
if lhs.IsBool() {
163167
if rhs.IsBool() {
164168
return lhs.AsBool() == rhs.AsBool()
165169
}
166170
return false
171+
} else if rhs.IsBool() {
172+
return false
167173
}
168174
if lhs.IsList() {
169175
if rhs.IsList() {
170176
return ListEquals(lhs.AsList(), rhs.AsList())
171177
}
172178
return false
179+
} else if rhs.IsList() {
180+
return false
173181
}
174182
if lhs.IsMap() {
175183
if rhs.IsMap() {
176184
return lhs.AsMap().Equals(rhs.AsMap())
177185
}
178186
return false
187+
} else if rhs.IsMap() {
188+
return false
179189
}
180190
if lhs.IsNull() {
181191
if rhs.IsNull() {
182192
return true
183193
}
184194
return false
195+
} else if rhs.IsNull() {
196+
return false
185197
}
186198
// No field is set, on either objects.
187199
return true

0 commit comments

Comments
 (0)