Skip to content

Commit 1c73727

Browse files
committed
add IsType.
1 parent b571152 commit 1c73727

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

DESIGN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,14 @@ This is a complete list of expectations, "+" means already supported:
320320
* +Panic: panic and panic with an object
321321
* +Equal: deep equal
322322
* Is: shallow equal
323+
* +IsType: if a interface is of a type
323324
* Order comparision: >, <, >=, <=, Within(delta).Of(value)
324325
* Composition: Not, And, Or
325326
* True/False: can be supported by Is(true), Is(false)
326327
* Nil: can be supported by Is(nil)
327328
* Empty: empty for container types
328329
* Error: can be supported by Equal
329330
* Implements: can be supported by IsType
330-
* IsType: if a interface is of a type
331331
* String
332332
- +HasPrefix
333333
- +HasSuffix

expectation/checker.go

+12
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func Panic(actual, expected interface{}, skip int) (ret error) {
4949
return nil
5050
}
5151

52+
func IsType(actual, expected interface{}, skip int) error {
53+
if reflect.TypeOf(actual) != reflect.TypeOf(expected) {
54+
return errors.Compare(actual, expected, "to have type of", skip+1)
55+
}
56+
return nil
57+
}
58+
5259
// Equal is the fluent method for checker Equal.
5360
func (a *Actual) Equal(expected interface{}) {
5461
a.to(Equal, expected, 1)
@@ -63,3 +70,8 @@ func (a *Actual) NotEqual(expected interface{}) {
6370
func (a *Actual) Panic() {
6471
a.to(Panic, nil, 1)
6572
}
73+
74+
// IsType is the fluent method for checker IsType.
75+
func (a *Actual) IsType(expected interface{}) {
76+
a.to(IsType, expected, 1)
77+
}

expectation/checker_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ var panicTestCases = []expectTestCase{
5353
func TestPanic(t *testing.T) {
5454
testExpectations(t, panicTestCases)
5555
}
56+
57+
var isTypeTestCases = []expectTestCase{
58+
{`expect(int(0)).IsType(int(1))`, func(expect ExpectFunc) { expect(int(0)).IsType(int(1)) }, true},
59+
{`expect(int(0)).IsType(bool(true))`, func(expect ExpectFunc) { expect(int(0)).IsType(bool(true)) }, false},
60+
}
61+
62+
func TestIsType(t *testing.T) {
63+
testExpectations(t, isTypeTestCases)
64+
}

0 commit comments

Comments
 (0)