Skip to content

Commit

Permalink
Remove Update(). Now all params are updatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Gonzalez committed Sep 9, 2020
1 parent 3b7e8c6 commit 14c0a38
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
```
```

## Examples

See more [examples](examples/)
30 changes: 12 additions & 18 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
}
}
13 changes: 13 additions & 0 deletions examples/with-multi-param-update/wparamupdate.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions examples/with-multi-param-update/wparamupdate_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
1 change: 0 additions & 1 deletion examples/with-param-update/wparamupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 1 addition & 3 deletions examples/with-param-update2/wparamupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 14c0a38

Please sign in to comment.