github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/test/marshal_uint_test.go (about)

     1  package test_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/trim21/go-phpserialize"
     8  	"github.com/trim21/go-phpserialize/internal/test"
     9  	"github.com/volatiletech/null/v9"
    10  )
    11  
    12  func TestMarshal_uint_as_string(t *testing.T) {
    13  	var data = struct {
    14  		A uint8  `php:"a,string"`
    15  		B uint16 `php:"b,string"`
    16  		C uint32 `php:"c,string"`
    17  		D uint64 `php:"d,string"`
    18  		E uint   `php:"e,string"`
    19  	}{
    20  		A: 2,
    21  		B: 3,
    22  		C: 0,
    23  		D: 52,
    24  		E: 110,
    25  	}
    26  
    27  	actual, err := phpserialize.Marshal(&data)
    28  	require.NoError(t, err)
    29  	expected := `a:5:{s:1:"a";s:1:"2";s:1:"b";s:1:"3";s:1:"c";s:1:"0";s:1:"d";s:2:"52";s:1:"e";s:3:"110";}`
    30  	test.StringEqual(t, expected, string(actual))
    31  }
    32  
    33  func TestMarshal_uint_as_string_omitempty(t *testing.T) {
    34  	var data = struct {
    35  		A uint8  `php:"a,string,omitempty"`
    36  		B uint16 `php:"b,string,omitempty"`
    37  		C uint32 `php:"c,string,omitempty"`
    38  		D uint64 `php:"d,string,omitempty"`
    39  		E uint   `php:"e,string,omitempty"`
    40  	}{}
    41  
    42  	actual, err := phpserialize.Marshal(&data)
    43  	require.NoError(t, err)
    44  	expected := `a:0:{}`
    45  	test.StringEqual(t, expected, string(actual))
    46  }
    47  
    48  func TestMarshal_uint_as_string_ptr_omitempty(t *testing.T) {
    49  	t.Run("indirect", func(t *testing.T) {
    50  		var data = struct {
    51  			A *uint8  `php:"a,string,omitempty"`
    52  			B *uint16 `php:"b,string,omitempty"`
    53  			C *uint32 `php:"c,string,omitempty"`
    54  			D *uint64 `php:"d,string,omitempty"`
    55  			E *uint   `php:"e,string,omitempty"`
    56  		}{
    57  			A: null.Uint8From(0).Ptr(),
    58  			B: null.Uint16From(0).Ptr(),
    59  			C: null.Uint32From(0).Ptr(),
    60  			D: null.Uint64From(0).Ptr(),
    61  			E: null.UintFrom(0).Ptr(),
    62  		}
    63  
    64  		actual, err := phpserialize.Marshal(&data)
    65  		require.NoError(t, err)
    66  		expected := `a:5:{s:1:"a";s:1:"0";s:1:"b";s:1:"0";s:1:"c";s:1:"0";s:1:"d";s:1:"0";s:1:"e";s:1:"0";}`
    67  		test.StringEqual(t, expected, string(actual))
    68  	})
    69  
    70  	t.Run("uint8-indirect", func(t *testing.T) {
    71  		var data = struct {
    72  			A *uint8 `php:"a,string,omitempty"`
    73  		}{
    74  			A: null.Uint8From(0).Ptr(),
    75  		}
    76  
    77  		actual, err := phpserialize.Marshal(&data)
    78  		require.NoError(t, err)
    79  		expected := `a:1:{s:1:"a";s:1:"0";}`
    80  		test.StringEqual(t, expected, string(actual))
    81  	})
    82  
    83  	t.Run("uint16-direct", func(t *testing.T) {
    84  		var data = struct {
    85  			B *uint16 `php:"b,string,omitempty"`
    86  		}{
    87  			B: null.Uint16From(0).Ptr(),
    88  		}
    89  
    90  		actual, err := phpserialize.Marshal(&data)
    91  		require.NoError(t, err)
    92  		expected := `a:1:{s:1:"b";s:1:"0";}`
    93  		test.StringEqual(t, expected, string(actual))
    94  	})
    95  
    96  	t.Run("uint32-direct", func(t *testing.T) {
    97  		var data = struct {
    98  			C *uint32 `php:"c,string,omitempty"`
    99  		}{
   100  			C: null.Uint32From(0).Ptr(),
   101  		}
   102  
   103  		actual, err := phpserialize.Marshal(&data)
   104  		require.NoError(t, err)
   105  		expected := `a:1:{s:1:"c";s:1:"0";}`
   106  		test.StringEqual(t, expected, string(actual))
   107  	})
   108  
   109  	t.Run("uint64-direct", func(t *testing.T) {
   110  		var data = struct {
   111  			D *uint64 `php:"d,string,omitempty"`
   112  		}{
   113  			D: null.Uint64From(0).Ptr(),
   114  		}
   115  
   116  		actual, err := phpserialize.Marshal(&data)
   117  		require.NoError(t, err)
   118  		expected := `a:1:{s:1:"d";s:1:"0";}`
   119  		test.StringEqual(t, expected, string(actual))
   120  	})
   121  
   122  	t.Run("uint-direct", func(t *testing.T) {
   123  		var data = struct {
   124  			E *uint `php:"e,string,omitempty"`
   125  		}{
   126  			E: null.UintFrom(0).Ptr(),
   127  		}
   128  
   129  		actual, err := phpserialize.Marshal(&data)
   130  		require.NoError(t, err)
   131  		expected := `a:1:{s:1:"e";s:1:"0";}`
   132  		test.StringEqual(t, expected, string(actual))
   133  	})
   134  }