-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils_test.go
111 lines (95 loc) · 3.05 KB
/
utils_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
package noodlog
import (
"fmt"
"strings"
"testing"
)
var jsnSlashes = "{\"param1\": 1, \"param2\": \"secret\", \"param3\": {\"param4\": \"secret\"}}"
var jsnRaw = `{"param1": 1, "param2": "secret", "param3": {"param4": "secret"}}`
var jsnMap = map[string]interface{}{
"param1": 1,
"param2": "secret",
"param3": map[string]interface{}{"param4": "secret"},
}
func TestPointerOfString(t *testing.T) {
actual := pointerOfString("red")
if *actual != *Red {
t.Errorf("TestPointerOfString failed: expected %s, got %s", *Red, *actual)
}
}
func TestPointerOfBool(t *testing.T) {
actual := pointerOfBool(true)
if *actual != *Enable {
t.Errorf("TestPointerOfString failed: expected %t, got %t", *Enable, *actual)
}
}
func TestStringify(t *testing.T) {
expected := "hello to everyone!"
actual := stringify([]interface{}{"hello", "to", "everyone!"})
if actual != expected {
t.Errorf("TestStringify failed: expected %s, got %s", expected, actual)
}
}
func TestObscureParam(t *testing.T) {
testData := map[string]string{
jsnSlashes: "{\"param1\": 1, \"param2\": \"**********\", \"param3\": {\"param4\": \"secret\"}}",
jsnRaw: `{"param1": 1, "param2": "**********", "param3": {"param4": "secret"}}`,
}
for input, expected := range testData {
if actual := obscureParam(input, "param2"); actual != expected {
t.Errorf("TestObscureParam failed: expected %s, got %s", expected, actual)
}
}
}
func TestObscureParams(t *testing.T) {
testData := map[string]string{
jsnSlashes: "{\"param1\": 1, \"param2\": \"**********\", \"param3\": {\"param4\": \"**********\"}}",
jsnRaw: `{"param1": 1, "param2": "**********", "param3": {"param4": "**********"}}`,
}
for input, expected := range testData {
if actual := obscureParams(input, []string{"param2", "param4"}); actual != expected {
t.Errorf("TestObscureParams failed: expected %s, got %s", expected, actual)
}
}
}
func TestStrToObj(t *testing.T) {
simpleMessageStr := "simple-message"
var simpleMessage interface{} = simpleMessageStr
testData := map[string]interface{}{
jsnSlashes: jsnMap,
jsnRaw: jsnMap,
simpleMessageStr: simpleMessage,
}
for input, expected := range testData {
if actual := strToObj(input); fmt.Sprintf("%v", actual) != fmt.Sprintf("%v", expected) {
t.Errorf("TestStrToObj failed: expected %s, got %s", expected, actual)
}
}
}
func TestTraceCaller(t *testing.T) {
errFormat := "TestTraceCaller failed: expected %s, got %s"
file, function := traceCaller(5)
expectedFile := ":0"
expectedFunction := ""
if file != expectedFile {
t.Errorf(errFormat, expectedFile, file)
}
if function != expectedFunction {
t.Errorf(errFormat, expectedFunction, function)
}
expectedFilePortion := "runtime"
expectedFunction = "runtime.goexit"
file, function = caller2()
if !strings.Contains(file, expectedFilePortion) {
t.Errorf(errFormat, expectedFilePortion, file)
}
if function != expectedFunction {
t.Errorf(errFormat, expectedFunction, function)
}
}
func caller1() (string, string) {
return traceCaller(6)
}
func caller2() (string, string) {
return caller1()
}