aboutsummaryrefslogtreecommitdiffstats
path: root/formats/pktline/encoder_test.go
blob: f718c3391c9629f382f41c952bc83bf8b67a8ccc (plain) (blame)
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 pktline

import (
	"bytes"
	"io/ioutil"
	"strings"

	. "gopkg.in/check.v1"
)

type EncoderSuite struct{}

var _ = Suite(&EncoderSuite{})

func (s *EncoderSuite) TestEncode(c *C) {
	line, err := Encode([]byte("a\n"))
	c.Assert(err, IsNil)
	c.Assert(string(line), Equals, "0006a\n")
}

func (s *EncoderSuite) TestEncodeNil(c *C) {
	line, err := Encode(nil)
	c.Assert(err, IsNil)
	c.Assert(string(line), Equals, "0000")
}

func (s *EncoderSuite) TestEncodeOverflow(c *C) {
	_, err := Encode(bytes.Repeat([]byte{'0'}, MaxLength+1))
	c.Assert(err, Equals, ErrOverflow)
}

func (s *EncoderSuite) TestEncodeFromString(c *C) {
	line, err := EncodeFromString("a\n")
	c.Assert(err, IsNil)
	c.Assert(string(line), Equals, "0006a\n")
}

func (s *EncoderSuite) TestEncoder(c *C) {
	e := NewEncoder()
	c.Assert(e.AddLine("a"), IsNil)
	e.AddFlush()
	c.Assert(e.AddLine("b"), IsNil)

	over := strings.Repeat("0", MaxLength+1)
	c.Assert(e.AddLine(over), Equals, ErrOverflow)

	r := e.Reader()
	a, _ := ioutil.ReadAll(r)
	c.Assert(string(a), Equals, "0006a\n00000006b\n")
}