|
| 1 | +package option_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + "github.com/vmihailenco/msgpack/v5" |
| 10 | + |
| 11 | + "github.com/tarantool/go-option" |
| 12 | +) |
| 13 | + |
| 14 | +func TestOptionalFloat32(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + var buf bytes.Buffer |
| 18 | + |
| 19 | + enc := msgpack.NewEncoder(&buf) |
| 20 | + dec := msgpack.NewDecoder(&buf) |
| 21 | + |
| 22 | + someFloat32 := option.SomeFloat32(3.11) |
| 23 | + assert.True(t, someFloat32.IsSome(), "Expected IsPresent() to return true") |
| 24 | + require.NoError(t, someFloat32.EncodeMsgpack(enc)) |
| 25 | + |
| 26 | + var shouldBeSomeFloat32 option.Float32 |
| 27 | + require.NoError(t, shouldBeSomeFloat32.DecodeMsgpack(dec)) |
| 28 | + assert.True(t, shouldBeSomeFloat32.IsSome(), "Expected IsPresent() to return true") |
| 29 | + assert.InEpsilon(t, someFloat32.Unwrap(), shouldBeSomeFloat32.Unwrap(), 1e-6, |
| 30 | + "Expected Value() to return the same value") |
| 31 | + |
| 32 | + emptyFloat32 := option.NoneFloat32() |
| 33 | + assert.False(t, emptyFloat32.IsSome(), "Expected IsPresent() to return false") |
| 34 | + require.NoError(t, emptyFloat32.EncodeMsgpack(enc)) |
| 35 | + |
| 36 | + var shouldBeEmptyFloat32 option.Float32 |
| 37 | + require.NoError(t, shouldBeEmptyFloat32.DecodeMsgpack(dec)) |
| 38 | + assert.False(t, shouldBeEmptyFloat32.IsSome(), "Expected IsPresent() to return false") |
| 39 | +} |
| 40 | + |
| 41 | +func TestOptionalFloat64(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + var buf bytes.Buffer |
| 45 | + |
| 46 | + enc := msgpack.NewEncoder(&buf) |
| 47 | + dec := msgpack.NewDecoder(&buf) |
| 48 | + |
| 49 | + someFloat64 := option.SomeFloat64(3.11) |
| 50 | + assert.True(t, someFloat64.IsSome(), "Expected IsPresent() to return true") |
| 51 | + require.NoError(t, someFloat64.EncodeMsgpack(enc)) |
| 52 | + |
| 53 | + var shouldBeSomeFloat64 option.Float64 |
| 54 | + require.NoError(t, shouldBeSomeFloat64.DecodeMsgpack(dec)) |
| 55 | + assert.True(t, shouldBeSomeFloat64.IsSome(), "Expected IsPresent() to return true") |
| 56 | + assert.InEpsilon(t, someFloat64.Unwrap(), shouldBeSomeFloat64.Unwrap(), 1e-6, |
| 57 | + "Expected Value() to return the same value") |
| 58 | + |
| 59 | + emptyFloat64 := option.NoneFloat64() |
| 60 | + assert.False(t, emptyFloat64.IsSome(), "Expected IsPresent() to return false") |
| 61 | + require.NoError(t, emptyFloat64.EncodeMsgpack(enc)) |
| 62 | + |
| 63 | + var shouldBeEmptyFloat64 option.Float64 |
| 64 | + require.NoError(t, shouldBeEmptyFloat64.DecodeMsgpack(dec)) |
| 65 | + assert.False(t, shouldBeEmptyFloat64.IsSome(), "Expected IsPresent() to return false") |
| 66 | +} |
0 commit comments