-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
setters_test.go
39 lines (35 loc) · 1011 Bytes
/
setters_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package queryparam_test
import (
"errors"
"github.com/tomwright/queryparam/v4"
"reflect"
"testing"
)
func TestGenericSetter(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
data := struct {
Value string
Target string
}{}
err := queryparam.GenericSetter(reflect.ValueOf(&data).Elem().FieldByName("Value"), reflect.ValueOf(&data).Elem().FieldByName("Target"))
if err != nil {
t.Errorf("unexpected error: %s", err)
return
}
if data.Value != data.Target {
t.Errorf("expected %v, got %v", data.Value, data.Target)
}
})
t.Run("BadValue", func(t *testing.T) {
data := struct {
Value string
Target int
}{}
err := queryparam.GenericSetter(reflect.ValueOf(&data).Elem().FieldByName("Value"), reflect.ValueOf(&data).Elem().FieldByName("Target"))
expErr := errors.New("reflect.Set: value of type string is not assignable to type int")
if err == nil || err.Error() != expErr.Error() {
t.Errorf("expected error `%v`, got `%v`", expErr, err)
return
}
})
}