-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestable_test.go
More file actions
80 lines (71 loc) · 1.85 KB
/
Copy pathtestable_test.go
File metadata and controls
80 lines (71 loc) · 1.85 KB
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
package testable
import (
"errors"
"strings"
"testing"
gloo "github.com/gloo-foo/framework"
"github.com/gloo-foo/framework/patterns"
)
// upper is a tiny command that upper-cases each input line.
func upper() gloo.Command[[]byte, []byte] {
return patterns.Map(func(line []byte) ([]byte, error) {
return []byte(strings.ToUpper(string(line))), nil
})
}
// failing is a command that fails on the first line.
func failing() gloo.Command[[]byte, []byte] {
return patterns.Map(func([]byte) ([]byte, error) {
return nil, errors.New("boom")
})
}
func TestTest_OutputRetainsTrailingNewline(t *testing.T) {
out, err := Test(upper(), "hello\nworld\n")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "HELLO\nWORLD\n" {
t.Errorf("Test() = %q, want %q", out, "HELLO\nWORLD\n")
}
}
func TestTest_PropagatesError(t *testing.T) {
out, err := Test(failing(), "x\n")
if err == nil {
t.Fatalf("expected error, got nil")
}
if out != "" {
t.Errorf("Test() output = %q, want empty on error", out)
}
}
func TestTestLines_SplitsAndStrips(t *testing.T) {
lines, err := TestLines(upper(), "a\nb\nc\n")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := []string{"A", "B", "C"}
if len(lines) != len(want) {
t.Fatalf("got %d lines, want %d: %v", len(lines), len(want), lines)
}
for i := range want {
if lines[i] != want[i] {
t.Errorf("line[%d] = %q, want %q", i, lines[i], want[i])
}
}
}
func TestTestLines_Empty(t *testing.T) {
lines, err := TestLines(upper(), "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(lines) != 0 {
t.Errorf("got %d lines, want 0", len(lines))
}
}
func TestTestLines_PropagatesError(t *testing.T) {
lines, err := TestLines(failing(), "x\n")
if err == nil {
t.Fatalf("expected error, got nil")
}
if lines != nil {
t.Errorf("lines = %v, want nil on error", lines)
}
}