Merge "Differentiate between empty and nil input" into udc-dev am: 267a137415
Original change: https://googleplex-android-review.googlesource.com/c/platform/build/soong/+/23374038 Change-Id: If4189ee29bf9a44eb86e594a2b3a2d6f3d27cec3 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -26,7 +26,11 @@ import (
|
||||
|
||||
// CopyOf returns a new slice that has the same contents as s.
|
||||
func CopyOf(s []string) []string {
|
||||
return append([]string(nil), s...)
|
||||
// If the input is nil, return nil and not an empty list
|
||||
if s == nil {
|
||||
return s
|
||||
}
|
||||
return append([]string{}, s...)
|
||||
}
|
||||
|
||||
// Concat returns a new slice concatenated from the two input slices. It does not change the input
|
||||
|
@@ -381,6 +381,14 @@ func TestRemoveFromList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyOfEmptyAndNil(t *testing.T) {
|
||||
emptyList := []string{}
|
||||
copyOfEmptyList := CopyOf(emptyList)
|
||||
AssertBoolEquals(t, "Copy of an empty list should be an empty list and not nil", true, copyOfEmptyList != nil)
|
||||
copyOfNilList := CopyOf(nil)
|
||||
AssertBoolEquals(t, "Copy of a nil list should be a nil list and not an empty list", true, copyOfNilList == nil)
|
||||
}
|
||||
|
||||
func ExampleCopyOf() {
|
||||
a := []string{"1", "2", "3"}
|
||||
b := CopyOf(a)
|
||||
|
Reference in New Issue
Block a user