-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils_test.go
96 lines (78 loc) · 3.23 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
package main
import (
"bufio"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type UtilsTestSuite struct {
suite.Suite
}
func TestUtilsTestSuite(t *testing.T) {
suite.Run(t, new(UtilsTestSuite))
}
func (s *UtilsTestSuite) TestConfigCommaDelimitedStringSet() {
ccds := ConfigCommaDelimitedString{"a", "b"}
ccds.Set("c,d")
assert.Len(s.T(), ccds, 4, "'c' and 'd' should have been separated items")
ccds.Set("e f")
assert.Len(s.T(), ccds, 5, "'e' and 'f' should not have been separated")
}
func (s *UtilsTestSuite) TestConfigCommaDelimitedStringString() {
ccds := ConfigCommaDelimitedString{"a", "b", "c", "d"}
assert.Len(s.T(), ccds.String(), 7)
assert.Equal(s.T(), 3, strings.Count(ccds.String(), ","))
}
func (s *UtilsTestSuite) TestConfigMultiflagStringSet() {
ccds := ConfigMultiflagString{"a", "b"}
ccds.Set("c,d")
assert.Len(s.T(), ccds, 3, "'c' and 'd' should not have been separated items")
}
func (s *UtilsTestSuite) TestConfigMultiflagStringString() {
ccds := ConfigMultiflagString{"a", "b", "c", "d"}
assert.Len(s.T(), ccds.String(), 7)
assert.Equal(s.T(), 3, strings.Count(ccds.String(), ","))
}
func (s *UtilsTestSuite) Test_confirm_withReply() {
assert.True(s.T(), confirm(bufio.NewReader(strings.NewReader("y\n")), "hi", true))
assert.False(s.T(), confirm(bufio.NewReader(strings.NewReader("n\n")), "hi", true))
}
func (s *UtilsTestSuite) Test_confirm_withWindowsReply() {
assert.True(s.T(), confirm(bufio.NewReader(strings.NewReader("y\r\n")), "hi", true))
assert.False(s.T(), confirm(bufio.NewReader(strings.NewReader("n\r\n")), "hi", true))
}
func (s *UtilsTestSuite) Test_confirm_withWeirdReplyNoRetry() {
assert.False(s.T(), confirm(bufio.NewReader(strings.NewReader("something\n")), "hi", true))
assert.False(s.T(), confirm(bufio.NewReader(strings.NewReader("something\n")), "hi", true))
}
func (s *UtilsTestSuite) Test_confirm_withWeirdReplyAndRetry() {
assert.True(s.T(), confirm(bufio.NewReader(strings.NewReader("something\ny\n")), "hi", true, "retry please"))
assert.False(s.T(), confirm(bufio.NewReader(strings.NewReader("something\nn\n")), "hi", true, "retry please"))
}
func (s *UtilsTestSuite) Test_confirm_withoutReply() {
assert.True(s.T(), confirm(bufio.NewReader(strings.NewReader("\n")), "hi", true))
assert.False(s.T(), confirm(bufio.NewReader(strings.NewReader("\n")), "hi", false))
}
func (s *UtilsTestSuite) Test_directoryExists() {
assert.True(s.T(), directoryExists(getCurrentWorkingDirectory()))
}
func (s *UtilsTestSuite) Test_directoryExists_onFile() {
assert.False(s.T(), directoryExists(path.Join(getCurrentWorkingDirectory(), "/main.go")))
}
func (s *UtilsTestSuite) Test_fileExists() {
assert.True(s.T(), fileExists(path.Join(getCurrentWorkingDirectory(), "/main.go")))
}
func (s *UtilsTestSuite) Test_fileExists_onDirectory() {
assert.False(s.T(), fileExists(getCurrentWorkingDirectory()))
}
func (s *UtilsTestSuite) Test_sliceContainsString() {
testSlice := []string{"a", "b", "c"}
// test the happy path
assert.True(s.T(), sliceContainsString(testSlice, "a"))
// test the sad path
assert.False(s.T(), sliceContainsString(testSlice, "d"))
// test if duplicates might affect searching
assert.False(s.T(), sliceContainsString(testSlice, "aa"))
}