github.com/jrperritt/terraform@v0.1.1-0.20170525065507-96f391dafc38/helper/structure/normalize_json_test.go (about) 1 package structure 2 3 import ( 4 "testing" 5 ) 6 7 func TestNormalizeJsonString_valid(t *testing.T) { 8 // Well formatted and valid. 9 validJson := `{ 10 "abc": { 11 "def": 123, 12 "xyz": [ 13 { 14 "a": "ホリネズミ" 15 }, 16 { 17 "b": "1\\n2" 18 } 19 ] 20 } 21 }` 22 expected := `{"abc":{"def":123,"xyz":[{"a":"ホリネズミ"},{"b":"1\\n2"}]}}` 23 24 actual, err := NormalizeJsonString(validJson) 25 if err != nil { 26 t.Fatalf("Expected not to throw an error while parsing JSON, but got: %s", err) 27 } 28 29 if actual != expected { 30 t.Fatalf("Got:\n\n%s\n\nExpected:\n\n%s\n", actual, expected) 31 } 32 } 33 34 func TestNormalizeJsonString_invalid(t *testing.T) { 35 // Well formatted but not valid, 36 // missing closing squre bracket. 37 invalidJson := `{ 38 "abc": { 39 "def": 123, 40 "xyz": [ 41 { 42 "a": "1" 43 } 44 } 45 } 46 }` 47 expected := `{"abc":{"def":123,"xyz":[{"a":"ホリネズミ"},{"b":"1\\n2"}]}}` 48 actual, err := NormalizeJsonString(invalidJson) 49 if err == nil { 50 t.Fatalf("Expected to throw an error while parsing JSON, but got: %s", err) 51 } 52 53 // We expect the invalid JSON to be shown back to us again. 54 if actual != invalidJson { 55 t.Fatalf("Got:\n\n%s\n\nExpected:\n\n%s\n", expected, invalidJson) 56 } 57 }