File: allownil_test.go

package info (click to toggle)
golang-github-tinylib-msgp 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: makefile: 47
file content (57 lines) | stat: -rw-r--r-- 1,035 bytes parent folder | download | duplicates (2)
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
package _generated

import (
	"bytes"
	"reflect"
	"testing"

	"github.com/tinylib/msgp/msgp"
)

func TestAllownil(t *testing.T) {
	tt := &NamedStructAN{
		A: []string{},
		B: nil,
	}
	var buf bytes.Buffer

	err := msgp.Encode(&buf, tt)
	if err != nil {
		t.Fatal(err)
	}
	in := buf.Bytes()

	for _, tnew := range []*NamedStructAN{{}, {A: []string{}}, {B: []string{}}} {
		err = msgp.Decode(bytes.NewBuffer(in), tnew)
		if err != nil {
			t.Error(err)
		}

		if !reflect.DeepEqual(tt, tnew) {
			t.Logf("in:  %#v", tt)
			t.Logf("out: %#v", tnew)
			t.Fatal("objects not equal")
		}
	}

	in, err = tt.MarshalMsg(nil)
	if err != nil {
		t.Fatal(err)
	}
	for _, tanother := range []*NamedStructAN{{}, {A: []string{}}, {B: []string{}}} {
		var left []byte
		left, err = tanother.UnmarshalMsg(in)
		if err != nil {
			t.Error(err)
		}
		if len(left) > 0 {
			t.Errorf("%d bytes left", len(left))
		}

		if !reflect.DeepEqual(tt, tanother) {
			t.Logf("in:  %#v", tt)
			t.Logf("out: %#v", tanother)
			t.Fatal("objects not equal")
		}
	}
}