github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/test/marshal_bool_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 TestMarshalBool_ptr_as_string(t *testing.T) {
    13  	t.Run("direct-false", func(t *testing.T) {
    14  		var data = struct {
    15  			Value *bool `php:"value,string"`
    16  		}{
    17  			Value: null.BoolFrom(false).Ptr(),
    18  		}
    19  
    20  		actual, err := phpserialize.Marshal(&data)
    21  		require.NoError(t, err)
    22  		expected := `a:1:{s:5:"value";s:5:"false";}`
    23  		test.StringEqual(t, expected, string(actual))
    24  	})
    25  
    26  	t.Run("direct-true", func(t *testing.T) {
    27  		var data = struct {
    28  			Value *bool `php:"value,string"`
    29  		}{
    30  			Value: null.BoolFrom(true).Ptr(),
    31  		}
    32  
    33  		actual, err := phpserialize.Marshal(&data)
    34  		require.NoError(t, err)
    35  		expected := `a:1:{s:5:"value";s:4:"true";}`
    36  		test.StringEqual(t, expected, string(actual))
    37  	})
    38  
    39  	t.Run("indirect-false", func(t *testing.T) {
    40  		var data = struct {
    41  			Value *bool `php:"value,string"`
    42  			B     *bool
    43  		}{
    44  			Value: null.BoolFrom(false).Ptr(),
    45  		}
    46  
    47  		actual, err := phpserialize.Marshal(&data)
    48  		require.NoError(t, err)
    49  		expected := `a:2:{s:5:"value";s:5:"false";s:1:"B";N;}`
    50  		test.StringEqual(t, expected, string(actual))
    51  	})
    52  
    53  	t.Run("indirect-true", func(t *testing.T) {
    54  		var data = struct {
    55  			Value *bool `php:"value,string"`
    56  			B     *bool
    57  		}{
    58  			Value: null.BoolFrom(true).Ptr(),
    59  		}
    60  
    61  		actual, err := phpserialize.Marshal(&data)
    62  		require.NoError(t, err)
    63  		expected := `a:2:{s:5:"value";s:4:"true";s:1:"B";N;}`
    64  		test.StringEqual(t, expected, string(actual))
    65  	})
    66  }