...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldvalue/value_text_conversion_test.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/ldvalue

     1  package ldvalue
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func makeMarshallableValues() []Value {
    11  	return []Value{
    12  		Null(),
    13  		Bool(true),
    14  		Bool(false),
    15  		Int(1),
    16  		Float64(2.5),
    17  		String("x"),
    18  		ArrayBuild().Add(Bool(true)).Add(String("x")).Build(),
    19  		ObjectBuild().Set("a", Bool(true)).Build(),
    20  	}
    21  }
    22  
    23  func makeMarshallableValueArrays() []ValueArray {
    24  	return []ValueArray{
    25  		{},
    26  		ValueArrayOf(),
    27  		ValueArrayOf(String("a")),
    28  	}
    29  }
    30  
    31  func makeMarshallableValueMaps() []ValueMap {
    32  	return []ValueMap{
    33  		{},
    34  		ValueMapBuild().Build(),
    35  		ValueMapBuild().Set("a", Int(1)).Build(),
    36  	}
    37  }
    38  
    39  func TestValueAsStringerIsSameAsJSONString(t *testing.T) {
    40  	for _, value := range makeMarshallableValues() {
    41  		t.Run(value.JSONString(), func(t *testing.T) {
    42  			assert.Equal(t, value.JSONString(), value.String())
    43  		})
    44  	}
    45  }
    46  
    47  func TestValueArrayAsStringerIsSameAsJSONString(t *testing.T) {
    48  	for _, value := range makeMarshallableValueArrays() {
    49  		t.Run(value.JSONString(), func(t *testing.T) {
    50  			assert.Equal(t, value.JSONString(), value.String())
    51  		})
    52  	}
    53  }
    54  
    55  func TestValueMapAsStringerIsSameAsJSONString(t *testing.T) {
    56  	for _, value := range makeMarshallableValueMaps() {
    57  		t.Run(value.JSONString(), func(t *testing.T) {
    58  			assert.Equal(t, value.JSONString(), value.String())
    59  		})
    60  	}
    61  }
    62  
    63  func TestValueMarshalTextIsSameAsJSONString(t *testing.T) {
    64  	for _, value := range makeMarshallableValues() {
    65  		t.Run(value.JSONString(), func(t *testing.T) {
    66  			bytes, err := value.MarshalText()
    67  			require.NoError(t, err)
    68  			assert.Equal(t, value.JSONString(), string(bytes))
    69  		})
    70  	}
    71  }
    72  
    73  func TestValueUnmarshalText(t *testing.T) {
    74  	for _, expected := range makeMarshallableValues() {
    75  		t.Run(expected.JSONString(), func(t *testing.T) {
    76  			var actual Value
    77  			require.NoError(t, actual.UnmarshalText([]byte(expected.JSONString())))
    78  			assert.Equal(t, expected, actual)
    79  		})
    80  	}
    81  
    82  	t.Run("non-JSON inputs are treated as strings", func(t *testing.T) {
    83  		for _, s := range []string{``, `t`, `{hello`} {
    84  			expected := String(s)
    85  			var actual Value
    86  			require.NoError(t, actual.UnmarshalText([]byte(s)))
    87  			assert.Equal(t, expected, actual)
    88  		}
    89  	})
    90  }
    91  
    92  func TestValueArrayMarshalTextIsSameAsJSONMarshal(t *testing.T) {
    93  	for _, expected := range makeMarshallableValueArrays() {
    94  		t.Run(expected.JSONString(), func(t *testing.T) {
    95  			bytes, err := expected.MarshalText()
    96  			require.NoError(t, err)
    97  			assert.Equal(t, expected.JSONString(), string(bytes))
    98  		})
    99  	}
   100  }
   101  
   102  func TestValueArrayUnmarshalTextIsSameAsJSONUnmarshal(t *testing.T) {
   103  	for _, expected := range makeMarshallableValueArrays() {
   104  		t.Run(expected.JSONString(), func(t *testing.T) {
   105  			var actual ValueArray
   106  			require.NoError(t, actual.UnmarshalText([]byte(expected.JSONString())))
   107  			assert.Equal(t, expected, actual)
   108  		})
   109  	}
   110  }
   111  
   112  func TestValueMapMarshalTextIsSameAsJSONMarshal(t *testing.T) {
   113  	for _, expected := range makeMarshallableValueMaps() {
   114  		t.Run(expected.JSONString(), func(t *testing.T) {
   115  			bytes, err := expected.MarshalText()
   116  			require.NoError(t, err)
   117  			assert.Equal(t, expected.JSONString(), string(bytes))
   118  		})
   119  	}
   120  }
   121  
   122  func TestValueMapUnmarshalTextIsSameAsJSONUnmarshal(t *testing.T) {
   123  	for _, expected := range makeMarshallableValueMaps() {
   124  		t.Run(expected.JSONString(), func(t *testing.T) {
   125  			var actual ValueMap
   126  			require.NoError(t, actual.UnmarshalText([]byte(expected.JSONString())))
   127  			assert.Equal(t, expected, actual)
   128  		})
   129  	}
   130  }
   131  

View as plain text