-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotuil_test.go
152 lines (131 loc) · 3.29 KB
/
gotuil_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
package goutil
import (
"os"
"testing"
)
func Test_FloatPrecision(t *testing.T) {
result := FloatPrecision(2.34343434, 2)
if result != 2.34 {
t.Errorf("Returned = %f; want 2.34", result)
}
}
func Test_FullName(t *testing.T) {
val := FullName("Lucian", "Khanakia")
if val != "Lucian Khanakia" {
t.Errorf("Returned = %s; want Lucian Khanakia", val)
}
}
// Run this in terminal - `mode="prod" go test`
func Test_Env1(t *testing.T) {
os.Setenv("ENV_VAR", "prod")
defer os.Unsetenv("ENV_VAR")
result := Env("ENV_VAR", "local")
if result != "prod" {
t.Errorf("Returned = %s; want prod", result)
}
}
func Test_Env2(t *testing.T) {
result := Env("ENV_VAR1", "local")
if result != "local" {
t.Errorf("Returned = %s; want local", result)
}
}
func Test_RandString(t *testing.T) {
val := RandString(10)
if len(val) != 10 {
t.Errorf("Run(10) = %d; want 10", len(val))
}
}
func Test_SnakeCase(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"camelCase", "camel_case"},
{"PascalCase", "pascal_case"},
{"snake_case", "snake_case"},
{"Pascal_Snake", "pascal_snake"},
{"SCREAMING_SNAKE", "screaming_snake"},
{"kebab-case", "kebab_case"},
{"Pascal-Kebab", "pascal_kebab"},
{"SCREAMING-KEBAB", "screaming_kebab"},
{"A", "a"},
{"AA", "aa"},
{"AAA", "aaa"},
{"AAAA", "aaaa"},
{"AaAa", "aa_aa"},
{"HTTPRequest", "http_request"},
{"BatteryLifeValue", "battery_life_value"},
{"Id0Value", "id0_value"},
{"ID0Value", "id0_value"},
}
for _, test := range tests {
result := SnakeCase(test.input)
if result != test.expected {
t.Errorf("Returned = %s; want %s", result, test.expected)
}
// assert.Equal(t, test.expected, result)
}
}
func Test_ToJSON(t *testing.T) {
type Student struct {
Name string `json:"name"`
RoleNo int `json:"roleNo"`
}
student := Student{
Name: "Lucian",
RoleNo: 7,
}
_, err := ToJSON(student)
if err != nil {
t.Error(err)
}
}
func Test_StringIndex(t *testing.T) {
result, _ := StringIndex([]string{"luci", "aman", "khanakia"}, "aman")
if result != 1 {
t.Errorf("Returned = %d; want %s", result, "1")
}
result, _ = StringIndex([]string{"luci", "aman", "khanakia"}, "na")
if result != -1 {
t.Errorf("Returned = %d; want %s", result, "-1")
}
}
func Test_UintIndex(t *testing.T) {
result, _ := UintIndex([]uint{1, 2, 3}, 2)
if result != 1 {
t.Errorf("Returned = %d; want %s", result, "1")
}
result, _ = UintIndex([]uint{1, 2, 3}, 12)
if result != -1 {
t.Errorf("Returned = %d; want %s", result, "-1")
}
}
func Test_IntIndex(t *testing.T) {
result, _ := IntIndex([]int{1, 2, 3}, 2)
if result != 1 {
t.Errorf("Returned = %d; want %s", result, "1")
}
result, _ = IntIndex([]int{1, 2, 3}, 12)
if result != -1 {
t.Errorf("Returned = %d; want %s", result, "-1")
}
}
func Test_CleanString(t *testing.T) {
result := CleanString("Aman C。Salcedo")
if result != "Aman CSalcedo" {
t.Errorf("Returned = %s; want %s", result, "Aman CSalcedo")
}
result = CleanString("##Khanakia")
if result != "Khanakia" {
t.Errorf("Returned = %s; want %s", result, "Khanakia")
}
}
func Test_HashString(t *testing.T) {
result := HashString("luci")
want := "2fdcbc8615c275ffbe49106cf85fbab1566b92559a251a5535a217f211dfa3f2"
if result != want {
t.Errorf("Returned = %s; want %s", result, want)
}
}