-
Notifications
You must be signed in to change notification settings - Fork 1
/
bool_test.go
106 lines (101 loc) · 2.09 KB
/
bool_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
102
103
104
105
106
package jaws
import (
"encoding/json"
"reflect"
"testing"
)
func TestBool_BoolSetter(t *testing.T) {
var s Bool
if s.String() != "false" {
t.Fail()
}
if err := s.JawsSetBool(nil, true); err != nil {
t.Error(err)
}
if s.JawsGetBool(nil) != true {
t.Fail()
}
if s.String() != "true" {
t.Fail()
}
if err := s.JawsSetBool(nil, true); err != ErrValueUnchanged {
t.Error(err)
}
}
func TestBool_MarshalJSON(t *testing.T) {
var s, s2 Bool
s.Set(true)
b, err := json.Marshal(&s)
if err != nil {
t.Error(err)
} else {
x := string(b)
if x != "true" {
t.Errorf("%T %q", x, x)
}
}
err = json.Unmarshal(b, &s2)
if err != nil {
t.Error(err)
} else {
if s2.Value != true {
t.Error(s2.Value)
}
}
}
func TestBool_MarshalText(t *testing.T) {
tests := []struct {
name string
Value bool
want []byte
wantErr bool
}{
{"false", false, []byte("false"), false},
{"true", true, []byte("true"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Bool{
Value: tt.Value,
}
got, err := s.MarshalText()
if (err != nil) != tt.wantErr {
t.Errorf("Bool.MarshalText() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Bool.MarshalText() = %v, want %v", got, tt.want)
}
})
}
}
func TestBool_UnmarshalText(t *testing.T) {
tests := []struct {
name string
Value bool
str string
wantErr bool
}{
{"false", false, "false", false},
{"true", true, "true", false},
{"false/err", false, "foo", true},
{"true/err", true, "foo", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Bool{
Value: !tt.Value,
}
if err := s.UnmarshalText([]byte(tt.str)); (err != nil) != tt.wantErr {
t.Errorf("Bool.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr && s.Value != !tt.Value {
t.Errorf("Bool.UnmarshalText() = %v, want %v", s.Value, !tt.Value)
}
if !tt.wantErr && s.Value != tt.Value {
t.Errorf("Bool.UnmarshalText() = %v, want %v", s.Value, tt.Value)
}
})
}
}