aboutsummaryrefslogtreecommitdiffstats
path: root/skate/parallel/processor_test.go
blob: ee4a14f2423fc27245abf76bd84acc56e3e3a266 (plain)
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
package parallel

import (
	"bytes"
	"errors"
	"io"
	"strings"
	"testing"
)

var errFake1 = errors.New("fake error #1")

func StringSliceContains(sl []string, s string) bool {
	for _, v := range sl {
		if s == v {
			return true
		}
	}
	return false
}

// LinesEqualSeparator returns true, if every line in a, when separated by
// separator, can be found in b.
func LinesEqualSeparator(a, b, sep string) bool {
	al := strings.Split(a, sep)
	bl := strings.Split(b, sep)
	if len(al) != len(bl) {
		return false
	}
	for _, line := range al {
		if !StringSliceContains(bl, line) {
			return false
		}
	}
	return true
}

// LinesEqual returns true, if every line in a, when separated by a newline, can be found in b.
func LinesEqual(a, b string) bool {
	return LinesEqualSeparator(a, b, "\n")
}

func TestSimple(t *testing.T) {
	var cases = []struct {
		about    string
		r        io.Reader
		expected string
		f        func([]byte) ([]byte, error)
		err      error
	}{
		{
			about:    `No input produces no output.`,
			r:        strings.NewReader(""),
			expected: "",
			f:        func(b []byte) ([]byte, error) { return []byte{}, nil },
			err:      nil,
		},
		{
			about:    `Order is not guaranteed.`,
			r:        strings.NewReader("a\nb\n"),
			expected: "B\nA\n",
			f:        func(b []byte) ([]byte, error) { return bytes.ToUpper(b), nil },
			err:      nil,
		},
		{
			about:    `Like grep, we can filter out items by returning nothing.`,
			r:        strings.NewReader("a\nb\n"),
			expected: "B\n",
			f: func(b []byte) ([]byte, error) {
				if strings.TrimSpace(string(b)) == "a" {
					return []byte{}, nil
				}
				return bytes.ToUpper(b), nil
			},
			err: nil,
		},
		{
			about:    `Empty lines skipped.`,
			r:        strings.NewReader("a\na\na\na\n\n\nb\n"),
			expected: "B\n",
			f: func(b []byte) ([]byte, error) {
				if strings.TrimSpace(string(b)) == "a" {
					return []byte{}, nil
				}
				return bytes.ToUpper(b), nil
			},
			err: nil,
		},
		{
			about:    `On empty input, the transformer func is never called.`,
			r:        strings.NewReader(""),
			expected: "",
			f: func(b []byte) ([]byte, error) {
				return nil, errFake1
			},
			err: nil,
		},
		{
			about:    `Error does not come through, if all lines are skipped.`,
			r:        strings.NewReader("\n"),
			expected: "",
			f: func(b []byte) ([]byte, error) {
				return nil, errFake1
			},
			err: nil,
		},
	}

	for _, c := range cases {
		var buf bytes.Buffer
		p := NewProcessor(c.r, &buf, c.f)
		err := p.Run()
		if err != c.err {
			t.Errorf("p.Run: got %v, want %v", err, c.err)
		}
		if !LinesEqual(buf.String(), c.expected) {
			t.Errorf("p.Run: got %v, want %v", buf.String(), c.expected)
		}
	}
}