-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy patheditor_test.go
59 lines (53 loc) · 1.44 KB
/
editor_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
package editor
import (
"bytes"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type editorFunc func(string) (string, error)
func (f editorFunc) Edit(line string) (string, error) {
return f(line)
}
func addPrefixEditor(prefix string) Editor {
return editorFunc(func(line string) (string, error) {
return prefix + line, nil
})
}
func TestWriter(t *testing.T) {
t.Run("valid editor and writer", func(t *testing.T) {
var buffer bytes.Buffer
stream := Stream(&buffer, addPrefixEditor("A"), addPrefixEditor("B"))
{
_, err := stream.Write([]byte("ab"))
require.NoError(t, err, "write in stream")
assert.Empty(t, buffer.Bytes())
}
{
_, err := stream.Write([]byte("\ncd"))
require.NoError(t, err, "write in stream")
assert.Equal(t, "BAab\n", buffer.String())
}
{
require.NoError(t, stream.Close(), "close a stream")
// It's undesirable spec :(
// A following newline is unnecessary.
assert.Equal(t, "BAab\nBAcd\n", buffer.String())
}
})
t.Run("invalid editor", func(t *testing.T) {
var buffer bytes.Buffer
stream := Stream(&buffer, editorFunc(func(string) (string, error) { return "", errors.New("test") }))
{
_, err := stream.Write([]byte("ab"))
require.NoError(t, err, "write in stream")
assert.Empty(t, buffer.Bytes())
}
{
_, err := stream.Write([]byte("\ncd"))
require.EqualError(t, err, "test")
assert.Empty(t, buffer.Bytes())
}
})
}