-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountWriter_test.go
More file actions
47 lines (42 loc) · 1.08 KB
/
countWriter_test.go
File metadata and controls
47 lines (42 loc) · 1.08 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
package main
import (
"bytes"
"encoding/binary"
"io"
"testing"
)
type shortWriter struct {
w io.Writer
max int
}
func (sw *shortWriter) Write(p []byte) (int, error) {
if len(p) > sw.max {
p = p[:sw.max]
}
return sw.w.Write(p)
}
func TestWriteStringNormal(t *testing.T) {
var buf bytes.Buffer
if err := WriteLPString(&buf, "hello"); err != nil {
t.Fatalf("WriteString returned error: %v", err)
}
want := make([]byte, 2)
binary.LittleEndian.PutUint16(want, uint16(len("hello")))
want = append(want, []byte("hello")...)
if !bytes.Equal(buf.Bytes(), want) {
t.Errorf("unexpected output: %v", buf.Bytes())
}
}
func TestWriteStringShortWrite(t *testing.T) {
var underlying bytes.Buffer
sw := &shortWriter{w: &underlying, max: 2}
if err := WriteLPString(sw, "hello"); err != nil {
t.Fatalf("WriteString returned error: %v", err)
}
want := make([]byte, 2)
binary.LittleEndian.PutUint16(want, uint16(len("hello")))
want = append(want, []byte("hello")...)
if !bytes.Equal(underlying.Bytes(), want) {
t.Errorf("unexpected output with short writer: %v", underlying.Bytes())
}
}