File: omitzero_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 (77 lines) | stat: -rw-r--r-- 1,794 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package _generated

import (
	"bytes"
	"testing"
)

func TestOmitZero(t *testing.T) {

	t.Run("OmitZeroExt_not_empty", func(t *testing.T) {

		z := OmitZero0{AExt: OmitZeroExt{a: 1}}
		b, err := z.MarshalMsg(nil)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Contains(b, []byte("aext")) {
			t.Errorf("expected to find aext in bytes %X", b)
		}
		z = OmitZero0{}
		_, err = z.UnmarshalMsg(b)
		if err != nil {
			t.Fatal(err)
		}
		if z.AExt.a != 1 {
			t.Errorf("z.AExt.a expected 1 but got %d", z.AExt.a)
		}

	})

	t.Run("OmitZeroExt_negative", func(t *testing.T) {

		z := OmitZero0{AExt: OmitZeroExt{a: -1}} // negative value should act as empty, via IsEmpty() call
		b, err := z.MarshalMsg(nil)
		if err != nil {
			t.Fatal(err)
		}
		if bytes.Contains(b, []byte("aext")) {
			t.Errorf("expected to not find aext in bytes %X", b)
		}
		z = OmitZero0{}
		_, err = z.UnmarshalMsg(b)
		if err != nil {
			t.Fatal(err)
		}
		if z.AExt.a != 0 {
			t.Errorf("z.AExt.a expected 0 but got %d", z.AExt.a)
		}

	})

	t.Run("OmitZeroTuple", func(t *testing.T) {

		// make sure tuple encoding isn't affected by omitempty or omitzero

		z := OmitZero0{OmitZeroTuple: OmitZeroTuple{FieldA: "", FieldB: "", FieldC: "fcval"}}
		b, err := z.MarshalMsg(nil)
		if err != nil {
			t.Fatal(err)
		}
		// verify the exact binary encoding, that the values follow each other without field names
		if !bytes.Contains(b, []byte{0xA0, 0xA0, 0xA5, 'f', 'c', 'v', 'a', 'l'}) {
			t.Errorf("failed to find expected bytes in %X", b)
		}
		z = OmitZero0{}
		_, err = z.UnmarshalMsg(b)
		if err != nil {
			t.Fatal(err)
		}
		if z.OmitZeroTuple.FieldA != "" ||
			z.OmitZeroTuple.FieldB != "" ||
			z.OmitZeroTuple.FieldC != "fcval" {
			t.Errorf("z.OmitZeroTuple unexpected value: %#v", z.OmitZeroTuple)
		}

	})
}