MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / TestEncoderDecoder_Roundtrip

Function TestEncoderDecoder_Roundtrip

internal/copyover/copyover_test.go:84–131  ·  view source on GitHub ↗

TestEncoderDecoder_Roundtrip verifies that the wire format preserves values exactly across multiple sections, including non-ASCII content. It goes through Save/Restore since the encoder and decoder are only well-formed as a pair produced by Save (which writes the section count header).

(t *testing.T)

Source from the content-addressed store, hash-verified

82// Save/Restore since the encoder and decoder are only well-formed as a pair
83// produced by Save (which writes the section count header).
84func TestEncoderDecoder_Roundtrip(t *testing.T) {
85 type payload struct {
86 Value string `json:"value"`
87 Count int `json:"count"`
88 }
89
90 cases := []struct {
91 name string
92 data payload
93 }{
94 {"section-a", payload{"hello", 1}},
95 {"section-b", payload{"world", 42}},
96 {"unicode", payload{"日本語テスト", 0}},
97 }
98
99 // Build contributors that hold the expected payloads.
100 type structContributor struct {
101 name string
102 data payload
103 restored payload
104 }
105 contribs := make([]*structContributor, len(cases))
106 for i, tc := range cases {
107 contribs[i] = &structContributor{name: tc.name, data: tc.data}
108 }
109
110 copyover.ResetRegistry()
111 for _, c := range contribs {
112 c := c
113 copyover.Register(copyover.FuncContributor(
114 c.name,
115 func(enc *copyover.Encoder) error { return enc.WriteSection(c.name, c.data) },
116 func(dec *copyover.Decoder) error { return dec.ReadSection(c.name, &c.restored) },
117 ))
118 }
119
120 r, w, err := os.Pipe()
121 require.NoError(t, err)
122 require.NoError(t, copyover.Save(w))
123 w.Close()
124
125 require.NoError(t, copyover.Restore(int(r.Fd())))
126 r.Close()
127
128 for _, c := range contribs {
129 assert.Equal(t, c.data, c.restored, "section %q", c.name)
130 }
131}
132
133// TestDecoder_MissingSection verifies that reading a section that was never
134// written returns an error rather than silently producing a zero value.

Callers

nothing calls this directly

Calls 8

ResetRegistryFunction · 0.92
RegisterFunction · 0.92
FuncContributorFunction · 0.92
SaveFunction · 0.92
RestoreFunction · 0.92
WriteSectionMethod · 0.80
ReadSectionMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected