Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions tpp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ type mockImpl struct {
mock.Mock
}

func (m *mockImpl) DoSomethingMockCall(x interface{}) *mock.Call {
return m.On("DoSomething", x)
}

func (m *mockImpl) DoSomething(x int) bool {
args := m.Called(x)
return args.Bool(0)
}

func (m *mockImpl) DoSomethingVariadicMockCall(x interface{}, ys ...interface{}) *mock.Call {
return m.On("DoSomethingVariadic", x, ys)
}

func (m *mockImpl) DoSomethingVariadic(x int, ys ...int) bool {
args := m.Called(x, ys)
return args.Bool(0)
}

func TestUnexpected(t *testing.T) {
is := require.New(t)
mockObj := new(mockImpl)
Expand Down Expand Up @@ -64,3 +77,101 @@ func TestUnexpected(t *testing.T) {
is.Empty(mockObj.ExpectedCalls)
})
}

func TestExpects(t *testing.T) {
is := require.New(t)

t.Run("Expectorise works on non-variadic function", func(t *testing.T){
mockObj := new(mockImpl)
x := 3

expects := OKs([]Call{
{
Given: []any{int(x)},
Return: []any{false},
}})

expects.Expectorise(t, mockObj.DoSomethingMockCall, []any{false})

res := mockObj.DoSomething(x)

mockObj.AssertExpectations(t)
is.False(res)
})

t.Run("Expectorise works on variadic function when no variadic args", func(t *testing.T) {
mockObj := new(mockImpl)
x := 3

expects := OKs([]Call{
{
Given: []any{int(x)},
Return: []any{false},
}})

expects.Expectorise(t, mockObj.DoSomethingVariadicMockCall, []any{false})

res := mockObj.DoSomethingVariadic(x)

mockObj.AssertExpectations(t)
is.False(res)
})

t.Run("Expectorise works on variadic function when 1 variadic arg", func(t *testing.T) {
mockObj := new(mockImpl)
x := 3
y := 1

expects := OKs([]Call{
{
Given: []any{int(x), int(y)},
Return: []any{false},
}})

expects.Expectorise(t, mockObj.DoSomethingVariadicMockCall, []any{false})

res := mockObj.DoSomethingVariadic(x, y)

mockObj.AssertExpectations(t)
is.False(res)
})

t.Run("Expectorise works on variadic function when >1 variadic args", func(t *testing.T) {
mockObj := new(mockImpl)
x := 3
y1 := 1
y2 := 2

expects := OKs([]Call{
{
Given: []any{int(x), int(y1), int(y2)},
Return: []any{false},
}})

expects.Expectorise(t, mockObj.DoSomethingVariadicMockCall, []any{false})

res := mockObj.DoSomethingVariadic(x, y1, y2)

mockObj.AssertExpectations(t)
is.False(res)
})

t.Run("Expectorise works on variadic function when variadic args provided as array", func(t *testing.T) {
mockObj := new(mockImpl)
x := 3
ys := []int{1,2}

expects := OKs([]Call{
{
Given: []any{int(x), ys},
Return: []any{false},
}})

expects.Expectorise(t, mockObj.DoSomethingVariadicMockCall, []any{false})

res := mockObj.DoSomethingVariadic(x, ys...)

mockObj.AssertExpectations(t)
is.False(res)
})
}