2017-03-27 16:35:40 +02:00
|
|
|
package structure
|
2016-09-15 09:57:18 +02:00
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2016-09-15 09:57:18 +02:00
|
|
|
|
|
|
|
func TestNormalizeJsonString(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
var actual string
|
|
|
|
|
|
|
|
// Well formatted and valid.
|
|
|
|
validJson := `{
|
|
|
|
"abc": {
|
|
|
|
"def": 123,
|
|
|
|
"xyz": [
|
|
|
|
{
|
|
|
|
"a": "ホリネズミ"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"b": "1\\n2"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
expected := `{"abc":{"def":123,"xyz":[{"a":"ホリネズミ"},{"b":"1\\n2"}]}}`
|
|
|
|
|
2017-03-27 16:35:40 +02:00
|
|
|
actual, err = NormalizeJsonString(validJson)
|
2016-09-15 09:57:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected not to throw an error while parsing JSON, but got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("Got:\n\n%s\n\nExpected:\n\n%s\n", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Well formatted but not valid,
|
|
|
|
// missing closing squre bracket.
|
|
|
|
invalidJson := `{
|
|
|
|
"abc": {
|
|
|
|
"def": 123,
|
|
|
|
"xyz": [
|
|
|
|
{
|
|
|
|
"a": "1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
2017-03-27 16:35:40 +02:00
|
|
|
actual, err = NormalizeJsonString(invalidJson)
|
2016-09-15 09:57:18 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected to throw an error while parsing JSON, but got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the invalid JSON to be shown back to us again.
|
|
|
|
if actual != invalidJson {
|
|
|
|
t.Fatalf("Got:\n\n%s\n\nExpected:\n\n%s\n", expected, invalidJson)
|
|
|
|
}
|
|
|
|
}
|