common_test.go 798 B

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