diff --git a/README.md b/README.md index 72c96db..04ec7b1 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,8 @@ func TestParam_AddItem(t *testing.T) { // An extra check to ensure that all mocks were used assert.Equal(t, 0, simple.UnusedMocks()) } -``` \ No newline at end of file +``` + +## Examples + +See more [examples](examples/) \ No newline at end of file diff --git a/call.go b/call.go index 43b9b91..f949a5d 100644 --- a/call.go +++ b/call.go @@ -105,6 +105,17 @@ func (c *Call) Fill(params ...interface{}) { panic("The numbers of values doesn't match the number of params") } + // Update params + for i, v := range c.ParamUpdate { + elem := reflect.ValueOf(c.Params[i]).Elem() + if elem.Kind() == reflect.Ptr { + elem.Elem().Set(reflect.ValueOf(v)) + } else { + elem.Set(reflect.ValueOf(v)) + } + } + + // Set return values for i, v := range c.Response { // Param must be a pointer pType := reflect.TypeOf(params[i]) @@ -121,21 +132,4 @@ func (c *Call) Fill(params ...interface{}) { param := reflect.ValueOf(params[i]).Elem() param.Set(reflect.ValueOf(v)) } -} - -func (c *Call) Update(position int, param interface{}) *Call { - // Skip if there is no value to update - v, ok := c.ParamUpdate[position] - if !ok { - return c - } - - elem := reflect.ValueOf(param).Elem() - if elem.Kind() == reflect.Ptr { - elem.Elem().Set(reflect.ValueOf(v)) - } else { - elem.Set(reflect.ValueOf(v)) - } - - return c -} +} \ No newline at end of file diff --git a/examples/with-multi-param-update/wparamupdate.go b/examples/with-multi-param-update/wparamupdate.go new file mode 100644 index 0000000..b46c4e2 --- /dev/null +++ b/examples/with-multi-param-update/wparamupdate.go @@ -0,0 +1,13 @@ +package wparam + +import "github.com/gbrlmza/gosmock" + +// The mocked implementation +type MyStruct struct { + gosmock.MockTool +} + +func (s *MyStruct) AddItem(name *string, quantity *int, updatableParam *string) (p1 error) { + s.GetMockedResponse(s.AddItem, name, quantity, updatableParam).Fill(&p1) + return +} diff --git a/examples/with-multi-param-update/wparamupdate_test.go b/examples/with-multi-param-update/wparamupdate_test.go new file mode 100644 index 0000000..ae8a914 --- /dev/null +++ b/examples/with-multi-param-update/wparamupdate_test.go @@ -0,0 +1,30 @@ +package wparam + +import ( + "gotest.tools/assert" + "testing" +) + +func TestParam_AddItem(t *testing.T) { + simple := &MyStruct{} + + num := 1 + str1 := "str1" + str2 := "str2" + simple.Mock(simple.AddItem). + WithParams(&str1, &num, &str2). + WithParamUpdate(0, "newstr1"). + WithParamUpdate(1, 2). + WithParamUpdate(2, "newstr2"). + WithResponse(nil). + Times(1) + + err := simple.AddItem(&str1, &num, &str2) + assert.NilError(t, err) + assert.Equal(t, "newstr1", str1) + assert.Equal(t, 2, num) + assert.Equal(t, "newstr2", str2) + + // An extra check to ensure that all mocks were used + assert.Equal(t, true, simple.AllMocksUsed()) +} diff --git a/examples/with-param-update/wparamupdate.go b/examples/with-param-update/wparamupdate.go index 8051e7a..e4296cb 100644 --- a/examples/with-param-update/wparamupdate.go +++ b/examples/with-param-update/wparamupdate.go @@ -9,7 +9,6 @@ type MyStruct struct { func (s *MyStruct) AddItem(name string, quantity int, updatableParam interface{}) (p1 error) { s.GetMockedResponse(s.AddItem, name, quantity, updatableParam). - Update(2, updatableParam). Fill(&p1) return } diff --git a/examples/with-param-update2/wparamupdate.go b/examples/with-param-update2/wparamupdate.go index 719947f..b48f89e 100644 --- a/examples/with-param-update2/wparamupdate.go +++ b/examples/with-param-update2/wparamupdate.go @@ -8,8 +8,6 @@ type MyStruct struct { } func (s *MyStruct) AddItem(name string, quantity int, updatableParam *string) (p1 error) { - s.GetMockedResponse(s.AddItem, name, quantity, updatableParam). - Update(2, updatableParam). - Fill(&p1) + s.GetMockedResponse(s.AddItem, name, quantity, updatableParam).Fill(&p1) return }