code.vegaprotocol.io/vega@v0.79.0/libs/stringer/stringer_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package stringer_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/libs/num"
    22  	"code.vegaprotocol.io/vega/libs/stringer"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestToString(t *testing.T) {
    28  	tcs := []struct {
    29  		name     string
    30  		stringer stringer.Stringer
    31  		expected string
    32  	}{
    33  		{
    34  			name:     "with nil interface",
    35  			stringer: nil,
    36  			expected: "nil",
    37  		}, {
    38  			name:     "with nil struct",
    39  			stringer: stringer.Stringer(nil),
    40  			expected: "nil",
    41  		}, {
    42  			name:     "with existing struct",
    43  			stringer: &dummyStringer{},
    44  			expected: "stringer",
    45  		},
    46  	}
    47  
    48  	for _, tc := range tcs {
    49  		t.Run(tc.name, func(tt *testing.T) {
    50  			// when
    51  			str := stringer.ObjToString(tc.stringer)
    52  
    53  			// then
    54  			assert.Equal(tt, tc.expected, str)
    55  		})
    56  	}
    57  }
    58  
    59  func TestPtrToString(t *testing.T) {
    60  	tcs := []struct {
    61  		name     string
    62  		num      *num.Uint
    63  		expected string
    64  	}{
    65  		{
    66  			name:     "with nil number",
    67  			num:      nil,
    68  			expected: "nil",
    69  		}, {
    70  			name:     "with existing number",
    71  			num:      num.NewUint(42),
    72  			expected: "42",
    73  		},
    74  	}
    75  
    76  	for _, tc := range tcs {
    77  		t.Run(tc.name, func(tt *testing.T) {
    78  			// when
    79  			str := stringer.PtrToString(tc.num)
    80  
    81  			// then
    82  			assert.Equal(tt, tc.expected, str)
    83  		})
    84  	}
    85  }
    86  
    87  func TestInt64PointerToString(t *testing.T) {
    88  	tcs := []struct {
    89  		name     string
    90  		num      *int64
    91  		expected string
    92  	}{
    93  		{
    94  			name:     "with nil number",
    95  			num:      nil,
    96  			expected: "nil",
    97  		}, {
    98  			name: "with existing number",
    99  			num: func() *int64 {
   100  				n := int64(42)
   101  				return &n
   102  			}(),
   103  			expected: "42",
   104  		},
   105  	}
   106  
   107  	for _, tc := range tcs {
   108  		t.Run(tc.name, func(tt *testing.T) {
   109  			// when
   110  			str := stringer.PtrToString(tc.num)
   111  
   112  			// then
   113  			assert.Equal(tt, tc.expected, str)
   114  		})
   115  	}
   116  }
   117  
   118  type dummyStringer struct{}
   119  
   120  func (d dummyStringer) String() string {
   121  	return "stringer"
   122  }