59 lines
951 B
Go
59 lines
951 B
Go
![]() |
package azurerm
|
||
|
|
||
|
import "testing"
|
||
|
|
||
|
func TestValidateJsonString(t *testing.T) {
|
||
|
type testCases struct {
|
||
|
Value string
|
||
|
ErrCount int
|
||
|
}
|
||
|
|
||
|
invalidCases := []testCases{
|
||
|
{
|
||
|
Value: `{0:"1"}`,
|
||
|
ErrCount: 1,
|
||
|
},
|
||
|
{
|
||
|
Value: `{'abc':1}`,
|
||
|
ErrCount: 1,
|
||
|
},
|
||
|
{
|
||
|
Value: `{"def":}`,
|
||
|
ErrCount: 1,
|
||
|
},
|
||
|
{
|
||
|
Value: `{"xyz":[}}`,
|
||
|
ErrCount: 1,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tc := range invalidCases {
|
||
|
_, errors := validateJsonString(tc.Value, "json")
|
||
|
if len(errors) != tc.ErrCount {
|
||
|
t.Fatalf("Expected %q to trigger a validation error.", tc.Value)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
validCases := []testCases{
|
||
|
{
|
||
|
Value: ``,
|
||
|
ErrCount: 0,
|
||
|
},
|
||
|
{
|
||
|
Value: `{}`,
|
||
|
ErrCount: 0,
|
||
|
},
|
||
|
{
|
||
|
Value: `{"abc":["1","2"]}`,
|
||
|
ErrCount: 0,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for _, tc := range validCases {
|
||
|
_, errors := validateJsonString(tc.Value, "json")
|
||
|
if len(errors) != tc.ErrCount {
|
||
|
t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
|
||
|
}
|
||
|
}
|
||
|
}
|