-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_test.go
129 lines (116 loc) · 3.06 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package rrh
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestIsInputYes(t *testing.T) {
var testcases = []struct {
givesString string
wontFlag bool
}{
{"YeS", true},
{"y", true},
{"no", false},
{"true", false},
}
for _, tc := range testcases {
GivesStringAsStdin(tc.givesString, func() {
var flag = IsInputYes("")
if flag != tc.wontFlag {
t.Errorf("%s read flag did not match, wont %v, got %v", tc.givesString, tc.wontFlag, flag)
}
})
}
}
func TestGitRepositoryCheck(t *testing.T) {
var testcases = []struct {
path string
errorFlag bool
}{
{"testdata/fibonacci", false},
{"testdata/database.json", true},
{"testdata/other", true},
{"testdata/not-exist", true},
}
for _, testcase := range testcases {
var absPath, _ = filepath.Abs(testcase.path)
var err = IsExistAndGitRepository(absPath, testcase.path)
if (err == nil) == testcase.errorFlag {
t.Errorf("%s: error wont: %v, got: %v (%v)", testcase.path, testcase.errorFlag, !testcase.errorFlag, err)
}
}
}
func TestStrftime(t *testing.T) {
os.Setenv(TimeFormat, Relative)
os.Setenv(ConfigPath, "testdata/config.json")
var now = time.Now()
var testcases = []struct {
formatter string
time time.Time
wont string
}{
{Relative, now.Add(time.Minute * -1), "1 minute ago"},
{Relative, now.Add(time.Hour * -24 * 6), "6 days ago"},
{Relative, now.Add(time.Hour * -24 * 10), "1 week ago"},
{Relative, now.Add(time.Hour * -24 * 15), "2 weeks ago"},
{"2006-01-02 15:04:05", now, now.Format("2006-01-02 15:04:05")},
}
var config = OpenConfig()
for _, test := range testcases {
os.Setenv(TimeFormat, test.formatter)
var time = Strftime(test.time, config)
if time != test.wont {
t.Errorf("wont: %s, got: %s", test.wont, time)
}
}
os.Unsetenv(TimeFormat)
os.Unsetenv(ConfigPath)
}
func TestRollback(t *testing.T) {
var file = Rollback("testdata/test_db.json", "testdata/config.json", func(config *Config, db *Database) {
db.ForceDeleteGroup("group1")
db.ForceDeleteGroup("group2")
db.DeleteRepository("repo1")
db.DeleteRepository("repo2")
db.StoreAndClose()
})
defer os.Remove(file)
var db, _ = Open(OpenConfig())
if !db.HasGroup("group1") || !db.HasGroup("group2") {
t.Errorf("database did not rollbacked")
}
if !db.HasRepository("repo1") || !db.HasRepository("repo2") {
t.Errorf("database did not rollbacked")
}
}
func TestReplaceNewline(t *testing.T) {
var testcases = []struct {
give string
replaceTo string
wont string
}{
{"a\nb\nc", ",", "a,b,c"},
{"a\rb\n", ",", "a,b"},
{"a\nb\rc\r\n", ",", "a,b,c"},
{"a\nb\rc\r\n", ", ", "a, b, c"},
}
for _, tc := range testcases {
var got = ReplaceNewline(tc.give, tc.replaceTo)
if got != tc.wont {
t.Errorf("ReplaceNewLine(%s, %s) wont: %s, got: %s", tc.give, tc.replaceTo, tc.wont, got)
}
}
}
func TestCaptureStdout(t *testing.T) {
var result = CaptureStdout(func() {
fmt.Println("Hello World")
})
result = strings.TrimSpace(result)
if result != "Hello World" {
t.Errorf("wont: \"Hello World\", got: %s", result)
}
}