File: clearomitted_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 (93 lines) | stat: -rw-r--r-- 2,324 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package _generated

import (
	"bytes"
	"encoding/json"
	"reflect"
	"testing"
	"time"

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

func TestClearOmitted(t *testing.T) {
	cleared := ClearOmitted0{}
	encoded, err := cleared.MarshalMsg(nil)
	if err != nil {
		t.Fatal(err)
	}
	vPtr := NamedStringCO("value")
	filled := ClearOmitted0{
		AStruct:             ClearOmittedA{A: "something"},
		BStruct:             ClearOmittedA{A: "somthing"},
		AStructPtr:          &ClearOmittedA{A: "something"},
		AExt:                OmitZeroExt{25},
		APtrNamedStr:        &vPtr,
		ANamedStruct:        NamedStructCO{A: "value"},
		APtrNamedStruct:     &NamedStructCO{A: "sdf"},
		EmbeddableStructCO:  EmbeddableStructCO{"value"},
		EmbeddableStructCO2: EmbeddableStructCO2{"value"},
		ATime:               time.Now(),
		ASlice:              []int{1, 2, 3},
		AMap:                map[string]int{"1": 1},
		ABin:                []byte{1, 2, 3},
		ClearOmittedTuple:   ClearOmittedTuple{FieldA: "value"},
		AInt:                42,
		AString:             "value",
		Adur:                time.Second,
		AJSON:               json.Number(`43.0000000000002`),
	}
	dst := filled
	_, err = dst.UnmarshalMsg(encoded)
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(dst, cleared) {
		t.Errorf("\n got=%#v\nwant=%#v", dst, cleared)
	}
	// Reset
	dst = filled
	err = dst.DecodeMsg(msgp.NewReader(bytes.NewReader(encoded)))
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(dst, cleared) {
		t.Errorf("\n got=%#v\nwant=%#v", dst, cleared)
	}

	// Check that fields aren't accidentally zeroing fields.
	wantJson, err := json.Marshal(filled)
	if err != nil {
		t.Fatal(err)
	}
	encoded, err = filled.MarshalMsg(nil)
	if err != nil {
		t.Fatal(err)
	}
	dst = ClearOmitted0{}
	_, err = dst.UnmarshalMsg(encoded)
	if err != nil {
		t.Fatal(err)
	}
	got, err := json.Marshal(dst)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(got, wantJson) {
		t.Errorf("\n got=%#v\nwant=%#v", string(got), string(wantJson))
	}
	// Reset
	dst = ClearOmitted0{}
	err = dst.DecodeMsg(msgp.NewReader(bytes.NewReader(encoded)))
	if err != nil {
		t.Fatal(err)
	}
	got, err = json.Marshal(dst)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(got, wantJson) {
		t.Errorf("\n got=%#v\nwant=%#v", string(got), string(wantJson))
	}
	t.Log("OK - got", string(got))
}