github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/azurerm/structure_test.go (about)

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