Skip to content

Commit

Permalink
Add a function for expecting a panic.
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidgecka committed Jan 23, 2018
1 parent be1f6d7 commit 4959b2e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ matrix:
- go: 1.5
- go: 1.6
- go: 1.7
- go: 1.8
- go: 1.9
env: FMT_AND_VET=1
- go: tip

Expand All @@ -34,4 +36,4 @@ script:
- test "$FMT_AND_VET" != 1 || go test -v -race .

after_script:
- test "$FMT_AND_VET" != 1 || $HOME/gopath/bin/goveralls -coverprofile=/tmp/coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
- test "$FMT_AND_VET" != 1 || $HOME/gopath/bin/goveralls -coverprofile=/tmp/coverage.out -service=travis-ci -repotoken=$COVERALLS_TOKEN
17 changes: 17 additions & 0 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,20 @@ func (t *T) ExpectErrorMessagef(err error, msg string, spec string, args ...inte
"Error message=%s\nExpected string=%s", prefix, err.Error(), msg)
}
}

// Expects the function passed in to panic. This will call f() and expect
// that an error matching err will be raised as a panic.
func (t *T) ExpectPanic(f func(), err interface{}, desc ...string) {
prefix := ""
if len(desc) > 0 {
prefix = strings.Join(desc, " ") + ": "
}
defer func() {
i := recover()
if i == nil {
t.Fatalf("%sFunction call did not panic as expected.", prefix)
}
t.Equal(i, err, "Raised value is not correct")
}()
f()
}
14 changes: 14 additions & 0 deletions expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,17 @@ func TestT_ExpectErrorMessagef(t *testing.T) {
T.ExpectErrorMessage(fmt.Errorf("XXX"), "XXX")
})
}

func TestT_ExpectErrorPanic(t *testing.T) {
t.Parallel()
m, T := testSetup()

m.CheckPass(t, func() {
T.ExpectPanic(func() {
panic("EXPECTED")
}, "EXPECTED", "prefix: ")
})
m.CheckFail(t, func() {
T.ExpectPanic(func() {}, "UNEXPECTED")
})
}
5 changes: 3 additions & 2 deletions timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ func TestT_TryUntilYield(t *testing.T) {
l := sync.Mutex{}
unlocked := false
go func() {
time.Sleep(time.Second / 200)
time.Sleep(time.Second / 100)
l.Lock()
unlocked = true
l.Unlock()
}()
getUnlocked := func() bool {
l.Lock()
defer l.Unlock()
time.Sleep(time.Millisecond)
return unlocked
}
m.CheckPass(t, func() {
T.TryUntil(getUnlocked, time.Second/50)
T.TryUntil(getUnlocked, time.Second)
})
}

0 comments on commit 4959b2e

Please sign in to comment.