-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbool_test.go
More file actions
144 lines (136 loc) · 2.52 KB
/
bool_test.go
File metadata and controls
144 lines (136 loc) · 2.52 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package fmt
import "testing"
func TestToBool(t *testing.T) {
tests := []struct {
name string
input any
expected bool
hasContent bool
}{
{
name: "String true",
input: "true",
expected: true,
hasContent: false,
},
{
name: "String false",
input: "false",
expected: false,
hasContent: false,
},
{
name: "String 1",
input: "1",
expected: true,
hasContent: false,
},
{
name: "String 0",
input: "0",
expected: false,
hasContent: false,
},
{
name: "Boolean true",
input: true,
expected: true,
hasContent: false,
},
{
name: "Boolean false",
input: false,
expected: false,
hasContent: false,
},
{
name: "Integer 1",
input: 1,
expected: true,
hasContent: false,
},
{
name: "Integer 0",
input: 0,
expected: false,
hasContent: false,
},
{
name: "Integer non-zero",
input: 42,
expected: true,
hasContent: false,
},
{
name: "Invalid string",
input: "invalid",
expected: false,
hasContent: true,
},
{
name: "Nil input",
input: nil,
expected: false,
hasContent: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out, err := Convert(tt.input).Bool()
if tt.hasContent {
if err == nil {
t.Errorf("Expected error, but got none")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if out != tt.expected {
t.Errorf("Expected %v, got %v", tt.expected, out)
}
}
})
}
}
func TestFromBool(t *testing.T) {
tests := []struct {
name string
input bool
expected string
}{
{
name: "True to string",
input: true,
expected: "true",
},
{
name: "False to string",
input: false,
expected: "false",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := Convert(tt.input).String()
if out != tt.expected {
t.Errorf("Expected %q, got %q", tt.expected, out)
}
})
}
}
func TestBoolChaining(t *testing.T) {
// Test chaining with boolean operations
out := Convert(true).ToUpper().String()
expected := "TRUE"
if out != expected {
t.Errorf("Expected %q, got %q", expected, out)
}
// Test converting back
boolVal, err := Convert("TRUE").ToLower().Bool()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !boolVal {
t.Errorf("Expected true, got false")
}
}