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

     1  package deep
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  type (
     9  	A struct {
    10  		A int
    11  		B string
    12  		C uint64
    13  		D []int
    14  	}
    15  
    16  	B struct {
    17  		A A
    18  		B int `deep:"print:omit"`
    19  		C []byte
    20  		D []int
    21  		E interface{}
    22  	}
    23  )
    24  
    25  func TestFprint(t *testing.T) {
    26  	b := B{
    27  		A: A{
    28  			A: 1,
    29  			B: "second",
    30  			C: 5,
    31  		},
    32  		B: 9,
    33  		C: []byte("123"),
    34  		D: []int{1, 2, 3},
    35  	}
    36  
    37  	var buf bytes.Buffer
    38  
    39  	n, err := Fprint(&buf, &b)
    40  	if err != nil {
    41  		t.Errorf("fprint error: %v", err)
    42  	}
    43  	if n != buf.Len() {
    44  		t.Errorf("bad size: %d != %d", n, buf.Len())
    45  	}
    46  
    47  	if b := buf.Bytes(); len(b) == 0 {
    48  		t.Errorf("empty result")
    49  	}
    50  
    51  	t.Logf("result:\n%s", buf.Bytes())
    52  }
    53  
    54  func TestEqual(t *testing.T) {
    55  	x := B{
    56  		A: A{
    57  			A: 1,
    58  			B: "second",
    59  			C: 5,
    60  		},
    61  		B: 9,
    62  		C: []byte("123"),
    63  		D: []int{1, 2, 3},
    64  		E: 5,
    65  	}
    66  
    67  	y := B{
    68  		A: A{
    69  			A: 1,
    70  			B: "second",
    71  			C: 5,
    72  		},
    73  		B: 9,
    74  		C: []byte("123"),
    75  		D: []int{1, 2, 3},
    76  		E: 5,
    77  	}
    78  
    79  	eq := Equal(x, y)
    80  	if !eq {
    81  		t.Errorf("excepted to be equal")
    82  	}
    83  
    84  	//
    85  
    86  	y.D[2]++
    87  
    88  	eq = Equal(x, y)
    89  	if eq {
    90  		t.Errorf("excepted to not to be equal")
    91  	}
    92  
    93  	y.D[2]--
    94  
    95  	//
    96  
    97  	y.E = 6
    98  
    99  	eq = Equal(x, y)
   100  	if eq {
   101  		t.Errorf("excepted to not to be equal")
   102  	}
   103  
   104  	//
   105  
   106  	y.E = int64(5)
   107  
   108  	eq = Equal(x, y)
   109  	if eq {
   110  		t.Errorf("excepted to not to be equal")
   111  	}
   112  }