Skip to content

Commit

Permalink
Unit tests (#10)
Browse files Browse the repository at this point in the history
Add a couple unit tests for core functionality
  • Loading branch information
mwielbut authored and markphelps committed Dec 10, 2018
1 parent e0a8e47 commit f71faa1
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 0 deletions.
143 changes: 143 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package optional

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestInt_Get_Present(t *testing.T) {
o := NewInt(42)

v, err := o.Get()
assert.True(t, o.Present())
assert.NoError(t, err)
assert.Equal(t, 42, v)
}

func TestInt_Get_NotPresent(t *testing.T) {
o := Int{}
var zero int

v, err := o.Get()
assert.False(t, o.Present())
assert.Error(t, err)
assert.Equal(t, zero, v)
}

func TestInt_OrElse_Present(t *testing.T) {
o := NewInt(42)

v := o.OrElse(99)
assert.True(t, o.Present())
assert.Equal(t, 42, v)
}

func TestInt_OrElse_NotPresent(t *testing.T) {
o := Int{}

v := o.OrElse(99)
assert.False(t, o.Present())
assert.Equal(t, 99, v)
}

func TestInt_If_Present(t *testing.T) {
o := NewInt(42)

canary := false
o.If(func(v int) {
canary = true
})
assert.True(t, o.Present())
assert.True(t, canary)
}

func TestInt_If_NotPresent(t *testing.T) {
o := Int{}

canary := false
o.If(func(v int) {
canary = true
})
assert.False(t, o.Present())
assert.False(t, canary)
}

func TestInt_MarshalJSON(t *testing.T) {
type fields struct {
WithValue Int
WithZeroValue Int
WithNoValue Int
Unused Int
}

var instance = fields{
WithValue: NewInt(42),
WithZeroValue: NewInt(0),
WithNoValue: Int{},
}

out, err := json.Marshal(instance)
assert.NoError(t, err)
assert.Equal(t, `{"WithValue":42,"WithZeroValue":0,"WithNoValue":null,"Unused":null}`, string(out))
}

func TestInt_UnmarshalJSON(t *testing.T) {
type fields struct {
WithValue Int
WithZeroValue Int
WithNoValue Int
Unused Int
}

var jsonString = `{"WithValue":42,"WithZeroValue":0,"WithNoValue":null}`
instance := fields{}

err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)

assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, 42, *instance.WithValue.value)

assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, 0, *instance.WithZeroValue.value)

assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)

assert.False(t, instance.Unused.Present())
assert.Nil(t, instance.Unused.value)
}

func TestInt_UnmarshalJSON_Overwritten(t *testing.T) {
type fields struct {
WithValue Int
WithZeroValue Int
WithNoValue Int
Unused Int
}

var jsonString = `{"WithValue":42,"WithZeroValue":0,"WithNoValue":null}`
instance := fields{
WithValue: NewInt(1),
WithZeroValue: NewInt(2),
WithNoValue: NewInt(3),
Unused: NewInt(4),
}

err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)

assert.True(t, instance.WithValue.Present())
assert.Equal(t, 42, *instance.WithValue.value)

assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, 0, *instance.WithZeroValue.value)

assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)

assert.True(t, instance.Unused.Present())
assert.Equal(t, 4, *instance.Unused.value)
}
143 changes: 143 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package optional

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestString_Get_Present(t *testing.T) {
o := NewString("foo")

v, err := o.Get()
assert.True(t, o.Present())
assert.NoError(t, err)
assert.Equal(t, "foo", v)
}

func TestString_Get_NotPresent(t *testing.T) {
o := String{}
var zero string

v, err := o.Get()
assert.False(t, o.Present())
assert.Error(t, err)
assert.Equal(t, zero, v)
}

func TestString_OrElse_Present(t *testing.T) {
o := NewString("foo")

v := o.OrElse("bar")
assert.True(t, o.Present())
assert.Equal(t, "foo", v)
}

func TestString_OrElse_NotPresent(t *testing.T) {
o := String{}

v := o.OrElse("bar")
assert.False(t, o.Present())
assert.Equal(t, "bar", v)
}

func TestString_If_Present(t *testing.T) {
o := NewString("foo")

canary := false
o.If(func(v string) {
canary = true
})
assert.True(t, o.Present())
assert.True(t, canary)
}

func TestString_If_NotPresent(t *testing.T) {
o := String{}

canary := false
o.If(func(v string) {
canary = true
})
assert.False(t, o.Present())
assert.False(t, canary)
}

func TestString_MarshalJSON(t *testing.T) {
type fields struct {
WithValue String
WithZeroValue String
WithNoValue String
Unused String
}

var instance = fields{
WithValue: NewString("foo"),
WithZeroValue: NewString(""),
WithNoValue: String{},
}

out, err := json.Marshal(instance)
assert.NoError(t, err)
assert.Equal(t, `{"WithValue":"foo","WithZeroValue":"","WithNoValue":null,"Unused":null}`, string(out))
}

func TestString_UnmarshalJSON(t *testing.T) {
type fields struct {
WithValue String
WithZeroValue String
WithNoValue String
Unused String
}

var jsonString = `{"WithValue":"foo","WithZeroValue":"","WithNoValue":null}`
instance := fields{}

err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)

assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, "foo", *instance.WithValue.value)

assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, "", *instance.WithZeroValue.value)

assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)

assert.False(t, instance.Unused.Present())
assert.Nil(t, instance.Unused.value)
}

func TestString_UnmarshalJSON_Overwritten(t *testing.T) {
type fields struct {
WithValue String
WithZeroValue String
WithNoValue String
Unused String
}

var jsonString = `{"WithValue":"foo","WithZeroValue":"","WithNoValue":null}`
instance := fields{
WithValue: NewString("seed_a"),
WithZeroValue: NewString("seed_b"),
WithNoValue: NewString("seed_c"),
Unused: NewString("seed_d"),
}

err := json.Unmarshal([]byte(jsonString), &instance)
assert.NoError(t, err)

assert.True(t, instance.WithValue.Present())
assert.Equal(t, "foo", *instance.WithValue.value)

assert.True(t, instance.WithZeroValue.Present())
assert.Equal(t, "", *instance.WithZeroValue.value)

assert.False(t, instance.WithNoValue.Present())
assert.Nil(t, instance.WithNoValue.value)

assert.True(t, instance.Unused.Present())
assert.Equal(t, "seed_d", *instance.Unused.value)
}

0 comments on commit f71faa1

Please sign in to comment.