github.com/gofiber/fiber/v2@v2.47.0/utils/assertions.go (about)

     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  
     5  package utils
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"log"
    11  	"path/filepath"
    12  	"reflect"
    13  	"runtime"
    14  	"testing"
    15  	"text/tabwriter"
    16  )
    17  
    18  // AssertEqual checks if values are equal
    19  func AssertEqual(tb testing.TB, expected, actual interface{}, description ...string) { //nolint:thelper // TODO: Verify if tb can be nil
    20  	if tb != nil {
    21  		tb.Helper()
    22  	}
    23  
    24  	if reflect.DeepEqual(expected, actual) {
    25  		return
    26  	}
    27  
    28  	aType := "<nil>"
    29  	bType := "<nil>"
    30  
    31  	if expected != nil {
    32  		aType = reflect.TypeOf(expected).String()
    33  	}
    34  	if actual != nil {
    35  		bType = reflect.TypeOf(actual).String()
    36  	}
    37  
    38  	testName := "AssertEqual"
    39  	if tb != nil {
    40  		testName = tb.Name()
    41  	}
    42  
    43  	_, file, line, _ := runtime.Caller(1)
    44  
    45  	var buf bytes.Buffer
    46  	const pad = 5
    47  	w := tabwriter.NewWriter(&buf, 0, 0, pad, ' ', 0)
    48  	_, _ = fmt.Fprintf(w, "\nTest:\t%s", testName)
    49  	_, _ = fmt.Fprintf(w, "\nTrace:\t%s:%d", filepath.Base(file), line)
    50  	if len(description) > 0 {
    51  		_, _ = fmt.Fprintf(w, "\nDescription:\t%s", description[0])
    52  	}
    53  	_, _ = fmt.Fprintf(w, "\nExpect:\t%v\t(%s)", expected, aType)
    54  	_, _ = fmt.Fprintf(w, "\nResult:\t%v\t(%s)", actual, bType)
    55  
    56  	result := ""
    57  	if err := w.Flush(); err != nil {
    58  		result = err.Error()
    59  	} else {
    60  		result = buf.String()
    61  	}
    62  
    63  	if tb != nil {
    64  		tb.Fatal(result)
    65  	} else {
    66  		log.Fatal(result) //nolint:revive // tb might be nil, so we need a fallback
    67  	}
    68  }