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 21package atomic 22 23import ( 24 "encoding/json" 25 "encoding/xml" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30) 31 32func TestStringNoInitialValue(t *testing.T) { 33 atom := &String{} 34 require.Equal(t, "", atom.Load(), "Initial value should be blank string") 35} 36 37func 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