-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
50 lines (44 loc) · 1.46 KB
/
reader_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
package iodebug
import (
"log"
"strings"
"testing"
)
func TestReadReturnsExpectedData(t *testing.T) {
// Configure logging to a string
var logOutput strings.Builder
log.SetOutput(&logOutput)
// Set up a sub-reader
str := "This is a test message!"
strReader := strings.NewReader(str)
// Create a DebugReader and then Read()
r := NewDebugReader(strReader)
buff := make([]byte, 32)
n, err := r.Read(buff)
// Test data returned by Read()
if n != len(str) {
t.Errorf("expected: n === %d, got: %d", len(str), n)
}
if err != nil {
t.Errorf("expected: err === nil, got: %q", err)
}
if string(buff[:n]) != str {
t.Errorf("expected: buff[:n] === %q, got: %q", str, string(buff[:n]))
}
// Test debug output - we must strip the log output prefix first as this
// default to date so will vary for every test
prefixLength := 20
expectedOutput := [2]string{
"00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6d |This is a test m|",
"00000010 65 73 73 61 67 65 21 |essage!|",
}
receivedOutput := strings.Split(strings.TrimSuffix(logOutput.String(), "\n"), "\n")
if len(receivedOutput) != len(expectedOutput) {
t.Errorf("expected: len(receivedOutput) === %d, got: %d", len(expectedOutput), len(receivedOutput))
}
for i, _ := range expectedOutput {
if receivedOutput[i][prefixLength:] != expectedOutput[i] {
t.Errorf("expected: logOutput[%d] === %q, got: %q", i, expectedOutput[i][prefixLength:], receivedOutput[i])
}
}
}