errors_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package fserr
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/suite"
  5. "net/http"
  6. "testing"
  7. )
  8. type TestErrorsSuite struct {
  9. suite.Suite
  10. originFundamental *fundamental
  11. originStack *withStack
  12. originMessage *withMessage
  13. originCode *withCode
  14. }
  15. func (s *TestErrorsSuite) SetupTest() {
  16. s.originFundamental = &fundamental{
  17. msg: "origin fundamental",
  18. stack: callers(),
  19. }
  20. s.originStack = &withStack{
  21. error: s.originFundamental,
  22. stack: callers(),
  23. }
  24. s.originMessage = &withMessage{
  25. cause: s.originStack,
  26. msg: "origin message",
  27. }
  28. s.originCode = &withCode{
  29. cause: s.originMessage,
  30. Msg: "with code",
  31. HttpCode: http.StatusOK,
  32. BusinessCode: ErrBasic,
  33. }
  34. }
  35. func (s *TestErrorsSuite) TestFundamental() {
  36. s.Equal("origin fundamental", s.originFundamental.msg)
  37. s.Equal("origin fundamental", s.originFundamental.Error())
  38. s.Equal("origin fundamental",
  39. fmt.Sprintf("%s", s.originFundamental))
  40. s.Equal(`"origin fundamental"`,
  41. fmt.Sprintf("%q", s.originFundamental))
  42. }
  43. func (s *TestErrorsSuite) TestStack() {
  44. s.Equal(s.originFundamental, s.originStack.Cause())
  45. s.Equal(s.originFundamental, s.originStack.Unwrap())
  46. s.Equal("origin fundamental", s.originStack.Error())
  47. s.Equal("origin fundamental", fmt.Sprintf("%s", s.originStack))
  48. s.Equal(`"origin fundamental"`, fmt.Sprintf("%q", s.originStack))
  49. }
  50. func (s *TestErrorsSuite) TestMessage() {
  51. s.Equal(s.originStack, s.originMessage.Cause())
  52. s.Equal(s.originStack, s.originMessage.Unwrap())
  53. s.Equal("origin message: origin fundamental", s.originMessage.Error())
  54. s.Equal("origin message: origin fundamental",
  55. fmt.Sprintf("%s", s.originMessage))
  56. s.Equal(`"origin message: origin fundamental"`,
  57. fmt.Sprintf("%q", s.originMessage))
  58. }
  59. func (s *TestErrorsSuite) TestCode() {
  60. s.Equal(s.originMessage, s.originCode.Cause())
  61. s.Equal(s.originMessage, s.originCode.Unwrap())
  62. s.Equal("with code", s.originCode.Error())
  63. s.Equal("with code", fmt.Sprintf("%s", s.originCode))
  64. s.Equal(`"with code"`, fmt.Sprintf("%q", s.originCode))
  65. }
  66. func TestErrors(t *testing.T) {
  67. suite.Run(t, &TestErrorsSuite{})
  68. }