github.com/aergoio/aergo@v1.3.1/types/p2plogging_test.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package types
     7  
     8  import (
     9  	"bytes"
    10  	"fmt"
    11  	"github.com/aergoio/aergo-lib/log"
    12  	"github.com/gofrs/uuid"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestLogB58EncMarshaller_MarshalZerologArray(t *testing.T) {
    18  	sampleID := make([][]byte,10)
    19  	for i:=0; i<10; i++ {
    20  		sampleID[i] = uuid.Must(uuid.NewV4()).Bytes()
    21  	}
    22  	type fields struct {
    23  		arr   [][]byte
    24  		limit int
    25  	}
    26  	tests := []struct {
    27  		name   string
    28  		fields fields
    29  
    30  		wantNum int
    31  	}{
    32  		{"TSmall", fields{sampleID, 9}, 2},
    33  		{"TSame", fields{sampleID, 10}, 0},
    34  		{"TBig", fields{sampleID, 100}, 0},
    35  	}
    36  	for _, tt := range tests {
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			buf := bytes.NewBuffer(nil)
    39  			logger := log.NewLogger("test").Output(buf)
    40  			m := NewLogB58EncMarshaller(tt.fields.arr, tt.fields.limit)
    41  
    42  			logger.Warn().Array("target", m).Msg("do test")
    43  			logStr := buf.String()
    44  
    45  			if tt.wantNum>0 {
    46  				moreMsg := fmt.Sprintf("%d more", tt.wantNum)
    47  				if !strings.Contains(logStr, moreMsg) {
    48  					t.Errorf("result %s, want exact %s items ",logStr, moreMsg)
    49  				}
    50  			} else {
    51  				if strings.Contains(logStr,  "more") {
    52  					t.Errorf("result %s, want no dropped item ",logStr)
    53  				}
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func TestLogBlockHashMarshaller_MarshalZerologArray(t *testing.T) {
    60  	sampleBlks := make([]*Block,10)
    61  	for i:=0; i<10; i++ {
    62  		sampleBlks[i] = &Block{Hash:uuid.Must(uuid.NewV4()).Bytes()}
    63  	}
    64  	type fields struct {
    65  		arr   []*Block
    66  		limit int
    67  	}
    68  	tests := []struct {
    69  		name   string
    70  		fields fields
    71  
    72  		wantNum int
    73  	}{
    74  		{"TSmall", fields{sampleBlks, 9}, 2},
    75  		{"TSame", fields{sampleBlks, 10}, 0},
    76  		{"TBig", fields{sampleBlks, 100}, 0},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			buf := bytes.NewBuffer(nil)
    81  			logger := log.NewLogger("test").Output(buf)
    82  			m := LogBlockHashMarshaller{
    83  				arr:   tt.fields.arr,
    84  				limit: tt.fields.limit,
    85  			}
    86  			logger.Warn().Array("target", m).Msg("do test")
    87  			logStr := buf.String()
    88  			if tt.wantNum>0 {
    89  				moreMsg := fmt.Sprintf("%d more", tt.wantNum)
    90  				if !strings.Contains(logStr, moreMsg) {
    91  					t.Errorf("result %s , want exact %s items ",buf.String(), moreMsg)
    92  				}
    93  			} else {
    94  				if strings.Contains(logStr,  "more") {
    95  					t.Errorf("result %s , want no dropped item ", buf.String())
    96  				}
    97  			}
    98  		})
    99  	}
   100  }