This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathto_test.go
162 lines (149 loc) · 3.94 KB
/
to_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
package godash
import (
"fmt"
"testing"
)
func TestToInt(t *testing.T) {
tests := []string{"1000", "-123", "abcdef", "100000000000000000000000000000000000000000000"}
expected := []int64{1000, -123, 0, 0}
for i := 0; i < len(tests); i++ {
result, _ := ToInt(tests[i])
if result != expected[i] {
t.Log("Case ", i, ": expected ", expected[i], " when result is ", result)
t.FailNow()
}
}
}
func TestToBoolean(t *testing.T) {
tests := []string{"true", "1", "True", "false", "0", "abcdef"}
expected := []bool{true, true, true, false, false, false}
for i := 0; i < len(tests); i++ {
res, _ := ToBoolean(tests[i])
if res != expected[i] {
t.Log("Case ", i, ": expected ", expected[i], " when result is ", res)
t.FailNow()
}
}
}
func ExampleToBoolean() {
fmt.Println(ToBoolean("True"))
fmt.Println(ToBoolean("true"))
fmt.Println(ToBoolean("1"))
fmt.Println(ToBoolean("False"))
fmt.Println(ToBoolean("false"))
fmt.Println(ToBoolean("0"))
// Output:
// true <nil>
// true <nil>
// true <nil>
// false <nil>
// false <nil>
// false <nil>
}
func toString(t *testing.T, test interface{}, expected string) {
res := ToString(test)
if res != expected {
t.Log("Case ToString: expected ", expected, " when result is ", res)
t.FailNow()
}
}
func TestToString(t *testing.T) {
toString(t, "str123", "str123")
toString(t, 123, "123")
toString(t, 12.3, "12.3")
toString(t, true, "true")
toString(t, 1.5+10i, "(1.5+10i)")
// Sprintf function not guarantee that maps with equal keys always will be equal in string representation
//toString(t, struct{ Keys map[int]int }{Keys: map[int]int{1: 2, 3: 4}}, "{map[1:2 3:4]}")
}
func TestToFloat(t *testing.T) {
tests := []string{"", "123", "-.01", "10.", "string", "1.23e3", ".23e10"}
expected := []float64{0, 123, -0.01, 10.0, 0, 1230, 0.23e10}
for i := 0; i < len(tests); i++ {
res, _ := ToFloat(tests[i])
if res != expected[i] {
t.Log("Case ", i, ": expected ", expected[i], " when result is ", res)
t.FailNow()
}
}
}
func TestToCamelCase(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected string
}{
{"a_b_c", "ABC"},
{"my_func", "MyFunc"},
{"1ab_cd", "1abCd"},
{"toUTFCamelCase", "ToUTFCamelCase"},
{"camel case", "CamelCase"},
{" camel case ", "CamelCase"},
{"!!!camel case====", "CamelCase"},
{"camel-case", "CamelCase"},
{"camel_case", "CamelCase"},
}
for _, test := range tests {
actual := ToCamelCase(test.param)
if actual != test.expected {
t.Errorf("Expected ToCamelCase(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
func ExampleToCamelCase() {
fmt.Println(ToCamelCase("camel case"))
fmt.Println(ToCamelCase(" camel case "))
fmt.Println(ToCamelCase("!!!camel case===="))
fmt.Println(ToCamelCase("camel-case"))
fmt.Println(ToCamelCase("camel_case"))
// Output:
// CamelCase
// CamelCase
// CamelCase
// CamelCase
// CamelCase
}
func TestToSnakeCase(t *testing.T) {
t.Parallel()
var tests = []struct {
param string
expected string
}{
{"a", "a"},
{"snake", "snake"},
{"A", "a"},
{"ID", "id"},
{"MOTD", "motd"},
{"Snake", "snake"},
{"SnakeTest", "snake_test"},
{"SnakeID", "snake_id"},
{"LinuxMOTD", "linux_motd"},
{"snake case", "snake_case"},
{" ! snake----- [ case", "snake_case"},
{"publicF", "public_f"},
{"RESTful", "restful"},
{"APIResponse", "apiresponse"},
{"simpleXML", "simple_xml"},
}
for _, test := range tests {
actual := ToSnakeCase(test.param)
if actual != test.expected {
t.Errorf("Expected ToSnakeCase(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}
func ExampleToSnakeCase() {
fmt.Println(ToSnakeCase("SnakeCase"))
fmt.Println(ToSnakeCase("snake case"))
fmt.Println(ToSnakeCase(" snake case "))
fmt.Println(ToSnakeCase("!!!snake case===="))
fmt.Println(ToSnakeCase("snake-case"))
fmt.Println(ToSnakeCase("snake_case"))
// Output:
// snake_case
// snake_case
// snake_case
// snake_case
// snake_case
// snake_case
}