Skip to content

Commit 3d87720

Browse files
authored
feat(bool): add in support for bools (#2)
Signed-off-by: Jason Field <jason@avon-lea.co.uk>
1 parent 0727e0f commit 3d87720

File tree

5 files changed

+122
-31
lines changed

5 files changed

+122
-31
lines changed

bool.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package pointerhelpers
2+
3+
// BoolHelper contains all Bool related
4+
// pointer helpers
5+
type BoolHelper struct {
6+
}
7+
8+
// Bool returns a pointer to the Bool value passed in.
9+
func Bool(v bool) *bool {
10+
return &v
11+
}
12+
13+
// Bool returns a pointer to the bool value passed in.
14+
func (b *BoolHelper) Bool(v bool) *bool {
15+
return Bool(v)
16+
}
17+
18+
// BoolValue returns the value of the bool pointer passed in or
19+
// 0 if the pointer is nil.
20+
func BoolValue(v *bool) bool {
21+
if v != nil {
22+
return *v
23+
}
24+
return false
25+
}
26+
27+
// BoolValue returns the value of the bool pointer passed in or
28+
// 0 if the pointer is nil.
29+
func (b *BoolHelper) BoolValue(v *bool) bool {
30+
return BoolValue(v)
31+
}
32+
33+
// BoolIsUnset returns true if the pointer is nil
34+
func BoolIsUnset(v *bool) bool {
35+
return v == nil
36+
}
37+
38+
// BoolIsUnset returns true if the pointer is nil
39+
func (b *BoolHelper) BoolIsUnset(v *bool) bool {
40+
return BoolIsUnset(v)
41+
}

bool_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package pointerhelpers
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestBool(t *testing.T) {
9+
h := BoolHelper{}
10+
want := true
11+
got := h.Bool(want)
12+
assert.True(t, *got)
13+
}
14+
15+
func TestBoolValue(t *testing.T) {
16+
t.Run("when nil returns false", func(t *testing.T) {
17+
h := BoolHelper{}
18+
got := h.BoolValue(nil)
19+
assert.False(t, got)
20+
})
21+
t.Run("When true, returns true", func(t *testing.T) {
22+
h := BoolHelper{}
23+
got := h.BoolValue(Bool(true))
24+
assert.True(t, got)
25+
})
26+
t.Run("When false, returns false", func(t *testing.T) {
27+
h := BoolHelper{}
28+
got := h.BoolValue(Bool(false))
29+
assert.False(t, got)
30+
})
31+
}
32+
33+
func TestBoolIsUnset(t *testing.T) {
34+
t.Run("when nil returns true", func(t *testing.T) {
35+
h := BoolHelper{}
36+
got := h.BoolIsUnset(nil)
37+
assert.True(t, got)
38+
})
39+
t.Run("When true, returns false", func(t *testing.T) {
40+
h := BoolHelper{}
41+
got := h.BoolIsUnset(Bool(true))
42+
assert.False(t, got)
43+
})
44+
t.Run("When false, returns false", func(t *testing.T) {
45+
h := BoolHelper{}
46+
got := h.BoolIsUnset(Bool(false))
47+
assert.False(t, got)
48+
})
49+
}

int64_test.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package pointerhelpers
22

33
import (
4-
"github.com/stretchr/testify/assert"
5-
"testing"
4+
"github.com/stretchr/testify/assert"
5+
"testing"
66
)
77

8-
func TestInt(t *testing.T) {
9-
h := StringHelper{}
10-
want := "foo"
11-
got := h.String(want)
8+
func TestInt64(t *testing.T) {
9+
h := Int64Helper{}
10+
want := int64(42)
11+
got := h.Int64(want)
1212
assert.EqualValues(t, want, *got)
1313
}
1414

15-
func TestStringValue(t *testing.T) {
16-
t.Run("when nil returns empty string", func(t *testing.T) {
17-
h := StringHelper{}
18-
want := ""
19-
got := h.StringValue(nil)
20-
assert.Equal(t, want, got)
15+
func TestInt64Value(t *testing.T) {
16+
t.Run("when nil returns 0", func(t *testing.T) {
17+
h := Int64Helper{}
18+
got := h.Int64Value(nil)
19+
assert.Equal(t, int64(0), got)
2120
})
22-
t.Run("when set returns the given string", func(t *testing.T) {
23-
h := StringHelper{}
24-
want := "foobar"
25-
got := h.StringValue(String(want))
26-
assert.Equal(t, want, got)
21+
t.Run("when set returns the given int64", func(t *testing.T) {
22+
h := Int64Helper{}
23+
want := h.Int64(int64(42))
24+
got := h.Int64Value(want)
25+
assert.Equal(t, *want, got)
2726
})
2827
}

string_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ import (
55
"testing"
66
)
77

8-
func TestInt64(t *testing.T) {
9-
h := Int64Helper{}
10-
want := int64(42)
11-
got := h.Int64(want)
8+
func TestInt(t *testing.T) {
9+
h := StringHelper{}
10+
want := "foo"
11+
got := h.String(want)
1212
assert.EqualValues(t, want, *got)
1313
}
1414

15-
func TestInt64Value(t *testing.T) {
16-
t.Run("when nil returns 0", func(t *testing.T) {
17-
h := Int64Helper{}
18-
got := h.Int64Value(nil)
19-
assert.Equal(t, int64(0), got)
15+
func TestStringValue(t *testing.T) {
16+
t.Run("when nil returns empty string", func(t *testing.T) {
17+
h := StringHelper{}
18+
want := ""
19+
got := h.StringValue(nil)
20+
assert.Equal(t, want, got)
2021
})
21-
t.Run("when set returns the given int64", func(t *testing.T) {
22-
h := Int64Helper{}
23-
want := h.Int64(int64(42))
24-
got := h.Int64Value(want)
25-
assert.Equal(t, *want, got)
22+
t.Run("when set returns the given string", func(t *testing.T) {
23+
h := StringHelper{}
24+
want := "foobar"
25+
got := h.StringValue(String(want))
26+
assert.Equal(t, want, got)
2627
})
2728
}

types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ package pointerhelpers
55
type PointerHelper struct {
66
StringHelper
77
Int64Helper
8+
BoolHelper
89
}

0 commit comments

Comments
 (0)