// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may // not use this file except in compliance with the License. A copy of the // License is located at // // http://aws.amazon.com/apache2.0/ // // or in the "license" file accompanying this file. This file is distributed // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either // express or implied. See the License for the specific language governing // permissions and limitations under the License. package utils import ( "testing" "github.com/stretchr/testify/assert" ) type dummyStruct struct { // no contents } func TestZeroOrNil(t *testing.T) { type ZeroTest struct { testInt int TestStr string testNilJson dummyStruct } var strMap map[string]string testCases := []struct { param interface{} expected bool name string }{ {nil, true, "Nil is nil"}, {0, true, "0 is 0"}, {"", true, "\"\" is the string zerovalue"}, {ZeroTest{}, true, "ZeroTest zero-value should be zero"}, {ZeroTest{TestStr: "asdf"}, false, "ZeroTest with a field populated isn't zero"}, {ZeroTest{testNilJson: dummyStruct{}}, true, "nil is nil"}, {1, false, "1 is not 0"}, {[]uint16{1, 2, 3}, false, "[1,2,3] is not zero"}, {[]uint16{}, true, "[] is zero"}, {struct{ uncomparable []uint16 }{uncomparable: []uint16{1, 2, 3}}, false, "Uncomparable structs are never zero"}, {struct{ uncomparable []uint16 }{uncomparable: nil}, false, "Uncomparable structs are never zero"}, {strMap, true, "map[string]string is zero or nil"}, {make(map[string]string), true, "empty map[string]string is zero or nil"}, {map[string]string{"foo": "bar"}, false, "map[string]string{foo:bar} is not zero or nil"}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { assert.Equal(t, tc.expected, ZeroOrNil(tc.param), tc.name) }) } }