Skip to content

Commit 05018b2

Browse files
committed
Added method to compare with and without ordering
1 parent 8927804 commit 05018b2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

properties.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,15 @@ func (m *Map) Clone() *Map {
430430
}
431431

432432
// Equals returns true if the current Map contains the same key/value pairs of
433-
// the Map passed as argument with the same order of insertion.
433+
// the Map passed as argument, the order of insertion does not matter.
434434
func (m *Map) Equals(other *Map) bool {
435-
return reflect.DeepEqual(m, other)
435+
return reflect.DeepEqual(m.kv, other.kv)
436+
}
437+
438+
// EqualsWithOrder returns true if the current Map contains the same key/value pairs of
439+
// the Map passed as argument with the same order of insertion.
440+
func (m *Map) EqualsWithOrder(other *Map) bool {
441+
return reflect.DeepEqual(m.o, other.o) && reflect.DeepEqual(m.kv, other.kv)
436442
}
437443

438444
// MergeMapsOfProperties merge the map-of-Maps (obtained from the method FirstLevelOf()) into the

properties_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,33 @@ func TestKeysMethod(t *testing.T) {
185185
sort.Strings(v)
186186
require.Equal(t, "[anothevalue othervalue value value]", fmt.Sprintf("%s", v))
187187
}
188+
189+
func TestEquals(t *testing.T) {
190+
x := NewMap()
191+
x.Set("k1", "value")
192+
x.Set("k2", "othervalue")
193+
x.Set("k3.k4", "anothevalue")
194+
x.Set("k5", "value")
195+
196+
y := NewMap()
197+
y.Set("k1", "value")
198+
y.Set("k2", "othervalue")
199+
y.Set("k3.k4", "anothevalue")
200+
y.Set("k5", "value")
201+
202+
z := NewMap()
203+
z.Set("k2", "othervalue")
204+
z.Set("k1", "value")
205+
z.Set("k3.k4", "anothevalue")
206+
z.Set("k5", "value")
207+
208+
require.True(t, x.Equals(y))
209+
require.True(t, y.Equals(x))
210+
require.True(t, x.Equals(z))
211+
require.True(t, z.Equals(x))
212+
213+
require.True(t, x.EqualsWithOrder(y))
214+
require.True(t, y.EqualsWithOrder(x))
215+
require.False(t, x.EqualsWithOrder(z))
216+
require.False(t, z.EqualsWithOrder(x))
217+
}

0 commit comments

Comments
 (0)