error_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package util
  2. import "testing"
  3. var okErrData string = `{"errcode": 0}`
  4. var errData string = `{"errcode": 43101, "errmsg": "user refuse to accept the msg"}`
  5. var expectError string = "Send Error , errcode=43101 , errmsg=user refuse to accept the msg"
  6. func TestDecodeWithCommonErrorNoError(t *testing.T) {
  7. err := DecodeWithCommonError([]byte(okErrData), "Send")
  8. if err != nil {
  9. t.Error("DecodeWithCommonError should not return error")
  10. return
  11. }
  12. }
  13. func TestDecodeWithCommonError(t *testing.T) {
  14. err := DecodeWithCommonError([]byte(errData), "Send")
  15. if err == nil {
  16. t.Error("DecodeWithCommonError should return error")
  17. return
  18. }
  19. cErr, ok := err.(*CommonError)
  20. if !ok {
  21. t.Errorf("DecodeWithCommonError should return *CommonError but %T", err)
  22. return
  23. }
  24. if !(cErr.ErrCode == 43101 && cErr.ErrMsg == "user refuse to accept the msg" && cErr.Error() == expectError) {
  25. t.Error("DecodeWithCommonError return bad *CommonError")
  26. return
  27. }
  28. }
  29. func TestDecodeWithError(t *testing.T) {
  30. type DE struct {
  31. CommonError
  32. }
  33. var obj DE
  34. err := DecodeWithError([]byte(errData), &obj, "Send")
  35. if err == nil {
  36. t.Error("DecodeWithError should return error")
  37. return
  38. }
  39. cErr, ok := err.(*CommonError)
  40. if !ok {
  41. t.Errorf("DecodeWithError should return *CommonError but %T", err)
  42. return
  43. }
  44. if !(cErr.ErrCode == 43101 && cErr.ErrMsg == "user refuse to accept the msg" && cErr.Error() == expectError) {
  45. t.Error("DecodeWithError return bad *CommonError")
  46. return
  47. }
  48. }