-
Notifications
You must be signed in to change notification settings - Fork 45
/
util_test.go
77 lines (66 loc) · 1.67 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
package squalor
import (
"errors"
"strings"
"testing"
)
func TestCombineErrors(t *testing.T) {
errs := []error{
errors.New("error 1"),
errors.New("error 2"),
}
expectedErr := strings.Join([]string{"error 1", "error 2"}, "\n")
if err := combineErrors(errs...); err == nil || err.Error() != expectedErr {
t.Fatalf("Expected: %v, got: %v", expectedErr, err)
}
}
func TestRecoveryToError(t *testing.T) {
expectedErr := errors.New("panic")
if err := recoveryToError(errors.New("panic")); err == nil || err.Error() != expectedErr.Error() {
t.Fatalf("Expected: %v, got %v", expectedErr, err)
}
if err := recoveryToError("panic"); err == nil || err.Error() != expectedErr.Error() {
t.Fatalf("Expected: %v, got %v", expectedErr, err)
}
expectedErr = errors.New("unknown panic")
if err := recoveryToError(nil); err == nil || err.Error() != expectedErr.Error() {
t.Fatalf("Expected: %v, got %v", expectedErr, err)
}
}
func TestTruncate(t *testing.T) {
testCases := []struct {
input, expected string
n int
}{
{
"😀😎🌎😀😎🌎", "😀😎🌎😀😎🌎", 7,
},
{
"😀😎🌎😀😎🌎", "😀😎🌎😀😎🌎", 6,
},
{
"😀😎🌎😀😎🌎", "😀😎🌎😀…", 5,
},
{
"😀😎🌎😀😎🌎", "😀😎🌎…", 4,
},
{
"😀😎🌎😀😎🌎", "😀😎…", 3,
},
{
"😀😎🌎😀😎🌎", "😀…", 2,
},
{
"😀😎🌎😀😎🌎", "…", 1,
},
{
"😀😎🌎😀😎🌎", "", 0,
},
}
for _, tc := range testCases {
actual := truncate(tc.input, tc.n)
if actual != tc.expected {
t.Fatalf("Expected %q got %q (n=%d)", tc.expected, actual, tc.n)
}
}
}