aboutsummaryrefslogtreecommitdiffstats
path: root/formats/pktline/encoder_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'formats/pktline/encoder_test.go')
-rw-r--r--formats/pktline/encoder_test.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/formats/pktline/encoder_test.go b/formats/pktline/encoder_test.go
new file mode 100644
index 0000000..f718c33
--- /dev/null
+++ b/formats/pktline/encoder_test.go
@@ -0,0 +1,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")
+}