MCPcopy
hub / github.com/google/mangle / TestJSONtoStruct

Function TestJSONtoStruct

json2struct/json2struct_test.go:35–62  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

33}
34
35func TestJSONtoStruct(t *testing.T) {
36 tests := []struct {
37 jsonBlob []byte
38 want *ast.Constant
39 }{
40 {
41 jsonBlob: []byte(`{"foo": "bar", "fnum": 3.13, "b": [true, false], "c": { "d": "e" }}`),
42 want: ast.Struct(map[*ast.Constant]*ast.Constant{
43 ptr(name("/foo")): ptr(ast.String("bar")),
44 ptr(name("/fnum")): ptr(ast.Float64(3.13)),
45 ptr(name("/b")): ptr(ast.ListCons(&ast.TrueConstant, ptr(ast.ListCons(&ast.FalseConstant, &ast.ListNil)))),
46 ptr(name("/c")): ast.Struct(map[*ast.Constant]*ast.Constant{
47 ptr(name("/d")): ptr(ast.String("e")),
48 }),
49 }),
50 },
51 }
52 for _, test := range tests {
53 got, err := JSONtoStruct(test.jsonBlob)
54 if err != nil {
55 t.Errorf("JSONtoStruct(%v) failed: %v", test.jsonBlob, err)
56 continue
57 }
58 if !got.Equals(test.want) {
59 t.Errorf("JSONtoStruct(%v) = %v, want %v", test.jsonBlob, got, test.want)
60 }
61 }
62}

Callers

nothing calls this directly

Calls 8

StructFunction · 0.92
StringFunction · 0.92
Float64Function · 0.92
ListConsFunction · 0.92
JSONtoStructFunction · 0.85
ptrFunction · 0.70
nameFunction · 0.70
EqualsMethod · 0.65

Tested by

no test coverage detected