1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2023 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package testutil_test
import (
. "github.com/snapcore/snapd/testutil"
. "gopkg.in/check.v1"
)
type interfaceNilCheckerSuite struct{}
var _ = Suite(&interfaceNilCheckerSuite{})
type someError struct{}
func (*someError) Error() string { return "" }
func (*interfaceNilCheckerSuite) TestIsInterfaceNilMatchesIntendedNilComparison(c *C) {
testInfo(c, IsInterfaceNil, "IsInterfaceNil", []string{"value"})
err1 := func() error {
return nil
}()
c.Assert(err1 == nil, Equals, true)
c.Assert(err1, IsInterfaceNil)
err2 := func() error {
var err error
return err
}()
c.Assert(err2 == nil, Equals, true)
c.Assert(err2, IsInterfaceNil)
// assigning nil to a typed variable and then returning it fails the '== nil' check
err3 := typedErrWithNilValue()
c.Assert(err3 == nil, Equals, false)
res, errMsg := IsInterfaceNil.Check([]any{err3}, []string{"value"})
c.Assert(res, Equals, false)
c.Assert(errMsg, Equals, "expected <nil> but got *testutil_test.someError type")
}
// returns an error with a *someError type but nil value
func typedErrWithNilValue() error {
err := &someError{}
// prevent inefassign from complaining
_ = err
err = nil
return err
}
|