MCPcopy
hub / github.com/tinylib/msgp / TestReplace

Function TestReplace

msgp/edit_test.go:86–176  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

84}
85
86func TestReplace(t *testing.T) {
87 // there are 4 cases that need coverage:
88 // - new value is smaller than old value
89 // - new value is the same size as the old value
90 // - new value is larger than old, but fits within cap(b)
91 // - new value is larger than old, and doesn't fit within cap(b)
92
93 var buf bytes.Buffer
94 en := NewWriter(&buf)
95 en.WriteMapHeader(3)
96 en.WriteString("thing_one")
97 en.WriteString("value_one")
98 en.WriteString("thing_two")
99 en.WriteFloat64(2.0)
100 en.WriteString("some_bytes")
101 en.WriteBytes([]byte("here are some bytes"))
102 en.Flush()
103
104 // same-size replacement
105 var fbuf bytes.Buffer
106 w := NewWriter(&fbuf)
107 w.WriteFloat64(4.0)
108 w.Flush()
109
110 // replace 2.0 with 4.0 in field two
111 raw := Replace("thing_two", buf.Bytes(), fbuf.Bytes())
112 if len(raw) == 0 {
113 t.Fatal("field not found")
114 }
115 var err error
116 m := make(map[string]any)
117 m, _, err = ReadMapStrIntfBytes(raw, m)
118 if err != nil {
119 t.Logf("%q", raw)
120 t.Fatal(err)
121 }
122
123 if !reflect.DeepEqual(m["thing_two"], 4.0) {
124 t.Errorf("wanted %v; got %v", 4.0, m["thing_two"])
125 }
126
127 // smaller-size replacement
128 // replace 2.0 with []byte("hi!")
129 fbuf.Reset()
130 w.WriteBytes([]byte("hi!"))
131 w.Flush()
132 raw = Replace("thing_two", raw, fbuf.Bytes())
133 if len(raw) == 0 {
134 t.Fatal("field not found")
135 }
136
137 m, _, err = ReadMapStrIntfBytes(raw, m)
138 if err != nil {
139 t.Logf("%q", raw)
140 t.Fatal(err)
141 }
142
143 if !reflect.DeepEqual(m["thing_two"], []byte("hi!")) {

Callers

nothing calls this directly

Calls 13

WriteMapHeaderMethod · 0.95
WriteStringMethod · 0.95
WriteFloat64Method · 0.95
WriteBytesMethod · 0.95
FlushMethod · 0.95
NewWriterFunction · 0.85
ReplaceFunction · 0.85
ReadMapStrIntfBytesFunction · 0.85
LocateFunction · 0.85
CopyReplaceFunction · 0.85
ResetMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…