From c87253e84f78ff8d2d1471060ce19d9bfb059de1 Mon Sep 17 00:00:00 2001 From: Patroklos Papapetrou Date: Sat, 21 Nov 2020 18:42:39 +0200 Subject: [PATCH 1/2] add HasTypeOf assertion --- assert/any.go | 9 +++++++++ assert/any_test.go | 31 ++++++++++++++++++++++++++++++- assert/error.go | 4 ++++ internal/pkg/values/any_value.go | 5 +++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/assert/any.go b/assert/any.go index ce26d0e..adb8ea8 100644 --- a/assert/any.go +++ b/assert/any.go @@ -1,6 +1,7 @@ package assert import ( + "reflect" "testing" "github.com/ppapapetrou76/go-testing/internal/pkg/values" @@ -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 +} diff --git a/assert/any_test.go b/assert/any_test.go index f1d1aa1..418aa8b 100644 --- a/assert/any_test.go +++ b/assert/any_test.go @@ -1,6 +1,9 @@ package assert -import "testing" +import ( + "reflect" + "testing" +) func TestAssertable_IsNil(t *testing.T) { tests := []struct { @@ -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) + }) + } +} diff --git a/assert/error.go b/assert/error.go index 7397ca2..52a125b 100644 --- a/assert/error.go +++ b/assert/error.go @@ -102,3 +102,7 @@ func shouldEndWith(actual types.Assertable, substr string) string { 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) +} diff --git a/internal/pkg/values/any_value.go b/internal/pkg/values/any_value.go index e86d826..e494bcb 100644 --- a/internal/pkg/values/any_value.go +++ b/internal/pkg/values/any_value.go @@ -71,6 +71,11 @@ func (s AnyValue) IsNotNil() bool { return !s.IsNil() } +// IsTypeOf 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) { From 0ceca4a3d7de7f377ba79671fc5a2c7ad83f85c8 Mon Sep 17 00:00:00 2001 From: Patroklos Papapetrou Date: Sat, 21 Nov 2020 18:48:00 +0200 Subject: [PATCH 2/2] make linter happy --- internal/pkg/values/any_value.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/values/any_value.go b/internal/pkg/values/any_value.go index e494bcb..c1b1e50 100644 --- a/internal/pkg/values/any_value.go +++ b/internal/pkg/values/any_value.go @@ -71,7 +71,7 @@ func (s AnyValue) IsNotNil() bool { return !s.IsNil() } -// IsTypeOf returns true if the value is of the given type else false +// 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 }