-
Notifications
You must be signed in to change notification settings - Fork 1
/
zend_operator_test.go
180 lines (167 loc) · 2.97 KB
/
zend_operator_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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package gophplib
import (
"fmt"
"os"
"reflect"
"testing"
)
type Sample struct{}
type Sample2 struct{}
type CustomType struct {
value string
}
func (s Sample) toString() string {
return "sample object"
}
func getFile() *os.File {
file, osErr := os.Open("README.md")
if osErr != nil {
panic(osErr)
}
return file
}
func ExampleConvertToString() {
// Nil
fmt.Println(ConvertToString(nil))
// Plain string
fmt.Println(ConvertToString("Hello, World"))
// Empty string
fmt.Println(ConvertToString(""))
// Int
fmt.Println(ConvertToString(123))
// Float
fmt.Println(ConvertToString(123.45))
// Exponent
fmt.Println(ConvertToString(10.1234567e10))
// Float exceeds 14 digits
fmt.Println(ConvertToString(1230.12984732591475609346509132875091237))
// Float ends with 0
fmt.Println(ConvertToString(1230.12984732500000000000000000000000000))
// True
fmt.Println(ConvertToString(true))
// False
fmt.Println(ConvertToString(false))
// Array
fmt.Println(ConvertToString([]int{1, 2, 3}))
// Slice
fmt.Println(ConvertToString([2]int{1, 2}))
// Object has toString
fmt.Println(ConvertToString(Cat{
name: "nabi",
age: 3,
}))
// Object has no toString
fmt.Println(ConvertToString(Dog{
name: "choco",
age: 5,
}))
// Output:
// <nil>
// Hello, World <nil>
// <nil>
// 123 <nil>
// 123.45 <nil>
// 101234567000 <nil>
// 1230.1298473259 <nil>
// 1230.129847325 <nil>
// 1 <nil>
// <nil>
// Array <nil>
// Array <nil>
// name is nabi and 3 years old <nil>
// unsupported type : gophplib.Dog
}
func TestConvertToString(t *testing.T) {
file := getFile()
testCase := []struct {
any
string
}{
{
nil,
"",
},
{
"Hello, World",
"Hello, World",
},
{
"",
"",
},
{
123,
"123",
},
{
123.45,
"123.45",
},
{
10.1234567e10,
"101234567000",
},
{
1230.12984732591475609346509132875091237,
"1230.1298473259",
},
{
1230.12984732500000000000000000000000000,
"1230.129847325",
},
{
float32(5.0546941757202),
"5.0546941757202",
},
{
true,
"1",
},
{
false,
"",
},
{
[]int{1, 2, 3},
"Array",
},
{
[2]int{1, 2},
"Array",
},
{
file,
fmt.Sprintf("Resource id %p", file),
},
{
Cat{name: "nabi", age: 3},
"name is nabi and 3 years old",
},
}
for _, tc := range testCase {
testName := fmt.Sprintf("%v", tc.any)
t.Run(testName, func(t *testing.T) {
result, err := ConvertToString(tc.any)
if err != nil {
t.Errorf("%s: expected success to convert, bug get error %v", testName, err)
}
if !reflect.DeepEqual(result, tc.string) {
t.Errorf("%s: string %v, bug got %v", testName, tc.string, result)
}
})
}
// Failing cases
errorCase := []any{
Dog{name: "choco", age: 5},
}
for _, tc := range errorCase {
testName := fmt.Sprintf("%v", tc)
t.Run(testName, func(t *testing.T) {
result, err := ConvertToString(tc)
if err == nil {
t.Errorf("%s: error, bug got %v", testName, result)
}
})
}
file.Close()
}