-
Notifications
You must be signed in to change notification settings - Fork 2
/
header_test.go
76 lines (61 loc) · 1.8 KB
/
header_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package xbase
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func currentDate() time.Time {
y, m, d := time.Now().Date()
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC)
}
func TestNewHeader(t *testing.T) {
h := newHeader()
require.Equal(t, byte(0x03), h.DbfId)
require.Equal(t, uint32(0), h.RecCount)
require.Equal(t, -1, h.fieldCount())
require.Equal(t, uint16(0), h.RecSize)
require.Equal(t, currentDate(), h.modDate())
}
func TestWriteHeader(t *testing.T) {
h := newHeader()
h.RecCount = uint32(3)
h.RecSize = uint16(39)
h.setFieldCount(5)
h.setModDate(time.Date(1930, 2, 20, 0, 0, 0, 0, time.UTC))
h.setCodePage(866)
buf := bytes.NewBuffer(nil)
h.write(buf)
expected := []byte{0x3, 0x1e, 0x2, 0x14, 0x3, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x27,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x65, 0x0, 0x0}
require.Equal(t, expected, buf.Bytes())
}
func TestReadHeader(t *testing.T) {
b := []byte{0x3, 0x1e, 0x2, 0x14, 0x3, 0x0, 0x0, 0x0, 0xc1, 0x0, 0x27,
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0, 0x0, 0x65, 0x0, 0x0}
r := bytes.NewReader(b)
h := &header{}
h.read(r)
require.Equal(t, byte(0x03), h.DbfId)
require.Equal(t, uint32(3), h.RecCount)
require.Equal(t, 5, h.fieldCount())
require.Equal(t, uint16(39), h.RecSize)
require.Equal(t, 866, h.codePage())
d := time.Date(1930, 2, 20, 0, 0, 0, 0, time.UTC)
require.Equal(t, d, h.modDate())
}
func TestReadHeaderNotDBF(t *testing.T) {
b := make([]byte, headerSize)
b[0] = 0x05 // valid 0x03
r := bytes.NewReader(b)
h := &header{}
require.Panics(t, func() { h.read(r) })
}
func TestHeaderSetCodePage(t *testing.T) {
h := &header{}
h.setCodePage(866)
require.Equal(t, byte(0x65), h.CP)
require.Equal(t, 866, h.codePage())
}