Implement fmt.Stringer for atomic types (#76)

Add safe `String()` methods for atomic types that replicate the same
behavior as `fmt.Sprintf("%v", x.Load())` without the allocations.

As with json.Marshaler/Unmarshaler, we've omitted the `atomic.Value`
type for now.

Resolves #50
This commit is contained in:
Abhinav Gupta
2020-05-14 16:26:08 -07:00
committed by GitHub
parent 982a2dde23
commit ddc304d531
13 changed files with 143 additions and 0 deletions

View File

@@ -22,8 +22,10 @@ package atomic
import (
"encoding/json"
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -63,4 +65,12 @@ func TestUint32(t *testing.T) {
assertErrorJSONUnmarshalType(t, err,
"json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
})
t.Run("String", func(t *testing.T) {
// Use an integer with the signed bit set. If we're converting
// incorrectly, we'll get a negative value here.
atom := NewUint32(math.MaxUint32)
assert.Equal(t, "4294967295", atom.String(),
"String() returned an unexpected value.")
})
}