forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
114 lines (103 loc) · 2.03 KB
/
util_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
package ravendb
import (
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestFirstNonNilString(t *testing.T) {
tests := [][]string{
{"", "", ""},
{"", "foo", "foo"},
{"foo", "", "foo"},
{"foo", "bar", "foo"},
}
strToPtr := func(s string) *string {
if s == "" {
return nil
}
return &s
}
for _, test := range tests {
s1 := strToPtr(test[0])
s2 := strToPtr(test[1])
got := firstNonNilString(s1, s2)
exp := strToPtr(test[2])
if got == nil || exp == nil {
assert.Nil(t, got)
assert.Nil(t, exp)
} else {
assert.Equal(t, *exp, *got)
}
}
}
func TestMin(t *testing.T) {
tests := [][]int{
{0, 0, 0},
{1, 0, 0},
{0, 1, 0},
{-1, 1, -1},
{-1, -3, -3},
{3, 8, 3},
}
for _, test := range tests {
got := min(test[0], test[1])
exp := test[2]
assert.Equal(t, exp, got, "test: %#v", test)
}
}
func TestFirstNonZero(t *testing.T) {
tests := [][]int{
{0, 0, 0},
{1, 0, 1},
{0, 1, 1},
{0, -81, -81},
{5, -11, 5},
}
for _, test := range tests {
got := firstNonZero(test[0], test[1])
exp := test[2]
assert.Equal(t, exp, got)
}
}
func TestBuilderWriteInt(t *testing.T) {
tests := []int{-123, -1, 0, 1, 123}
for _, test := range tests {
b := &strings.Builder{}
builderWriteInt(b, test)
got := b.String()
exp := fmt.Sprintf("%d", test)
assert.Equal(t, exp, got, "test: %d", test)
}
}
func TestPanicIf(t *testing.T) {
defer func() {
if err := recover(); err == nil {
assert.Fail(t, "expected to panic")
} else {
assert.Equal(t, err.(error).Error(), "should fail")
}
}()
panicIf(true, "should fail")
}
func TestDbg(t *testing.T) {
stdoutSaved := os.Stdout
verboseSaved := LogVerbose
path := "util_test_tmp.txt"
f, err := os.Create(path)
assert.NoError(t, err)
os.Stdout = f
LogVerbose = true
dbg("v: %d\n", 5)
os.Stdout = stdoutSaved
LogVerbose = verboseSaved
err = f.Close()
assert.NoError(t, err)
d, err := ioutil.ReadFile(path)
assert.NoError(t, err)
s := string(d)
assert.Equal(t, s, "v: 5\n")
_ = os.Remove(path)
}