github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/validators_test.go (about) 1 package azurerm 2 3 import "testing" 4 5 func TestValidateJsonString(t *testing.T) { 6 type testCases struct { 7 Value string 8 ErrCount int 9 } 10 11 invalidCases := []testCases{ 12 { 13 Value: `{0:"1"}`, 14 ErrCount: 1, 15 }, 16 { 17 Value: `{'abc':1}`, 18 ErrCount: 1, 19 }, 20 { 21 Value: `{"def":}`, 22 ErrCount: 1, 23 }, 24 { 25 Value: `{"xyz":[}}`, 26 ErrCount: 1, 27 }, 28 } 29 30 for _, tc := range invalidCases { 31 _, errors := validateJsonString(tc.Value, "json") 32 if len(errors) != tc.ErrCount { 33 t.Fatalf("Expected %q to trigger a validation error.", tc.Value) 34 } 35 } 36 37 validCases := []testCases{ 38 { 39 Value: ``, 40 ErrCount: 0, 41 }, 42 { 43 Value: `{}`, 44 ErrCount: 0, 45 }, 46 { 47 Value: `{"abc":["1","2"]}`, 48 ErrCount: 0, 49 }, 50 } 51 52 for _, tc := range validCases { 53 _, errors := validateJsonString(tc.Value, "json") 54 if len(errors) != tc.ErrCount { 55 t.Fatalf("Expected %q not to trigger a validation error.", tc.Value) 56 } 57 } 58 }