canon.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package httpexpect
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. )
  8. func canonNumber(opChain *chain, in interface{}) (out float64, ok bool) {
  9. ok = true
  10. defer func() {
  11. if err := recover(); err != nil {
  12. opChain.fail(AssertionFailure{
  13. Type: AssertValid,
  14. Actual: &AssertionValue{in},
  15. Errors: []error{
  16. errors.New("expected: valid number"),
  17. fmt.Errorf("%s", err),
  18. },
  19. })
  20. ok = false
  21. }
  22. }()
  23. out = reflect.ValueOf(in).Convert(reflect.TypeOf(float64(0))).Float()
  24. return
  25. }
  26. func canonArray(opChain *chain, in interface{}) ([]interface{}, bool) {
  27. var out []interface{}
  28. data, ok := canonValue(opChain, in)
  29. if ok {
  30. out, ok = data.([]interface{})
  31. if !ok {
  32. opChain.fail(AssertionFailure{
  33. Type: AssertValid,
  34. Actual: &AssertionValue{in},
  35. Errors: []error{
  36. errors.New("expected: valid array"),
  37. },
  38. })
  39. }
  40. }
  41. return out, ok
  42. }
  43. func canonMap(opChain *chain, in interface{}) (map[string]interface{}, bool) {
  44. var out map[string]interface{}
  45. data, ok := canonValue(opChain, in)
  46. if ok {
  47. out, ok = data.(map[string]interface{})
  48. if !ok {
  49. opChain.fail(AssertionFailure{
  50. Type: AssertValid,
  51. Actual: &AssertionValue{in},
  52. Errors: []error{
  53. errors.New("expected: valid map"),
  54. },
  55. })
  56. }
  57. }
  58. return out, ok
  59. }
  60. func canonValue(opChain *chain, in interface{}) (interface{}, bool) {
  61. b, err := json.Marshal(in)
  62. if err != nil {
  63. opChain.fail(AssertionFailure{
  64. Type: AssertValid,
  65. Actual: &AssertionValue{in},
  66. Errors: []error{
  67. errors.New("expected: marshalable value"),
  68. err,
  69. },
  70. })
  71. return nil, false
  72. }
  73. var out interface{}
  74. if err := json.Unmarshal(b, &out); err != nil {
  75. opChain.fail(AssertionFailure{
  76. Type: AssertValid,
  77. Actual: &AssertionValue{in},
  78. Errors: []error{
  79. errors.New("expected: unmarshalable value"),
  80. err,
  81. },
  82. })
  83. return nil, false
  84. }
  85. return out, true
  86. }
  87. func canonDecode(opChain *chain, value interface{}, target interface{}) {
  88. if target == nil {
  89. opChain.fail(AssertionFailure{
  90. Type: AssertUsage,
  91. Errors: []error{
  92. errors.New("unexpected nil target argument"),
  93. },
  94. })
  95. return
  96. }
  97. b, err := json.Marshal(value)
  98. if err != nil {
  99. opChain.fail(AssertionFailure{
  100. Type: AssertValid,
  101. Actual: &AssertionValue{value},
  102. Errors: []error{
  103. errors.New("expected: marshable value"),
  104. },
  105. })
  106. return
  107. }
  108. if err := json.Unmarshal(b, target); err != nil {
  109. opChain.fail(AssertionFailure{
  110. Type: AssertValid,
  111. Actual: &AssertionValue{target},
  112. Errors: []error{
  113. errors.New("expected: value can be unmarshaled into target argument"),
  114. },
  115. })
  116. return
  117. }
  118. }