Skip to content

Commit

Permalink
Merge pull request #9 from ppapapetrou76/add-hastypeof-assertion
Browse files Browse the repository at this point in the history
add HasTypeOf assertion
  • Loading branch information
ppapapetrou76 authored Nov 21, 2020
2 parents 903ee2d + 205f1b9 commit 32579b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions assert/any.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assert

import (
"reflect"
"testing"

"github.com/ppapapetrou76/go-testing/internal/pkg/values"
Expand Down Expand Up @@ -65,3 +66,11 @@ func (a AssertableAny) IsFalse() AssertableAny {
a.IsEqualTo(false)
return a
}

// HasTypeOf asserts if the expected value has the type of a given value
func (a AssertableAny) HasTypeOf(t reflect.Type) AssertableAny {
if !a.actual.HasTypeOf(t) {
a.t.Error(shouldHaveType(a.actual, t))
}
return a
}
31 changes: 30 additions & 1 deletion assert/any_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package assert

import "testing"
import (
"reflect"
"testing"
)

func TestAssertable_IsNil(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -105,3 +108,29 @@ func TestAssertable_IsFalse(t *testing.T) {
})
}
}

func TestAssertableAny_HasTypeOf(t *testing.T) {
tests := []struct {
name string
actual interface{}
shouldFail bool
}{
{
name: "should assert the same types",
actual: "123",
shouldFail: false,
},
{
name: "should assert different types",
actual: true,
shouldFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
That(test, tt.actual).HasTypeOf(reflect.TypeOf(""))
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
}
4 changes: 4 additions & 0 deletions assert/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func shouldHaveSameSizeAs(actual types.Assertable, substr string) string {
return fmt.Sprintf("assertion failed: expected size of [%v] should be same as the size of [%+v], but it isn't", actual.Value(), substr)
}

func shouldHaveType(actual types.Assertable, value interface{}) string {
return fmt.Sprintf("assertion failed: expected value of = %+v, to have type of %T but it hasn't", actual.Value(), value)
}

func shouldBeShorter(actual types.Assertable, expected interface{}) string {
return fmt.Sprintf("assertion failed: expected value of = %+v, to be greater than %+v", actual.Value(), expected)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/pkg/values/any_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (s AnyValue) IsNotNil() bool {
return !s.IsNil()
}

// HasTypeOf returns true if the value is of the given type else false
func (s AnyValue) HasTypeOf(t reflect.Type) bool {
return reflect.TypeOf(s.value) == t
}

// NewAnyValue creates and returns an AnyValue struct initialed with the given value
func NewAnyValue(value interface{}) AnyValue {
switch v := value.(type) {
Expand Down

0 comments on commit 32579b0

Please sign in to comment.