...

Source file src/go.uber.org/atomic/string_test.go

Documentation: go.uber.org/atomic

     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, "xml.Marshal encoded the wrong bytes.")
    72  	})
    73  
    74  	t.Run("XML/Unmarshal", func(t *testing.T) {
    75  		err := xml.Unmarshal([]byte("<String>bar</String>"), &atom)
    76  		require.NoError(t, err, "xml.Unmarshal errored unexpectedly.")
    77  		require.Equal(t, "bar", atom.Load(), "xml.Unmarshal didn't set the correct value.")
    78  	})
    79  
    80  	t.Run("String", func(t *testing.T) {
    81  		atom := NewString("foo")
    82  		assert.Equal(t, "foo", atom.String(),
    83  			"String() returned an unexpected value.")
    84  	})
    85  
    86  	t.Run("CompareAndSwap", func(t *testing.T) {
    87  		atom := NewString("foo")
    88  
    89  		swapped := atom.CompareAndSwap("bar", "bar")
    90  		require.False(t, swapped, "swapped isn't false")
    91  		require.Equal(t, atom.Load(), "foo", "Load returned wrong value")
    92  
    93  		swapped = atom.CompareAndSwap("foo", "bar")
    94  		require.True(t, swapped, "swapped isn't true")
    95  		require.Equal(t, atom.Load(), "bar", "Load returned wrong value")
    96  	})
    97  
    98  	t.Run("Swap", func(t *testing.T) {
    99  		atom := NewString("foo")
   100  
   101  		old := atom.Swap("bar")
   102  		require.Equal(t, old, "foo", "Swap returned wrong value")
   103  		require.Equal(t, atom.Load(), "bar", "Load returned wrong value")
   104  	})
   105  }
   106  
   107  func TestString_InitializeDefault(t *testing.T) {
   108  	tests := []struct {
   109  		msg    string
   110  		newStr func() *String
   111  	}{
   112  		{
   113  			msg: "Uninitialized",
   114  			newStr: func() *String {
   115  				var s String
   116  				return &s
   117  			},
   118  		},
   119  		{
   120  			msg: "NewString with default",
   121  			newStr: func() *String {
   122  				return NewString("")
   123  			},
   124  		},
   125  		{
   126  			msg: "String swapped with default",
   127  			newStr: func() *String {
   128  				s := NewString("initial")
   129  				s.Swap("")
   130  				return s
   131  			},
   132  		},
   133  		{
   134  			msg: "String CAS'd with default",
   135  			newStr: func() *String {
   136  				s := NewString("initial")
   137  				s.CompareAndSwap("initial", "")
   138  				return s
   139  			},
   140  		},
   141  	}
   142  
   143  	for _, tt := range tests {
   144  		t.Run(tt.msg, func(t *testing.T) {
   145  			t.Run("MarshalText", func(t *testing.T) {
   146  				str := tt.newStr()
   147  				text, err := str.MarshalText()
   148  				require.NoError(t, err)
   149  				assert.Equal(t, "", string(text), "")
   150  			})
   151  
   152  			t.Run("String", func(t *testing.T) {
   153  				str := tt.newStr()
   154  				assert.Equal(t, "", str.String())
   155  			})
   156  
   157  			t.Run("CompareAndSwap", func(t *testing.T) {
   158  				str := tt.newStr()
   159  				require.True(t, str.CompareAndSwap("", "new"))
   160  				assert.Equal(t, "new", str.Load())
   161  			})
   162  
   163  			t.Run("Swap", func(t *testing.T) {
   164  				str := tt.newStr()
   165  				assert.Equal(t, "", str.Swap("new"))
   166  			})
   167  		})
   168  	}
   169  }
   170  

View as plain text