-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_test.go
60 lines (53 loc) · 1.1 KB
/
models_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
package domainavailability
import (
"encoding/json"
"testing"
)
// TestTime tests JSON encoding/parsing functions for the StringBool values
func TestStringBool(t *testing.T) {
tests := []struct {
name string
decErr string
encErr string
}{
{
name: `"UNAVAILABLE"`,
decErr: "",
encErr: "",
},
{
name: `"AVAILABLE"`,
decErr: "",
encErr: "",
},
{
name: `""`,
decErr: "API error: [] \"\" is unexpected value for domainAvailability",
encErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var v *StringBool
err := json.Unmarshal([]byte(tt.name), &v)
checkErr(t, err, tt.decErr)
if tt.decErr != "" {
return
}
bb, err := json.Marshal(v)
checkErr(t, err, tt.encErr)
if tt.encErr != "" {
return
}
if string(bb) != tt.name {
t.Errorf("got = %v, want %v", string(bb), tt.name)
}
})
}
}
// checkErr checks for an error.
func checkErr(t *testing.T, err error, want string) {
if (err != nil || want != "") && (err == nil || err.Error() != want) {
t.Errorf("error = %v, wantErr %v", err, want)
}
}