Skip to content

Commit

Permalink
check if modifying copy will not change original value
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas-sidlauskas committed Jul 2, 2024
1 parent 3a4a1f7 commit 1d7803a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions common/types/configStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func TestDynamicConfigEntryCopy(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
valCopy := tc.input.Copy()
assert.Equal(t, tc.input, valCopy)
// check if modifying the copy does not modify the original
if valCopy != nil && valCopy.Values != nil {
valCopy.Values[0].Value.Data = []byte("modified")
assert.NotEqual(t, tc.input, valCopy)
}
assert.Equal(t, tc.input, tc.input.Copy())
})
}
Expand Down Expand Up @@ -100,7 +107,13 @@ func TestDynamicConfigFilterCopy(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.input, tc.input.Copy())
valCopy := tc.input.Copy()
assert.Equal(t, tc.input, valCopy)
// check if modifying the copy does not modify the original
if valCopy != nil {
valCopy.Name = "modified"
assert.NotEqual(t, tc.input, valCopy)
}
})
}
}
Expand Down Expand Up @@ -133,7 +146,13 @@ func TestDynamicConfigValueCopy(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.input, tc.input.Copy())
// check if modifying the copy does not modify the original
valCopy := tc.input.Copy()
assert.Equal(t, tc.input, valCopy)
if valCopy != nil && valCopy.Value != nil {
valCopy.Value.Data = []byte("modified")
assert.NotEqual(t, tc.input, valCopy)
}
})
}
}

0 comments on commit 1d7803a

Please sign in to comment.