forked from go-joe/joe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_test.go
91 lines (72 loc) · 2.23 KB
/
adapter_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
package joe_test
import (
"bytes"
"io/ioutil"
"testing"
"github.com/go-joe/joe"
"github.com/go-joe/joe/joetest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func cliTestAdapter(t *testing.T) (a *joe.CLIAdapter, output *bytes.Buffer) {
logger := zaptest.NewLogger(t)
a = joe.NewCLIAdapter("test", logger)
output = new(bytes.Buffer)
a.Output = output
a.Author = "TestUser" // ensure tests never depend on external factors such as os.Getenv(…)
return a, output
}
func TestCLIAdapter_Register(t *testing.T) {
input := new(bytes.Buffer)
a, output := cliTestAdapter(t)
a.Input = ioutil.NopCloser(input)
brain := joetest.NewBrain(t)
messages := brain.Events()
input.WriteString("Hello\n")
input.WriteString("World\n")
// Start the Goroutine of the adapter which consumes the input
a.RegisterAt(brain.Brain)
msg1 := <-messages
msg2 := <-messages
assert.Equal(t, "Hello", msg1.Data.(joe.ReceiveMessageEvent).Text)
assert.Equal(t, "World", msg2.Data.(joe.ReceiveMessageEvent).Text)
// Stop the brain to make sure we are done with all callbacks
brain.Finish()
// Close the adapter to finish up the test
assert.NoError(t, a.Close())
assert.Contains(t, output.String(), "test > ")
}
func TestCLIAdapter_Send(t *testing.T) {
a, output := cliTestAdapter(t)
err := a.Send("Hello World", "")
require.NoError(t, err)
assert.Equal(t, "Hello World\n", output.String())
}
func TestCLIAdapter_Send_Author(t *testing.T) {
input := new(bytes.Buffer)
a, _ := cliTestAdapter(t)
a.Input = ioutil.NopCloser(input)
a.Author = "Friedrich"
brain := joetest.NewBrain(t)
messages := brain.Events()
input.WriteString("Test\n")
// Start the Goroutine of the adapter which consumes the input
a.RegisterAt(brain.Brain)
msg := <-messages
assert.Equal(t, "Friedrich", msg.Data.(joe.ReceiveMessageEvent).AuthorID)
brain.Finish()
assert.NoError(t, a.Close())
}
func TestCLIAdapter_Close(t *testing.T) {
input := new(bytes.Buffer)
a, output := cliTestAdapter(t)
a.Input = ioutil.NopCloser(input)
brain := joe.NewBrain(a.Logger)
a.RegisterAt(brain)
err := a.Close()
require.NoError(t, err)
assert.Equal(t, "\n", output.String())
err = a.Close()
assert.EqualError(t, err, "already closed")
}