| 33 | } |
| 34 | |
| 35 | func 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 | } |