github.com/slonegd/structstringer@v0.0.0-20200810122817-9bee9ae25e10/examples/recursive/recursive_test.go (about)

     1  //nolint
     2  package recursive
     3  
     4  import (
     5  	"fmt"
     6  	"math/rand"
     7  	"testing"
     8  
     9  	"github.com/slonegd/structstringer/examples/simple"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestAString(t *testing.T) {
    14  	tests := []struct {
    15  		a       A
    16  		want    string
    17  		fmtWant string
    18  	}{
    19  		{
    20  			a: A{i: 42, flag: true, b: B{i: 43}},
    21  			want: `
    22  recursive.A{
    23  	i    int         42
    24  	flag bool        true
    25  	b    recursive.B {
    26  		i    int         43
    27  		flag bool        false
    28  		c    recursive.C {
    29  			i    int  0
    30  			flag bool false
    31  		}
    32  	}
    33  }`,
    34  			fmtWant: "recursive.A{i:42, flag:true, b:recursive.B{i:43, flag:false, c:recursive.C{i:0, flag:false}}}",
    35  		},
    36  	}
    37  	for _, tt := range tests {
    38  		assert.Equal(t, tt.want, tt.a.String())
    39  		assert.Equal(t, tt.fmtWant, fmt.Sprintf("%#v", tt.a))
    40  	}
    41  }
    42  
    43  func TestDString(t *testing.T) {
    44  	tests := []struct {
    45  		d       D
    46  		want    string
    47  		fmtWant string
    48  	}{
    49  		{
    50  			d: D{i: 42, flag: true, b: simple.B{I: 43}},
    51  			want: `
    52  recursive.D{
    53  	i    int      42
    54  	flag bool     true
    55  	b    simple.B {
    56  		I int 43
    57  	}
    58  }`,
    59  			fmtWant: "recursive.D{i:42, flag:true, b:simple.B{I:43}}",
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		assert.Equal(t, tt.want, tt.d.String())
    64  		assert.Equal(t, tt.fmtWant, fmt.Sprintf("%#v", tt.d))
    65  	}
    66  }
    67  
    68  func TestEString(t *testing.T) {
    69  	tests := []struct {
    70  		e       E
    71  		want    string
    72  		fmtWant string
    73  	}{
    74  		{
    75  			e: E{i: 42, flag: true, c: simple.C{I: 43}},
    76  			want: `
    77  recursive.E{
    78  	i    int      42
    79  	flag bool     true
    80  	c    simple.C {
    81  		I    int  43
    82  		flag bool not_implemented_unexported_fields
    83  	}
    84  }`,
    85  			fmtWant: "recursive.E{i:42, flag:true, c:simple.C{I:43, flag:false}}",
    86  		},
    87  	}
    88  	for _, tt := range tests {
    89  		assert.Equal(t, tt.want, tt.e.String())
    90  		assert.Equal(t, tt.fmtWant, fmt.Sprintf("%#v", tt.e))
    91  	}
    92  }
    93  
    94  func BenchmarkRecursiveAString(b *testing.B) {
    95  	b.ResetTimer()
    96  	for i := 0; i < 1000; i++ {
    97  		a := randomA()
    98  		b.StartTimer()
    99  		_ = a.String()
   100  		b.StopTimer()
   101  	}
   102  }
   103  
   104  func BenchmarkRecursiveAfmt(b *testing.B) {
   105  	b.ResetTimer()
   106  	for i := 0; i < 1000; i++ {
   107  		a := randomA()
   108  		b.StartTimer()
   109  		_ = fmt.Sprintf("%#v", a)
   110  		b.StopTimer()
   111  	}
   112  }
   113  
   114  func randomA() A {
   115  	return A{
   116  		i:    rand.Int(),
   117  		flag: rand.Int()%2 == 0,
   118  		b: B{
   119  			i:    rand.Int(),
   120  			flag: rand.Int()%2 == 0,
   121  			c: C{
   122  				i:    rand.Int(),
   123  				flag: rand.Int()%2 == 0,
   124  			},
   125  		},
   126  	}
   127  }