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

Function TestCopyJSON

msgp/json_test.go:11–78  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9)
10
11func TestCopyJSON(t *testing.T) {
12 var buf bytes.Buffer
13 enc := NewWriter(&buf)
14 const mapLength = 6
15 enc.WriteMapHeader(mapLength)
16
17 enc.WriteString("thing_1")
18 enc.WriteString("a string object")
19
20 enc.WriteString("a_map")
21 enc.WriteMapHeader(2)
22 enc.WriteString("float_a")
23 enc.WriteFloat32(1.0)
24 enc.WriteString("int_b")
25 enc.WriteInt64(-100)
26
27 enc.WriteString("some bytes")
28 enc.WriteBytes([]byte("here are some bytes"))
29 enc.WriteString("a bool")
30 enc.WriteBool(true)
31
32 enc.WriteString("a map")
33 enc.WriteMapStrStr(map[string]string{
34 "internal_one": "blah",
35 "internal_two": "blahhh...",
36 })
37 enc.WriteString("float64")
38 const encodedFloat64 = 1672209023
39 enc.WriteFloat64(encodedFloat64)
40 enc.Flush()
41
42 var js bytes.Buffer
43 _, err := CopyToJSON(&js, &buf)
44 if err != nil {
45 t.Fatal(err)
46 }
47 mp := make(map[string]any)
48 err = json.Unmarshal(js.Bytes(), &mp)
49 if err != nil {
50 t.Log(js.String())
51 t.Fatalf("Error unmarshaling: %s", err)
52 }
53
54 if len(mp) != mapLength {
55 t.Errorf("map length should be %d, not %d", mapLength, len(mp))
56 }
57
58 so, ok := mp["thing_1"]
59 if !ok || so != "a string object" {
60 t.Errorf("expected %q; got %q", "a string object", so)
61 }
62
63 in, ok := mp["a map"]
64 if !ok {
65 t.Error("no key 'a map'")
66 }
67 if inm, ok := in.(map[string]any); !ok {
68 t.Error("inner map not type-assertable to map[string]interface{}")

Callers

nothing calls this directly

Calls 13

WriteMapHeaderMethod · 0.95
WriteStringMethod · 0.95
WriteFloat32Method · 0.95
WriteInt64Method · 0.95
WriteBytesMethod · 0.95
WriteBoolMethod · 0.95
WriteMapStrStrMethod · 0.95
WriteFloat64Method · 0.95
FlushMethod · 0.95
NewWriterFunction · 0.85
CopyToJSONFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…