-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_test.go
More file actions
114 lines (84 loc) · 2.87 KB
/
output_test.go
File metadata and controls
114 lines (84 loc) · 2.87 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
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
package console_test
import (
"bytes"
"testing"
"github.com/eidolon/console"
"github.com/seeruk/assert"
)
func TestNewOutput(t *testing.T) {
output := console.NewOutput(&bytes.Buffer{})
assert.True(t, output != nil, "Output should not be nil")
}
func TestOutput(t *testing.T) {
// Realistically, these tests are a little lacking, but that's mainly because we're pretty much
// transparently passing everything over to the fmt package. "So why are you bothering to do
// that?", simply so we can customise the writer in the application, and not have to specify it
// every time we write.
t.Run("Print()", func(t *testing.T) {
t.Run("should handle printing", func(t *testing.T) {
buffer := bytes.Buffer{}
output := console.NewOutput(&buffer)
message := "Hello, World!"
nbytes, err := output.Print(message)
assert.OK(t, err)
assert.Equal(t, 13, nbytes)
assert.Equal(t, message, buffer.String())
})
t.Run("should handle printing multiple values", func(t *testing.T) {
buffer := bytes.Buffer{}
output := console.NewOutput(&buffer)
message1 := "Go "
message2 := "testing!"
nbytes, err := output.Print(message1, message2)
assert.OK(t, err)
assert.Equal(t, 11, nbytes)
assert.Equal(t, message1+message2, buffer.String())
})
})
t.Run("Printf()", func(t *testing.T) {
t.Run("should handle printing", func(t *testing.T) {
buffer := bytes.Buffer{}
output := console.NewOutput(&buffer)
nbytes, err := output.Printf("Hello, %s!", "World")
assert.OK(t, err)
assert.Equal(t, 13, nbytes)
assert.Equal(t, "Hello, World!", buffer.String())
})
t.Run("should handle multiple parameters", func(t *testing.T) {
buffer := bytes.Buffer{}
output := console.NewOutput(&buffer)
message := "Hello %s, the answer to life the universe and everything is %d!"
param1 := "Go"
param2 := 42
expected := "Hello Go, the answer to life the universe and everything is 42!"
nbytes, err := output.Printf(message, param1, param2)
assert.OK(t, err)
assert.Equal(t, 63, nbytes)
assert.Equal(t, expected, buffer.String())
})
})
t.Run("Println()", func(t *testing.T) {
t.Run("should handle printing", func(t *testing.T) {
buffer := bytes.Buffer{}
output := console.NewOutput(&buffer)
message := "Hello, World!"
expected := "Hello, World!\n"
nbytes, err := output.Println(message)
assert.OK(t, err)
assert.Equal(t, 14, nbytes)
assert.Equal(t, expected, buffer.String())
})
t.Run("should handle printing multiple values", func(t *testing.T) {
buffer := bytes.Buffer{}
output := console.NewOutput(&buffer)
message1 := "Go "
message2 := "testing!"
// Remember, fmt.Println adds spaces between things.
expected := "Go testing!\n"
nbytes, err := output.Println(message1, message2)
assert.OK(t, err)
assert.Equal(t, 13, nbytes)
assert.Equal(t, expected, buffer.String())
})
})
}