-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmust_test.go
42 lines (36 loc) · 972 Bytes
/
must_test.go
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
package must_test
import (
"errors"
"testing"
"github.com/mdw-go/must/must"
)
func TestNada(t *testing.T) { defer catch(t); must.Void(nada()) }
func TestValue(t *testing.T) { defer catch(t); _ = must.Value(value()) }
func TestValues(t *testing.T) { defer catch(t); _, _ = must.Values(values()) }
func catch(t *testing.T) {
if r := recover(); r.(error).Error() != err.Error() {
t.Error("should have panicked on err")
}
}
var err = errors.New("err")
func nada() error { return err }
func value() (int, error) { return 0, err }
func values() (int, int, error) { return 0, 0, err }
func TestDeferExecuted(t *testing.T) {
counter := 0
defer func() {
if counter == 0 {
t.Error("should have incremented counter")
}
}()
incrementer := func() error {
counter++
return nil
}
defer must.Defer(incrementer)()
}
func TestDeferPanics(t *testing.T) {
defer catch(t)
fails := func() error { return err }
defer must.Defer(fails)()
}