assertions.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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(t testing.TB, expected interface{}, actual interface{}, description ...string) {
  17. if reflect.DeepEqual(expected, actual) {
  18. return
  19. }
  20. var aType = "<nil>"
  21. var bType = "<nil>"
  22. if reflect.ValueOf(expected).IsValid() {
  23. aType = reflect.TypeOf(expected).Name()
  24. }
  25. if reflect.ValueOf(actual).IsValid() {
  26. bType = reflect.TypeOf(actual).Name()
  27. }
  28. testName := "AssertEqual"
  29. if t != nil {
  30. testName = t.Name()
  31. }
  32. _, file, line, _ := runtime.Caller(1)
  33. var buf bytes.Buffer
  34. w := tabwriter.NewWriter(&buf, 0, 0, 5, ' ', 0)
  35. fmt.Fprintf(w, "\nTest:\t%s", testName)
  36. fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
  37. fmt.Fprintf(w, "\nError:\tNot equal")
  38. fmt.Fprintf(w, "\nExpect:\t%v\t[%s]", expected, aType)
  39. fmt.Fprintf(w, "\nResult:\t%v\t[%s]", actual, bType)
  40. if len(description) > 0 {
  41. fmt.Fprintf(w, "\nDescription:\t%s", description[0])
  42. }
  43. result := ""
  44. if err := w.Flush(); err != nil {
  45. result = err.Error()
  46. } else {
  47. result = buf.String()
  48. }
  49. if t != nil {
  50. t.Fatal(result)
  51. } else {
  52. log.Fatal(result)
  53. }
  54. }