forked from facebookarchive/fbapi
/
params_test.go
66 lines (58 loc) · 1.41 KB
/
params_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fbapi_test
import (
"errors"
"net/url"
"reflect"
"testing"
"github.com/facebookgo/fbapi"
)
const paramWithErrorMessage = "foo"
type paramWithError struct{}
func (p paramWithError) Set(v url.Values) error {
return errors.New(paramWithErrorMessage)
}
func TestParams(t *testing.T) {
cases := []struct {
Params []fbapi.Param
Expected url.Values
}{
{
Params: []fbapi.Param{fbapi.ParamLimit(42)},
Expected: url.Values{"limit": []string{"42"}},
},
{
Params: []fbapi.Param{fbapi.ParamOffset(42)},
Expected: url.Values{"offset": []string{"42"}},
},
{
Params: []fbapi.Param{fbapi.ParamFields("abc", "def")},
Expected: url.Values{"fields": []string{"abc,def"}},
},
{
Params: []fbapi.Param{fbapi.ParamAccessToken("42")},
Expected: url.Values{"access_token": []string{"42"}},
},
{
Params: []fbapi.Param{fbapi.ParamDateFormat("42")},
Expected: url.Values{"date_format": []string{"42"}},
},
}
for _, c := range cases {
v, err := fbapi.ParamValues(c.Params...)
if err != nil {
t.Errorf("case %+v got error %s", c, err)
}
if !reflect.DeepEqual(c.Expected, v) {
t.Fatalf("case\n%+v\nactual:\n%+v", c, v)
}
}
}
func TestParamsError(t *testing.T) {
_, err := fbapi.ParamValues(paramWithError{})
if err == nil {
t.Fatal("was expecting error")
}
if err.Error() != paramWithErrorMessage {
t.Fatalf("expected %s got %s", paramWithErrorMessage, err)
}
}