File: generator_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 4,328 kB
  • sloc: sh: 61; makefile: 5
file content (184 lines) | stat: -rw-r--r-- 5,205 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package generator_test

import (
	"fmt"
	"regexp"
	"strings"
	"testing"

	"github.com/mattn/go-colorable"
	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"

	"github.com/nicholas-fedor/shoutrrr/pkg/util/generator"
)

func TestGenerator(t *testing.T) {
	gomega.RegisterFailHandler(ginkgo.Fail)
	ginkgo.RunSpecs(t, "Generator Suite")
}

var (
	client  *generator.UserDialog
	userOut *gbytes.Buffer
	userIn  *gbytes.Buffer
)

func mockTyped(a ...any) {
	_, _ = fmt.Fprint(userOut, a...)
	_, _ = fmt.Fprint(userOut, "\n")
}

func dumpBuffers() {
	for _, line := range strings.Split(string(userIn.Contents()), "\n") {
		_, _ = fmt.Fprint(ginkgo.GinkgoWriter, "> ", line, "\n")
	}

	for _, line := range strings.Split(string(userOut.Contents()), "\n") {
		_, _ = fmt.Fprint(ginkgo.GinkgoWriter, "< ", line, "\n")
	}
}

var _ = ginkgo.Describe("GeneratorCommon", func() {
	ginkgo.BeforeEach(func() {
		userOut = gbytes.NewBuffer()
		userIn = gbytes.NewBuffer()
		userInMono := colorable.NewNonColorable(userIn)
		client = generator.NewUserDialog(
			userOut,
			userInMono,
			map[string]string{"propKey": "propVal"},
		)
	})

	ginkgo.It("reprompt upon invalid answers", func() {
		defer dumpBuffers()
		answer := make(chan string)
		go func() {
			answer <- client.QueryString("name:", generator.Required, "")
		}()

		mockTyped("")
		mockTyped("Normal Human Name")

		gomega.Eventually(userIn).Should(gbytes.Say(`name: `))
		gomega.Eventually(userIn).Should(gbytes.Say(`field is required`))
		gomega.Eventually(userIn).Should(gbytes.Say(`name: `))
		gomega.Eventually(answer).Should(gomega.Receive(gomega.Equal("Normal Human Name")))
	})

	ginkgo.It("should accept any input when validator is nil", func() {
		defer dumpBuffers()
		answer := make(chan string)
		go func() {
			answer <- client.QueryString("name:", nil, "")
		}()
		mockTyped("")
		gomega.Eventually(answer).Should(gomega.Receive(gomega.BeEmpty()))
	})

	ginkgo.It("should use predefined prop value if key is present", func() {
		defer dumpBuffers()
		answer := make(chan string)
		go func() {
			answer <- client.QueryString("name:", generator.Required, "propKey")
		}()
		gomega.Eventually(answer).Should(gomega.Receive(gomega.Equal("propVal")))
	})

	ginkgo.Describe("Query", func() {
		ginkgo.It("should prompt until a valid answer is provided", func() {
			defer dumpBuffers()
			answer := make(chan []string)
			query := "pick foo or bar:"
			go func() {
				answer <- client.Query(query, regexp.MustCompile("(foo|bar)"), "")
			}()

			mockTyped("")
			mockTyped("foo")

			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(userIn).Should(gbytes.Say(`invalid format`))
			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(answer).Should(gomega.Receive(gomega.ContainElement("foo")))
		})
	})

	ginkgo.Describe("QueryAll", func() {
		ginkgo.It("should prompt until a valid answer is provided", func() {
			defer dumpBuffers()
			answer := make(chan [][]string)
			query := "pick foo or bar:"
			go func() {
				answer <- client.QueryAll(query, regexp.MustCompile(`foo(ba[rz])`), "", -1)
			}()

			mockTyped("foobar foobaz")

			gomega.Eventually(userIn).Should(gbytes.Say(query))
			var matches [][]string
			gomega.Eventually(answer).Should(gomega.Receive(&matches))
			gomega.Expect(matches).To(gomega.ContainElement([]string{"foobar", "bar"}))
			gomega.Expect(matches).To(gomega.ContainElement([]string{"foobaz", "baz"}))
		})
	})

	ginkgo.Describe("QueryStringPattern", func() {
		ginkgo.It("should prompt until a valid answer is provided", func() {
			defer dumpBuffers()
			answer := make(chan string)
			query := "type of bar:"
			go func() {
				answer <- client.QueryStringPattern(query, regexp.MustCompile(".*bar"), "")
			}()

			mockTyped("foo")
			mockTyped("foobar")

			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(userIn).Should(gbytes.Say(`invalid format`))
			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(answer).Should(gomega.Receive(gomega.Equal("foobar")))
		})
	})

	ginkgo.Describe("QueryInt", func() {
		ginkgo.It("should prompt until a valid answer is provided", func() {
			defer dumpBuffers()
			answer := make(chan int64)
			query := "number:"
			go func() {
				answer <- client.QueryInt(query, "", 64)
			}()

			mockTyped("x")
			mockTyped("0x20")

			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(userIn).Should(gbytes.Say(`not a number`))
			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(answer).Should(gomega.Receive(gomega.Equal(int64(32))))
		})
	})

	ginkgo.Describe("QueryBool", func() {
		ginkgo.It("should prompt until a valid answer is provided", func() {
			defer dumpBuffers()
			answer := make(chan bool)
			query := "cool?"
			go func() {
				answer <- client.QueryBool(query, "")
			}()

			mockTyped("maybe")
			mockTyped("y")

			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(userIn).Should(gbytes.Say(`answer must be yes or no`))
			gomega.Eventually(userIn).Should(gbytes.Say(query))
			gomega.Eventually(answer).Should(gomega.Receive(gomega.BeTrue()))
		})
	})
})