-
Notifications
You must be signed in to change notification settings - Fork 36
/
encode_test.go
101 lines (88 loc) · 2.63 KB
/
encode_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2014 Alvaro J. Genial. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package form
import (
"bytes"
"reflect"
"testing"
)
func TestEncodeToString(t *testing.T) {
for _, c := range testCases(encOnly) {
if s, err := EncodeToString(c.b); err != nil {
t.Errorf("EncodeToString(%#v): %s", c.b, err)
} else if !reflect.DeepEqual(c.s, s) {
t.Errorf("EncodeToString(%#v)\n want (%#v)\n have (%#v)", c.b, c.s, s)
}
}
}
func TestEncodeToValues(t *testing.T) {
for _, c := range testCases(encOnly) {
cvs := mustParseQuery(c.s)
if vs, err := EncodeToValues(c.b); err != nil {
t.Errorf("EncodeToValues(%#v): %s", c.b, err)
} else if !reflect.DeepEqual(cvs, vs) {
t.Errorf("EncodeToValues(%#v)\n want (%#v)\n have (%#v)", c.b, cvs, vs)
}
}
}
func TestEncode(t *testing.T) {
for _, c := range testCases(encOnly) {
var w bytes.Buffer
e := NewEncoder(&w)
if err := e.Encode(c.b); err != nil {
t.Errorf("Encode(%#v): %s", c.b, err)
} else if s := w.String(); !reflect.DeepEqual(c.s, s) {
t.Errorf("Encode(%#v)\n want (%#v)\n have (%#v)", c.b, c.s, s)
}
}
}
type Thing1 struct {
String string `form:"name,omitempty"`
Integer *uint `form:"num,omitempty"`
}
type Thing2 struct {
String string `form:"name,omitempty"`
Integer uint `form:"num,omitempty"`
}
type Thing3 struct {
String string `form:"name"`
Integer *uint `form:"num"`
}
type Thing4 struct {
String string `form:"name"`
Integer uint `form:"num"`
}
func TestEncode_KeepZero(t *testing.T) {
num := uint(0)
for _, c := range []struct {
b interface{}
s string
z bool
}{
{Thing1{"test", &num}, "name=test&num=", false},
{Thing1{"test", &num}, "name=test&num=0", true},
{Thing2{"test", num}, "name=test", false},
{Thing2{"test", num}, "name=test", true},
{Thing3{"test", &num}, "name=test&num=", false},
{Thing3{"test", &num}, "name=test&num=0", true},
{Thing4{"test", num}, "name=test&num=", false},
{Thing4{"test", num}, "name=test&num=0", true},
{Thing1{"", &num}, "num=", false},
{Thing1{"", &num}, "num=0", true},
{Thing2{"", num}, "", false},
{Thing2{"", num}, "", true},
{Thing3{"", &num}, "name=&num=", false},
{Thing3{"", &num}, "name=&num=0", true},
{Thing4{"", num}, "name=&num=", false},
{Thing4{"", num}, "name=&num=0", true},
} {
var w bytes.Buffer
e := NewEncoder(&w)
if err := e.KeepZeros(c.z).Encode(c.b); err != nil {
t.Errorf("KeepZeros(%#v).Encode(%#v): %s", c.z, c.b, err)
} else if s := w.String(); c.s != s {
t.Errorf("KeepZeros(%#v).Encode(%#v)\n want (%#v)\n have (%#v)", c.z, c.b, c.s, s)
}
}
}