summaryrefslogtreecommitdiff
path: root/lib/helpers_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/helpers_test.go')
-rw-r--r--lib/helpers_test.go100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/helpers_test.go b/lib/helpers_test.go
new file mode 100644
index 00000000..2ea80692
--- /dev/null
+++ b/lib/helpers_test.go
@@ -0,0 +1,100 @@
+package lib
+
+import (
+ "bytes"
+ "io"
+ "os"
+ "testing"
+)
+
+type mockInputConverter struct {
+ typ string
+ action Action
+ desc string
+ err error
+ inputFn func(Container) (Container, error)
+}
+
+func (m mockInputConverter) GetType() string {
+ return m.typ
+}
+
+func (m mockInputConverter) GetAction() Action {
+ return m.action
+}
+
+func (m mockInputConverter) GetDescription() string {
+ if m.desc != "" {
+ return m.desc
+ }
+ return "mock input converter"
+}
+
+func (m mockInputConverter) Input(c Container) (Container, error) {
+ if m.inputFn != nil {
+ return m.inputFn(c)
+ }
+ return c, m.err
+}
+
+type mockOutputConverter struct {
+ typ string
+ action Action
+ desc string
+ err error
+ outFn func(Container) error
+}
+
+func (m mockOutputConverter) GetType() string {
+ return m.typ
+}
+
+func (m mockOutputConverter) GetAction() Action {
+ return m.action
+}
+
+func (m mockOutputConverter) GetDescription() string {
+ if m.desc != "" {
+ return m.desc
+ }
+ return "mock output converter"
+}
+
+func (m mockOutputConverter) Output(c Container) error {
+ if m.outFn != nil {
+ return m.outFn(c)
+ }
+ return m.err
+}
+
+func captureOutput(t *testing.T, fn func()) string {
+ t.Helper()
+
+ stdout := os.Stdout
+ r, w, _ := os.Pipe()
+ os.Stdout = w
+
+ fn()
+
+ _ = w.Close()
+ os.Stdout = stdout
+
+ var buf bytes.Buffer
+ _, _ = io.Copy(&buf, r)
+ _ = r.Close()
+
+ return buf.String()
+}
+
+func resetInputConverters() {
+ inputConverterMap = make(map[string]InputConverter)
+}
+
+func resetOutputConverters() {
+ outputConverterMap = make(map[string]OutputConverter)
+}
+
+func resetConfigCreators() {
+ inputConfigCreatorCache = make(map[string]inputConfigCreator)
+ outputConfigCreatorCache = make(map[string]outputConfigCreator)
+}