string_test.go raw

   1  // Copyright (c) 2016-2020 Uber Technologies, Inc.
   2  //
   3  // Permission is hereby granted, free of charge, to any person obtaining a copy
   4  // of this software and associated documentation files (the "Software"), to deal
   5  // in the Software without restriction, including without limitation the rights
   6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   7  // copies of the Software, and to permit persons to whom the Software is
   8  // furnished to do so, subject to the following conditions:
   9  //
  10  // The above copyright notice and this permission notice shall be included in
  11  // all copies or substantial portions of the Software.
  12  //
  13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19  // THE SOFTWARE.
  20  
  21  package atomic
  22  
  23  import (
  24  	"encoding/json"
  25  	"encoding/xml"
  26  	"testing"
  27  
  28  	"github.com/stretchr/testify/assert"
  29  	"github.com/stretchr/testify/require"
  30  )
  31  
  32  func TestStringNoInitialValue(t *testing.T) {
  33  	atom := &String{}
  34  	require.Equal(t, "", atom.Load(), "Initial value should be blank string")
  35  }
  36  
  37  func TestString(t *testing.T) {
  38  	atom := NewString("")
  39  	require.Equal(t, "", atom.Load(), "Expected Load to return initialized value")
  40  
  41  	atom.Store("abc")
  42  	require.Equal(t, "abc", atom.Load(), "Unexpected value after Store")
  43  
  44  	atom = NewString("bcd")
  45  	require.Equal(t, "bcd", atom.Load(), "Expected Load to return initialized value")
  46  
  47  	t.Run("JSON/Marshal", func(t *testing.T) {
  48  		bytes, err := json.Marshal(atom)
  49  		require.NoError(t, err, "json.Marshal errored unexpectedly.")
  50  		require.Equal(t, []byte(`"bcd"`), bytes, "json.Marshal encoded the wrong bytes.")
  51  	})
  52  
  53  	t.Run("JSON/Unmarshal", func(t *testing.T) {
  54  		err := json.Unmarshal([]byte(`"abc"`), &atom)
  55  		require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
  56  		require.Equal(t, "abc", atom.Load(), "json.Unmarshal didn't set the correct value.")
  57  	})
  58  
  59  	t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
  60  		err := json.Unmarshal([]byte("42"), &atom)
  61  		require.Error(t, err, "json.Unmarshal didn't error as expected.")
  62  		assertErrorJSONUnmarshalType(t, err,
  63  			"json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
  64  	})
  65  
  66  	atom = NewString("foo")
  67  
  68  	t.Run("XML/Marshal", func(t *testing.T) {
  69  		bytes, err := xml.Marshal(atom)
  70  		require.NoError(t, err, "xml.Marshal errored unexpectedly.")
  71  		require.Equal(t, []byte("<String>foo</String>"), bytes,
  72  			"xml.Marshal encoded the wrong bytes.")
  73  	})
  74  
  75  	t.Run("XML/Unmarshal", func(t *testing.T) {
  76  		err := xml.Unmarshal([]byte("<String>bar</String>"), &atom)
  77  		require.NoError(t, err, "xml.Unmarshal errored unexpectedly.")
  78  		require.Equal(t, "bar", atom.Load(), "xml.Unmarshal didn't set the correct value.")
  79  	})
  80  
  81  	t.Run("String", func(t *testing.T) {
  82  		atom := NewString("foo")
  83  		assert.Equal(t, "foo", atom.String(),
  84  			"String() returned an unexpected value.")
  85  	})
  86  
  87  	t.Run("CompareAndSwap", func(t *testing.T) {
  88  		atom := NewString("foo")
  89  
  90  		swapped := atom.CompareAndSwap("bar", "bar")
  91  		require.False(t, swapped, "swapped isn't false")
  92  		require.Equal(t, atom.Load(), "foo", "Load returned wrong value")
  93  
  94  		swapped = atom.CompareAndSwap("foo", "bar")
  95  		require.True(t, swapped, "swapped isn't true")
  96  		require.Equal(t, atom.Load(), "bar", "Load returned wrong value")
  97  	})
  98  
  99  	t.Run("Swap", func(t *testing.T) {
 100  		atom := NewString("foo")
 101  
 102  		old := atom.Swap("bar")
 103  		require.Equal(t, old, "foo", "Swap returned wrong value")
 104  		require.Equal(t, atom.Load(), "bar", "Load returned wrong value")
 105  	})
 106  }
 107  
 108  func TestString_InitializeDefault(t *testing.T) {
 109  	tests := []struct {
 110  		msg    string
 111  		newStr func() *String
 112  	}{
 113  		{
 114  			msg: "Uninitialized",
 115  			newStr: func() *String {
 116  				var s String
 117  				return &s
 118  			},
 119  		},
 120  		{
 121  			msg: "NewString with default",
 122  			newStr: func() *String {
 123  				return NewString("")
 124  			},
 125  		},
 126  		{
 127  			msg: "String swapped with default",
 128  			newStr: func() *String {
 129  				s := NewString("initial")
 130  				s.Swap("")
 131  				return s
 132  			},
 133  		},
 134  		{
 135  			msg: "String CAS'd with default",
 136  			newStr: func() *String {
 137  				s := NewString("initial")
 138  				s.CompareAndSwap("initial", "")
 139  				return s
 140  			},
 141  		},
 142  	}
 143  
 144  	for _, tt := range tests {
 145  		t.Run(tt.msg, func(t *testing.T) {
 146  			t.Run("MarshalText", func(t *testing.T) {
 147  				str := tt.newStr()
 148  				text, err := str.MarshalText()
 149  				require.NoError(t, err)
 150  				assert.Equal(t, "", string(text), "")
 151  			})
 152  
 153  			t.Run("String", func(t *testing.T) {
 154  				str := tt.newStr()
 155  				assert.Equal(t, "", str.String())
 156  			})
 157  
 158  			t.Run("CompareAndSwap", func(t *testing.T) {
 159  				str := tt.newStr()
 160  				require.True(t, str.CompareAndSwap("", "new"))
 161  				assert.Equal(t, "new", str.Load())
 162  			})
 163  
 164  			t.Run("Swap", func(t *testing.T) {
 165  				str := tt.newStr()
 166  				assert.Equal(t, "", str.Swap("new"))
 167  			})
 168  		})
 169  	}
 170  }
 171