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 more power to Assert #48

Open
wants to merge 7 commits 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
68 changes: 60 additions & 8 deletions assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Assertion struct {
src interface{}
fail func(interface{})
Not *NotAssertion
}

func objectsAreEqual(a, b interface{}) bool {
Expand All @@ -34,26 +35,77 @@ func formatMessages(messages ...string) string {
return ""
}

func (a *Assertion) Eql(dst interface{}) {
a.Equal(dst)
func (a *Assertion) Eql(dst interface{}, messages ...string) {
a.Equal(dst, messages...)
}

func (a *Assertion) Equal(dst interface{}) {
func (a *Assertion) Equal(dst interface{}, messages ...string) {
if !objectsAreEqual(a.src, dst) {
a.fail(fmt.Sprintf("%v %s %v", a.src, "does not equal", dst))
a.fail(fmt.Sprintf("%v does not equal %v%s", a.src, dst, formatMessages(messages...)))
}
}

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

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

func (a *Assertion) IsOK(messages ...string) {
if objectsAreEqual(a.src, nil) {
a.fail(fmt.Sprintf("Expected %v to be OK%s", a.src, formatMessages(messages...)))
}
}

func (a *Assertion) HasSameType(dst interface{}, messages ...string) {
if reflect.TypeOf(a.src) != reflect.TypeOf(dst) {
a.fail(fmt.Sprintf("%v is not the same type as %v%s", a.src, dst, formatMessages(messages...)))
}

}

type NotAssertion struct {
src interface{}
fail func(interface{})
Not *Assertion
}

func (n *NotAssertion) Eql(dst interface{}, messages ...string) {
n.Equal(dst, messages...)
}

func (n *NotAssertion) Equal(dst interface{}, messages ...string) {
if objectsAreEqual(n.src, dst) {
n.fail(fmt.Sprintf("Expected %v to not equal %v%s", n.src, dst, formatMessages(messages...)))
}
}

func (n *NotAssertion) IsTrue(messages ...string) {
if objectsAreEqual(n.src, true) {
n.fail(fmt.Sprintf("Expected %v to not be truthy%s", n.src, formatMessages(messages...)))
}
}

func (n *NotAssertion) IsFalse(messages ...string) {
if objectsAreEqual(n.src, false) {
n.fail(fmt.Sprintf("Expected %v to not be falsey%s", n.src, formatMessages(messages...)))
}
}

func (n *NotAssertion) IsOK(messages ...string) {
if !objectsAreEqual(n.src, nil) {
n.fail(fmt.Sprintf("Expected %v to not be OK%s", n.src, formatMessages(messages...)))
}
}

func (n *NotAssertion) HasSameType(dst interface{}, messages ...string) {
if reflect.TypeOf(n.src) == reflect.TypeOf(dst) {
n.fail(fmt.Sprintf("Expected %v to not be the same type as %v%s", n.src, dst, formatMessages(messages...)))
}
}
4 changes: 2 additions & 2 deletions assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func TestIsFalseWithMessage(t *testing.T) {
a := Assertion{src: true, fail: verifier.FailFunc}
a.IsFalse("false is not true")
verifier.Verify(t)
verifier.VerifyMessage(t, "true expected true to be falsey, false is not true")
verifier.VerifyMessage(t, "Expected true to be falsey, false is not true")
}

func TestIsTrueWithMessage(t *testing.T) {
verifier := AssertionVerifier{ShouldPass: false}
a := Assertion{src: false, fail: verifier.FailFunc}
a.IsTrue("true is not false")
verifier.Verify(t)
verifier.VerifyMessage(t, "false expected false to be truthy, true is not false")
verifier.VerifyMessage(t, "Expected false to be truthy, true is not false")
}
164 changes: 164 additions & 0 deletions extended_assertions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package goblin

import (
"testing"
)

func TestCatching(t *testing.T) {
g := Goblin(t)

g.Describe("Catching", func() {
g.It("No panic", func() {
})

g.It("Expected panic", func() {
g.ExpectPanic()
panic("Expect")
})

g.It("Fail on no expected panic", func() {
g.ExpectFail()
g.ExpectPanic()
})

g.It("Fail on unexpected panic", func() {
g.ExpectFail()
panic("Suddenly, panicing!")
})
})
}

func TestCatchingAsync(t *testing.T) {
g := Goblin(t)

g.Describe("Catching", func() {
g.It("No panic", func(d Done) {
d()
})

g.It("Expected panic", func(d Done) {
g.ExpectPanic()
panic("Expect")
})

g.It("Fail on no expected panic", func(d Done) {
g.ExpectFail()
g.ExpectPanic()
d()
})

g.It("Fail on unexpected panic", func(d Done) {
g.ExpectFail()
panic("Suddenly, panicing!")
})
})
}

func TestWorking(t *testing.T) {
g := Goblin(t)

g.Describe("Normal", func() {
g.It("Eql", func() {
g.Assert("Hello").Eql("Hello")
})

g.It("IsTrue", func() {
g.Assert(true).IsTrue()
})

g.It("IsFalse", func() {
g.Assert(false).IsFalse()
})

g.It("IsOK", func() {
g.Assert(g).IsOK()
})

g.It("IsType", func() {
g.Assert("").HasSameType("")
})
})

g.Describe("Not", func() {
g.It("Eql", func() {
g.Assert("Hello").Not.Eql("Lol")
g.Assert("Hello").Not.Eql("hello")
})

g.It("IsTrue", func() {
g.Assert(false).Not.IsTrue()
g.Assert("").Not.IsTrue()
})

g.It("IsFalse", func() {
g.Assert(true).Not.IsFalse()
g.Assert("a").Not.IsFalse()
})

g.It("IsOK", func() {
g.Assert(nil).Not.IsOK()
})

g.It("IsType", func() {
g.Assert(1).Not.HasSameType("")
})
})
}

func TestFailing(t *testing.T) {
g := Goblin(t)

g.Describe("Normal", func() {
g.It("Eql", func() {
g.ExpectFail()
g.Assert("Hello").Eql("hello")
})

g.It("IsTrue", func() {
g.ExpectFail()
g.Assert(false).IsTrue()
})

g.It("IsFalse", func() {
g.ExpectFail()
g.Assert(true).IsFalse()
})

g.It("IsOK", func() {
g.ExpectFail()
g.Assert(nil).IsOK()
})

g.It("IsType", func() {
g.ExpectFail()
g.Assert(1).HasSameType("")
})
})

g.Describe("Not", func() {
g.It("Eql", func() {
g.ExpectFail()
g.Assert("Hello").Not.Eql("Hello")
})

g.It("IsTrue", func() {
g.ExpectFail()
g.Assert(true).Not.IsTrue()
})

g.It("IsFalse", func() {
g.ExpectFail()
g.Assert(false).Not.IsFalse()
})

g.It("IsOK", func() {
g.ExpectFail()
g.Assert(g).Not.IsOK()
})

g.It("IsType", func() {
g.ExpectFail()
g.Assert("").Not.HasSameType("")
})
})
}
Loading