assert.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package awstesting
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "net/url"
  7. "reflect"
  8. "regexp"
  9. "sort"
  10. "testing"
  11. )
  12. // Match is a testing helper to test for testing error by comparing expected
  13. // with a regular expression.
  14. func Match(t *testing.T, regex, expected string) {
  15. if !regexp.MustCompile(regex).Match([]byte(expected)) {
  16. t.Errorf("%q\n\tdoes not match /%s/", expected, regex)
  17. }
  18. }
  19. // AssertURL verifies the expected URL is matches the actual.
  20. func AssertURL(t *testing.T, expect, actual string, msgAndArgs ...interface{}) bool {
  21. expectURL, err := url.Parse(expect)
  22. if err != nil {
  23. t.Errorf(errMsg("unable to parse expected URL", err, msgAndArgs))
  24. return false
  25. }
  26. actualURL, err := url.Parse(actual)
  27. if err != nil {
  28. t.Errorf(errMsg("unable to parse actual URL", err, msgAndArgs))
  29. return false
  30. }
  31. equal(t, expectURL.Host, actualURL.Host, msgAndArgs...)
  32. equal(t, expectURL.Scheme, actualURL.Scheme, msgAndArgs...)
  33. equal(t, expectURL.Path, actualURL.Path, msgAndArgs...)
  34. return AssertQuery(t, expectURL.Query().Encode(), actualURL.Query().Encode(), msgAndArgs...)
  35. }
  36. // AssertQuery verifies the expect HTTP query string matches the actual.
  37. func AssertQuery(t *testing.T, expect, actual string, msgAndArgs ...interface{}) bool {
  38. expectQ, err := url.ParseQuery(expect)
  39. if err != nil {
  40. t.Errorf(errMsg("unable to parse expected Query", err, msgAndArgs))
  41. return false
  42. }
  43. actualQ, err := url.ParseQuery(expect)
  44. if err != nil {
  45. t.Errorf(errMsg("unable to parse actual Query", err, msgAndArgs))
  46. return false
  47. }
  48. // Make sure the keys are the same
  49. if !equal(t, queryValueKeys(expectQ), queryValueKeys(actualQ), msgAndArgs...) {
  50. return false
  51. }
  52. for k, expectQVals := range expectQ {
  53. sort.Strings(expectQVals)
  54. actualQVals := actualQ[k]
  55. sort.Strings(actualQVals)
  56. equal(t, expectQVals, actualQVals, msgAndArgs...)
  57. }
  58. return true
  59. }
  60. // AssertJSON verifies that the expect json string matches the actual.
  61. func AssertJSON(t *testing.T, expect, actual string, msgAndArgs ...interface{}) bool {
  62. expectVal := map[string]interface{}{}
  63. if err := json.Unmarshal([]byte(expect), &expectVal); err != nil {
  64. t.Errorf(errMsg("unable to parse expected JSON", err, msgAndArgs...))
  65. return false
  66. }
  67. actualVal := map[string]interface{}{}
  68. if err := json.Unmarshal([]byte(actual), &actualVal); err != nil {
  69. t.Errorf(errMsg("unable to parse actual JSON", err, msgAndArgs...))
  70. return false
  71. }
  72. return equal(t, expectVal, actualVal, msgAndArgs...)
  73. }
  74. // AssertXML verifies that the expect xml string matches the actual.
  75. func AssertXML(t *testing.T, expect, actual string, container interface{}, msgAndArgs ...interface{}) bool {
  76. expectVal := container
  77. if err := xml.Unmarshal([]byte(expect), &expectVal); err != nil {
  78. t.Errorf(errMsg("unable to parse expected XML", err, msgAndArgs...))
  79. }
  80. actualVal := container
  81. if err := xml.Unmarshal([]byte(actual), &actualVal); err != nil {
  82. t.Errorf(errMsg("unable to parse actual XML", err, msgAndArgs...))
  83. }
  84. return equal(t, expectVal, actualVal, msgAndArgs...)
  85. }
  86. // objectsAreEqual determines if two objects are considered equal.
  87. //
  88. // This function does no assertion of any kind.
  89. //
  90. // Based on github.com/stretchr/testify/assert.ObjectsAreEqual
  91. // Copied locally to prevent non-test build dependencies on testify
  92. func objectsAreEqual(expected, actual interface{}) bool {
  93. if expected == nil || actual == nil {
  94. return expected == actual
  95. }
  96. return reflect.DeepEqual(expected, actual)
  97. }
  98. // Equal asserts that two objects are equal.
  99. //
  100. // assert.Equal(t, 123, 123, "123 and 123 should be equal")
  101. //
  102. // Returns whether the assertion was successful (true) or not (false).
  103. //
  104. // Based on github.com/stretchr/testify/assert.Equal
  105. // Copied locally to prevent non-test build dependencies on testify
  106. func equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool {
  107. if !objectsAreEqual(expected, actual) {
  108. t.Errorf("Not Equal:\n\t%#v (expected)\n\t%#v (actual), %s",
  109. expected, actual, messageFromMsgAndArgs(msgAndArgs))
  110. return false
  111. }
  112. return true
  113. }
  114. func errMsg(baseMsg string, err error, msgAndArgs ...interface{}) string {
  115. message := messageFromMsgAndArgs(msgAndArgs)
  116. if message != "" {
  117. message += ", "
  118. }
  119. return fmt.Sprintf("%s%s, %v", message, baseMsg, err)
  120. }
  121. // Based on github.com/stretchr/testify/assert.messageFromMsgAndArgs
  122. // Copied locally to prevent non-test build dependencies on testify
  123. func messageFromMsgAndArgs(msgAndArgs []interface{}) string {
  124. if len(msgAndArgs) == 0 || msgAndArgs == nil {
  125. return ""
  126. }
  127. if len(msgAndArgs) == 1 {
  128. return msgAndArgs[0].(string)
  129. }
  130. if len(msgAndArgs) > 1 {
  131. return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
  132. }
  133. return ""
  134. }
  135. func queryValueKeys(v url.Values) []string {
  136. keys := make([]string, 0, len(v))
  137. for k := range v {
  138. keys = append(keys, k)
  139. }
  140. sort.Strings(keys)
  141. return keys
  142. }