This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
util_test.go
72 lines (61 loc) · 1.63 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
package main
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCheck(t *testing.T) {
// Handle checked errors nicely
defer func() {
if err := recover(); err != nil {
switch err.(type) {
case *CommandError:
assert.Equal(t, "Test Error (Inner Error)", fmt.Sprintf("%s", err))
default:
t.Errorf("Expected to catch a CommandError but got %v", err)
}
}
}()
check(errors.New("Inner Error"), "Test Error")
}
func TestAssert(t *testing.T) {
// Handle checked errors nicely
defer func() {
if err := recover(); err != nil {
switch err.(type) {
case *CommandError:
assert.Equal(t, "Test Error", fmt.Sprintf("%s", err))
default:
t.Errorf("Expected to catch a CommandError but got %v", err)
}
}
}()
assertThat(false, "Test Error")
}
func TestMin(t *testing.T) {
assert.Equal(t, 1, min(1, 2))
assert.Equal(t, 1, min(2, 1))
}
func TestMax(t *testing.T) {
assert.Equal(t, 2, max(1, 2))
assert.Equal(t, 2, max(2, 1))
}
func TestEllipsis(t *testing.T) {
assert.Equal(t, "123", ellipsis("123", 5))
assert.Equal(t, "12345", ellipsis("12345", 5))
assert.Equal(t, "12...", ellipsis("123456", 5))
assert.Equal(t, "", ellipsis("", 5))
}
func TestDefaults(t *testing.T) {
assert.Equal(t, "abc", defaults("abc", "123"))
assert.Equal(t, "123", defaults("", "123"))
assert.Equal(t, "", defaults("", ""))
assert.Equal(t, "", defaults(""))
assert.Equal(t, "", defaults())
}
func TestStripWhitespace(t *testing.T) {
assert.Equal(t, "abc", stripWhitespace(" a b c "))
assert.Equal(t, "abc", stripWhitespace(" a b\n c "))
assert.Equal(t, "abc", stripWhitespace(" a \r\nb\n c \n"))
}