github.com/eagleql/xray-core@v1.4.4/common/common_test.go (about) 1 package common_test 2 3 import ( 4 "errors" 5 "testing" 6 7 . "github.com/eagleql/xray-core/common" 8 ) 9 10 func TestMust(t *testing.T) { 11 hasPanic := func(f func()) (ret bool) { 12 defer func() { 13 if r := recover(); r != nil { 14 ret = true 15 } 16 }() 17 f() 18 return false 19 } 20 21 testCases := []struct { 22 Input func() 23 Panic bool 24 }{ 25 { 26 Panic: true, 27 Input: func() { Must(func() error { return errors.New("test error") }()) }, 28 }, 29 { 30 Panic: true, 31 Input: func() { Must2(func() (int, error) { return 0, errors.New("test error") }()) }, 32 }, 33 { 34 Panic: false, 35 Input: func() { Must(func() error { return nil }()) }, 36 }, 37 } 38 39 for idx, test := range testCases { 40 if hasPanic(test.Input) != test.Panic { 41 t.Error("test case #", idx, " expect panic ", test.Panic, " but actually not") 42 } 43 } 44 }