-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
decoder_test.go
59 lines (50 loc) · 1.22 KB
/
decoder_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
51
52
53
54
55
56
57
58
59
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package binary
import (
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBinaryDecodeStruct(t *testing.T) {
s := &s0{}
err := Unmarshal(s0b, s)
assert.NoError(t, err)
assert.Equal(t, s0v, s)
}
func TestBinaryDecodeToValueErrors(t *testing.T) {
b := []byte{1, 0, 0, 0}
var v uint32
err := Unmarshal(b, v)
assert.Error(t, err)
err = Unmarshal(b, &v)
assert.NoError(t, err)
assert.Equal(t, uint32(1), v)
}
type oneByteReader struct {
content []byte
}
// Read method of io.Reader reads *up to* len(buf) bytes.
// It is possible to read LESS, and it can happen when reading a file.
func (r *oneByteReader) Read(buf []byte) (n int, err error) {
if len(r.content) == 0 {
err = io.EOF
return
}
if len(buf) == 0 {
return
}
n = 1
buf[0] = r.content[0]
r.content = r.content[1:]
return
}
func TestDecodeFromReader(t *testing.T) {
data := "data string"
encoded, err := Marshal(data)
assert.NoError(t, err)
decoder := NewDecoder(&oneByteReader{content: encoded})
str, err := decoder.ReadString()
assert.NoError(t, err)
assert.Equal(t, data, str)
}