aboutsummaryrefslogtreecommitdiffstats
path: root/formats/packp/pktline/encoder_test.go
diff options
context:
space:
mode:
authorAlberto Cortés <alcortesm@gmail.com>2016-10-26 17:56:26 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-10-26 15:56:26 +0000
commit73fa9ef25a8af9c8337a4cf34a67cfe208f1a7c5 (patch)
tree66886d9b47e373b748c1bceafe1885e6f47868dd /formats/packp/pktline/encoder_test.go
parentf3ab3a6c73015b5ae9b2a4756dc646e1211cedb9 (diff)
downloadgo-git-73fa9ef25a8af9c8337a4cf34a67cfe208f1a7c5.tar.gz
Use advrefs in gituploadpackinfo (#92)
* add advrefs encoder and parser * modify advrefs encoder to resemble json encoder * turn advrefs parser into a decoder * clean code * improve documentation * improve documentation * clean code * upgrade to new pktline.Add and add Flush const to easy integration * gometalinter * Use packp/advrefs for GitUploadPackInfo parsing - GitUploadPackInfo now uses packp/advrefs instead of parsing the message by itself. - Capabilities has been moved from clients/common to packp to avoid a circular import. - Cleaning of advrefs_test code. - Add support for prefix encoding and decoding in advrefs. * clean advrefs test code * clean advrefs test code * clean advrefs test code * gometalinter * add pktline encoder * change pktline.EncodeFlush to pktline.Flush * make scanner tests use the encoder instead of Pktlines * check errors on flush and clean constants * ubstitute the PktLines type with a pktline.Encoder * use pktline.Encoder in all go-git * add example of pktline.Encodef() * add package overview * documentation * support symbolic links other than HEAD * simplify decoding of shallows * packp: fix mcuadros comments - all abbreviates removed (by visual inspection, some may remain) - all empty maps are initialized using make - simplify readRef with a switch - make decodeShallow malformed error more verbose - add pktline.Encoder.encodeLine - remove infamous panic in checkPayloadLength by refactoring out the whole function
Diffstat (limited to 'formats/packp/pktline/encoder_test.go')
-rw-r--r--formats/packp/pktline/encoder_test.go249
1 files changed, 249 insertions, 0 deletions
diff --git a/formats/packp/pktline/encoder_test.go b/formats/packp/pktline/encoder_test.go
new file mode 100644
index 0000000..5990ea5
--- /dev/null
+++ b/formats/packp/pktline/encoder_test.go
@@ -0,0 +1,249 @@
+package pktline_test
+
+import (
+ "bytes"
+ "os"
+ "strings"
+ "testing"
+
+ "gopkg.in/src-d/go-git.v4/formats/packp/pktline"
+
+ . "gopkg.in/check.v1"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type SuiteEncoder struct{}
+
+var _ = Suite(&SuiteEncoder{})
+
+func (s *SuiteEncoder) TestFlush(c *C) {
+ var buf bytes.Buffer
+ e := pktline.NewEncoder(&buf)
+
+ err := e.Flush()
+ c.Assert(err, IsNil)
+
+ obtained := buf.Bytes()
+ c.Assert(obtained, DeepEquals, pktline.FlushPkt)
+}
+
+func (s *SuiteEncoder) TestEncode(c *C) {
+ for i, test := range [...]struct {
+ input [][]byte
+ expected []byte
+ }{
+ {
+ input: [][]byte{
+ []byte("hello\n"),
+ },
+ expected: []byte("000ahello\n"),
+ }, {
+ input: [][]byte{
+ []byte("hello\n"),
+ pktline.Flush,
+ },
+ expected: []byte("000ahello\n0000"),
+ }, {
+ input: [][]byte{
+ []byte("hello\n"),
+ []byte("world!\n"),
+ []byte("foo"),
+ },
+ expected: []byte("000ahello\n000bworld!\n0007foo"),
+ }, {
+ input: [][]byte{
+ []byte("hello\n"),
+ pktline.Flush,
+ []byte("world!\n"),
+ []byte("foo"),
+ pktline.Flush,
+ },
+ expected: []byte("000ahello\n0000000bworld!\n0007foo0000"),
+ }, {
+ input: [][]byte{
+ []byte(strings.Repeat("a", pktline.MaxPayloadSize)),
+ },
+ expected: []byte(
+ "fff0" + strings.Repeat("a", pktline.MaxPayloadSize)),
+ }, {
+ input: [][]byte{
+ []byte(strings.Repeat("a", pktline.MaxPayloadSize)),
+ []byte(strings.Repeat("b", pktline.MaxPayloadSize)),
+ },
+ expected: []byte(
+ "fff0" + strings.Repeat("a", pktline.MaxPayloadSize) +
+ "fff0" + strings.Repeat("b", pktline.MaxPayloadSize)),
+ },
+ } {
+ comment := Commentf("input %d = %v\n", i, test.input)
+
+ var buf bytes.Buffer
+ e := pktline.NewEncoder(&buf)
+
+ err := e.Encode(test.input...)
+ c.Assert(err, IsNil, comment)
+
+ c.Assert(buf.Bytes(), DeepEquals, test.expected, comment)
+ }
+}
+
+func (s *SuiteEncoder) TestEncodeErrPayloadTooLong(c *C) {
+ for i, input := range [...][][]byte{
+ [][]byte{
+ []byte(strings.Repeat("a", pktline.MaxPayloadSize+1)),
+ },
+ [][]byte{
+ []byte("hello world!"),
+ []byte(strings.Repeat("a", pktline.MaxPayloadSize+1)),
+ },
+ [][]byte{
+ []byte("hello world!"),
+ []byte(strings.Repeat("a", pktline.MaxPayloadSize+1)),
+ []byte("foo"),
+ },
+ } {
+ comment := Commentf("input %d = %v\n", i, input)
+
+ var buf bytes.Buffer
+ e := pktline.NewEncoder(&buf)
+
+ err := e.Encode(input...)
+ c.Assert(err, Equals, pktline.ErrPayloadTooLong, comment)
+ }
+}
+
+func (s *SuiteEncoder) TestEncodeStrings(c *C) {
+ for i, test := range [...]struct {
+ input []string
+ expected []byte
+ }{
+ {
+ input: []string{
+ "hello\n",
+ },
+ expected: []byte("000ahello\n"),
+ }, {
+ input: []string{
+ "hello\n",
+ pktline.FlushString,
+ },
+ expected: []byte("000ahello\n0000"),
+ }, {
+ input: []string{
+ "hello\n",
+ "world!\n",
+ "foo",
+ },
+ expected: []byte("000ahello\n000bworld!\n0007foo"),
+ }, {
+ input: []string{
+ "hello\n",
+ pktline.FlushString,
+ "world!\n",
+ "foo",
+ pktline.FlushString,
+ },
+ expected: []byte("000ahello\n0000000bworld!\n0007foo0000"),
+ }, {
+ input: []string{
+ strings.Repeat("a", pktline.MaxPayloadSize),
+ },
+ expected: []byte(
+ "fff0" + strings.Repeat("a", pktline.MaxPayloadSize)),
+ }, {
+ input: []string{
+ strings.Repeat("a", pktline.MaxPayloadSize),
+ strings.Repeat("b", pktline.MaxPayloadSize),
+ },
+ expected: []byte(
+ "fff0" + strings.Repeat("a", pktline.MaxPayloadSize) +
+ "fff0" + strings.Repeat("b", pktline.MaxPayloadSize)),
+ },
+ } {
+ comment := Commentf("input %d = %v\n", i, test.input)
+
+ var buf bytes.Buffer
+ e := pktline.NewEncoder(&buf)
+
+ err := e.EncodeString(test.input...)
+ c.Assert(err, IsNil, comment)
+ c.Assert(buf.Bytes(), DeepEquals, test.expected, comment)
+ }
+}
+
+func (s *SuiteEncoder) TestEncodeStringErrPayloadTooLong(c *C) {
+ for i, input := range [...][]string{
+ []string{
+ strings.Repeat("a", pktline.MaxPayloadSize+1),
+ },
+ []string{
+ "hello world!",
+ strings.Repeat("a", pktline.MaxPayloadSize+1),
+ },
+ []string{
+ "hello world!",
+ strings.Repeat("a", pktline.MaxPayloadSize+1),
+ "foo",
+ },
+ } {
+ comment := Commentf("input %d = %v\n", i, input)
+
+ var buf bytes.Buffer
+ e := pktline.NewEncoder(&buf)
+
+ err := e.EncodeString(input...)
+ c.Assert(err, Equals, pktline.ErrPayloadTooLong, comment)
+ }
+}
+
+func (s *SuiteEncoder) TestEncodef(c *C) {
+ format := " %s %d\n"
+ str := "foo"
+ d := 42
+
+ var buf bytes.Buffer
+ e := pktline.NewEncoder(&buf)
+
+ err := e.Encodef(format, str, d)
+ c.Assert(err, IsNil)
+
+ expected := []byte("000c foo 42\n")
+ c.Assert(buf.Bytes(), DeepEquals, expected)
+}
+
+func ExampleEncoder() {
+ // Create an encoder that writes pktlines to stdout.
+ e := pktline.NewEncoder(os.Stdout)
+
+ // Encode some data as a new pkt-line.
+ _ = e.Encode([]byte("data\n")) // error checks removed for brevity
+
+ // Encode a flush-pkt.
+ _ = e.Flush()
+
+ // Encode a couple of byte slices and a flush in one go. Each of
+ // them will end up as payloads of their own pktlines.
+ _ = e.Encode(
+ []byte("hello\n"),
+ []byte("world!\n"),
+ pktline.Flush,
+ )
+
+ // You can also encode strings:
+ _ = e.EncodeString(
+ "foo\n",
+ "bar\n",
+ pktline.FlushString,
+ )
+
+ // You can also format and encode a payload:
+ _ = e.Encodef(" %s %d\n", "foo", 42)
+ // Output:
+ // 0009data
+ // 0000000ahello
+ // 000bworld!
+ // 00000008foo
+ // 0008bar
+ // 0000000c foo 42
+}