Commit 97a655d2 authored by YuviPanda's avatar YuviPanda

Add tests for ToJson

parent 5c52fe6a
...@@ -141,3 +141,52 @@ one: ...@@ -141,3 +141,52 @@ one:
t.Fatal("Expected parser error") t.Fatal("Expected parser error")
} }
} }
func TestToJson(t *testing.T) {
expect := "{\"foo\":\"bar\"}"
v := struct {
Foo string `json:"foo"`
}{
Foo: "bar",
}
if got := ToJson(v); got != expect {
t.Errorf("Expected %q, got %q", expect, got)
}
}
func TestFromJson(t *testing.T) {
doc := `{
"hello": "world",
"one": {
"two": "three"
}
}
`
dict := FromJson(doc)
if err, ok := dict["Error"]; ok {
t.Fatalf("Parse error: %s", err)
}
if len(dict) != 2 {
t.Fatal("expected two elements.")
}
world := dict["hello"]
if world.(string) != "world" {
t.Fatal("Expected the world. Is that too much to ask?")
}
// This should fail because we don't currently support lists:
doc2 := `
[
"one",
"two",
"three"
]
`
dict = FromJson(doc2)
if _, ok := dict["Error"]; !ok {
t.Fatal("Expected parser error")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment