Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added IsNil and IsNotNil assertions #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ func (a *Assertion) IsFalse(messages ...string) {
a.fail(message)
}
}

func (a *Assertion) IsNil(messages ...string) {
if !objectsAreEqual(a.src, nil) {
message := fmt.Sprintf("%v %s%s", a.src, "expected to be nil", formatMessages(messages...))
a.fail(message)
}
}

func (a *Assertion) IsNotNil(messages ...string) {
if objectsAreEqual(a.src, nil) {
message := fmt.Sprintf("%s%s", "expected not to be nil", formatMessages(messages...))
a.fail(message)
}
}
40 changes: 40 additions & 0 deletions assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,43 @@ func TestIsTrueWithMessage(t *testing.T) {
verifier.Verify(t)
verifier.VerifyMessage(t, "false expected false to be truthy, true is not false")
}

func TestIsNil(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: true}
a := Assertion{src: nil, fail: verifier.FailFunc}
a.IsNil()
verifier.Verify(t)

verifier = AssertionVerifier{ShouldPass: false}
a = Assertion{src: 102, fail: verifier.FailFunc}
a.IsNil()
verifier.Verify(t)
}

func TestIsNilWithMessage(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: false}
a := Assertion{src: 300, fail: verifier.FailFunc}
a.IsNil("foobar")
verifier.Verify(t)
verifier.VerifyMessage(t, "300 expected to be nil, foobar")
}

func TestIsNotNil(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: true}
a := Assertion{src: 103, fail: verifier.FailFunc}
a.IsNotNil()
verifier.Verify(t)

verifier = AssertionVerifier{ShouldPass: false}
a = Assertion{src: nil, fail: verifier.FailFunc}
a.IsNotNil()
verifier.Verify(t)
}

func TestIsNotNilWithMessage(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: false}
a := Assertion{src: nil, fail: verifier.FailFunc}
a.IsNotNil("foobar")
verifier.Verify(t)
verifier.VerifyMessage(t, "expected not to be nil, foobar")
}
31 changes: 31 additions & 0 deletions goblin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,34 @@ func TestTimeout(t *testing.T) {
t.Fatal("Failed")
}
}

func TestNils(t *testing.T) {
fakeTest := testing.T{}
g := Goblin(&fakeTest)

g.Describe("Test IsNil and IsNotNil assertions", func() {
g.It("IsNil should work fine for nil value", func() {
g.Assert(nil).IsNil()
})
g.It("IsNotNil should work fine for not-nil value", func() {
g.Assert(102).IsNotNil()
})
})

if fakeTest.Failed() {
t.Fatal("Failed")
}

g.Describe("Test IsNil and IsNotNil assertions with wrong values", func() {
g.It("IsNil should fail for not-nil value", func() {
g.Assert(102).IsNil()
})
g.It("IsNotNil should fail for nil value", func() {
g.Assert(nil).IsNotNil()
})
})

if !fakeTest.Failed() {
t.Fatal("Failed")
}
}