github.com/nikandfor/assert@v0.0.0-20231112165957-bf2ce0a3555a/is/equal.go (about)

     1  package is
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"runtime/debug"
     8  
     9  	"github.com/nikandfor/assert/deep"
    10  )
    11  
    12  func Equal(a, b interface{}) Checker {
    13  	return CheckerFunc(func(w io.Writer) bool {
    14  		var buf bytes.Buffer
    15  
    16  		defer func() {
    17  			p := recover()
    18  			if p == nil {
    19  				return
    20  			}
    21  
    22  			fmt.Fprintf(w, "PANIC: %v\n", p)
    23  			fmt.Fprintf(w, "%s", debug.Stack())
    24  		}()
    25  
    26  		eq := deep.Diff(&buf, a, b)
    27  		if eq {
    28  			return true
    29  		}
    30  
    31  		//	fmt.Fprintf(w, "Not equal:\nExpected: %#v\nActual:   %#v\nDiff:\n%s", a, b, buf.Bytes())
    32  		fmt.Fprintf(w, "Not equal:\nExpected: ")
    33  
    34  		_, err := deep.Fprint(w, a)
    35  		if err != nil {
    36  			fmt.Fprintf(w, "PRINT ERROR: %v\n", err)
    37  		}
    38  
    39  		fmt.Fprintf(w, "\nActual:   ")
    40  
    41  		_, err = deep.Fprint(w, b)
    42  		if err != nil {
    43  			fmt.Fprintf(w, "PRINT ERROR: %v\n", err)
    44  		}
    45  
    46  		fmt.Fprintf(w, "\nDiff:\n%s", buf.Bytes())
    47  
    48  		return false
    49  	})
    50  }
    51  
    52  func NotEqual(a, b interface{}) Checker {
    53  	return CheckerFunc(func(w io.Writer) bool {
    54  		var buf bytes.Buffer
    55  
    56  		defer func() {
    57  			p := recover()
    58  			if p == nil {
    59  				return
    60  			}
    61  
    62  			fmt.Fprintf(w, "PANIC: %v\n", p)
    63  			fmt.Fprintf(w, "%s", debug.Stack())
    64  		}()
    65  
    66  		eq := deep.Diff(&buf, a, b)
    67  		if !eq {
    68  			return true
    69  		}
    70  
    71  		//	fmt.Fprintf(w, "Not equal:\nExpected: %#v\nActual:   %#v\nDiff:\n%s", a, b, buf.Bytes())
    72  		fmt.Fprintf(w, "Expected not equal: ")
    73  
    74  		_, err := deep.Fprint(w, a)
    75  		if err != nil {
    76  			fmt.Fprintf(w, "PRINT ERROR: %v\n", err)
    77  		}
    78  
    79  		return false
    80  	})
    81  }