-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
171 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package apitest | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type Test struct { | ||
expectedOutputs []any | ||
goTest *testing.T | ||
inputs []any | ||
m *mock.Mock | ||
mockCalls []*mock.Call | ||
name string | ||
} | ||
|
||
func NewTest(name string) *Test { | ||
test := Test{ | ||
name: name, | ||
} | ||
return &test | ||
} | ||
|
||
func (test Test) AssertExpectations(outputs ...any) { | ||
test.m.AssertExpectations(test.goTest) | ||
|
||
for i, expectedValue := range test.expectedOutputs { | ||
assert.Equal(test.goTest, expectedValue, outputs[i], fmt.Sprintf("wrong value in output %d", i)) | ||
} | ||
} | ||
|
||
func (test *Test) ExpectOutputs(outputs ...any) *Test { | ||
test.expectedOutputs = outputs | ||
return test | ||
} | ||
|
||
func (test Test) GetInput(index int) any { | ||
return test.inputs[index] | ||
} | ||
|
||
func (test Test) GetStringPointerInput(index int) *string { | ||
return test.GetInput(index).(*string) | ||
} | ||
|
||
func (test Test) GetMock() *mock.Mock { | ||
return test.m | ||
} | ||
|
||
func (test Test) Run(goTest *testing.T, runner func(Test)) { | ||
test.m = &mock.Mock{} | ||
for _, call := range test.mockCalls { | ||
call.Parent = test.m | ||
} | ||
test.m.ExpectedCalls = test.mockCalls | ||
|
||
goTest.Run(test.name, func(goTest *testing.T) { | ||
test.goTest = goTest | ||
runner(test) | ||
}) | ||
} | ||
|
||
func (test *Test) WithMockCall(call *mock.Mock) *Test { | ||
test.mockCalls = append(test.mockCalls, call.ExpectedCalls...) | ||
return test | ||
} | ||
|
||
func (test *Test) WithInputs(inputs ...any) *Test { | ||
test.inputs = inputs | ||
return test | ||
} |
Oops, something went wrong.