assertions.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 Github Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package utils
  5. import (
  6. "bytes"
  7. "fmt"
  8. "log"
  9. "path/filepath"
  10. "reflect"
  11. "runtime"
  12. "testing"
  13. "text/tabwriter"
  14. )
  15. // AssertEqual checks if values are equal
  16. func AssertEqual(tb testing.TB, expected, actual interface{}, description ...string) {
  17. if tb != nil {
  18. tb.Helper()
  19. }
  20. if reflect.DeepEqual(expected, actual) {
  21. return
  22. }
  23. aType := "<nil>"
  24. bType := "<nil>"
  25. if expected != nil {
  26. aType = reflect.TypeOf(expected).String()
  27. }
  28. if actual != nil {
  29. bType = reflect.TypeOf(actual).String()
  30. }
  31. testName := "AssertEqual"
  32. if tb != nil {
  33. testName = tb.Name()
  34. }
  35. _, file, line, _ := runtime.Caller(1)
  36. var buf bytes.Buffer
  37. w := tabwriter.NewWriter(&buf, 0, 0, 5, ' ', 0)
  38. fmt.Fprintf(w, "\nTest:\t%s", testName)
  39. fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
  40. if len(description) > 0 {
  41. fmt.Fprintf(w, "\nDescription:\t%s", description[0])
  42. }
  43. fmt.Fprintf(w, "\nExpect:\t%v\t(%s)", expected, aType)
  44. fmt.Fprintf(w, "\nResult:\t%v\t(%s)", actual, bType)
  45. result := ""
  46. if err := w.Flush(); err != nil {
  47. result = err.Error()
  48. } else {
  49. result = buf.String()
  50. }
  51. if tb != nil {
  52. tb.Fatal(result)
  53. } else {
  54. log.Fatal(result)
  55. }
  56. }