Skip to content

Commit

Permalink
Merge pull request #10 from JosiahWitt/add-is-nil
Browse files Browse the repository at this point in the history
Add IsNil() chained function
  • Loading branch information
JosiahWitt authored Jan 14, 2021
2 parents 6d42a82 + be8a70f commit a336489
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
33 changes: 22 additions & 11 deletions ensurepkg/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ func (c Chain) IsFalse() {
}
}

// IsNil ensures the actual value is nil or a nil pointer.
func (c Chain) IsNil() {
c.t.Helper()

if !isNil(c.actual) {
c.t.Errorf("Got %+v, expected nil", c.actual)
}
}

// Equals ensures the actual value equals the expected value.
// Equals uses deep.Equal to print easy to read diffs.
func (c Chain) Equals(expected interface{}) {
c.t.Helper()

// If we expect nil, return early if actual is nil or if it is a nil pointer
if expected == nil {
if c.actual == nil {
return
}

actualReflection := reflect.ValueOf(c.actual)
isActualNilPointer := actualReflection.Kind() == reflect.Ptr && actualReflection.IsNil()
if isActualNilPointer {
return
}
if expected == nil && isNil(c.actual) {
return
}

deep.CompareUnexportedFields = true
Expand Down Expand Up @@ -89,7 +90,7 @@ func (c Chain) IsError(expected error) {
if !errors.Is(actual, expected) {
actualOutput := buildActualErrorOutput(actual)
expectedOutput := buildExpectedErrorOutput(expected)
c.t.Errorf("\nGot: %s\nExpected: %s", actualOutput, expectedOutput)
c.t.Errorf("\nActual error is not the expected error:\n\tActual: %s\n\tExpected: %s", actualOutput, expectedOutput)
}
}

Expand Down Expand Up @@ -127,3 +128,13 @@ func buildExpectedErrorOutput(expected error) string {
expectedErk.Params(),
)
}

func isNil(value interface{}) bool {
if value == nil {
return true
}

reflection := reflect.ValueOf(value)
isNilPointer := reflection.Kind() == reflect.Ptr && reflection.IsNil()
return isNilPointer
}
39 changes: 37 additions & 2 deletions ensurepkg/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,41 @@ func TestChainIsFalse(t *testing.T) {
})
}

func TestChainIsNil(t *testing.T) {
t.Run("when nil", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockT := mock_ensurepkg.NewMockT(ctrl)
mockT.EXPECT().Helper()

ensure := ensure.New(mockT)
ensure(nil).IsNil()
})

t.Run("when nil pointer", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockT := mock_ensurepkg.NewMockT(ctrl)
mockT.EXPECT().Helper()

var nilPtr *string

ensure := ensure.New(mockT)
ensure(nilPtr).IsNil()
})

t.Run("when not nil", func(t *testing.T) {
ctrl := gomock.NewController(t)
mockT := mock_ensurepkg.NewMockT(ctrl)

const val = "not nil"
mockT.EXPECT().Errorf("Got %+v, expected nil", val).After(
mockT.EXPECT().Helper(),
)

ensure := ensure.New(mockT)
ensure(val).IsNil()
})
}

func TestChainEquals(t *testing.T) {
const errorMessageFormat = "\n%s\n\nActual: %+v\nExpected: %+v"

Expand Down Expand Up @@ -275,7 +310,7 @@ func TestChainEquals(t *testing.T) {
}

func TestChainIsError(t *testing.T) {
const errorFormat = "\nGot: %s\nExpected: %s"
const errorFormat = "\nActual error is not the expected error:\n\tActual: %s\n\tExpected: %s"

t.Run("when equal error by reference", func(t *testing.T) {
ctrl := gomock.NewController(t)
Expand Down Expand Up @@ -474,7 +509,7 @@ func TestChainIsNotError(t *testing.T) {
mockT := mock_ensurepkg.NewMockT(ctrl)

err := errors.New("my error")
mockT.EXPECT().Errorf("\nGot: %s\nExpected: %s", err.Error(), "<nil>").After(
mockT.EXPECT().Errorf("\nActual error is not the expected error:\n\tActual: %s\n\tExpected: %s", err.Error(), "<nil>").After(
mockT.EXPECT().Helper().Times(2),
)

Expand Down

0 comments on commit a336489

Please sign in to comment.