github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/meta_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  )
     7  
     8  func TestValidationNoZeroValues(t *testing.T) {
     9  	runTestCases(t, []testCase{
    10  		{
    11  			val: "foo",
    12  			f:   NoZeroValues,
    13  		},
    14  		{
    15  			val: 1,
    16  			f:   NoZeroValues,
    17  		},
    18  		{
    19  			val: float64(1),
    20  			f:   NoZeroValues,
    21  		},
    22  		{
    23  			val:         "",
    24  			f:           NoZeroValues,
    25  			expectedErr: regexp.MustCompile("must not be empty"),
    26  		},
    27  		{
    28  			val:         0,
    29  			f:           NoZeroValues,
    30  			expectedErr: regexp.MustCompile("must not be zero"),
    31  		},
    32  		{
    33  			val:         float64(0),
    34  			f:           NoZeroValues,
    35  			expectedErr: regexp.MustCompile("must not be zero"),
    36  		},
    37  	})
    38  }
    39  
    40  func TestValidationAll(t *testing.T) {
    41  	runTestCases(t, []testCase{
    42  		{
    43  			val: "valid",
    44  			f: All(
    45  				StringLenBetween(5, 42),
    46  				StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
    47  			),
    48  		},
    49  		{
    50  			val: "foo",
    51  			f: All(
    52  				StringLenBetween(5, 42),
    53  				StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
    54  			),
    55  			expectedErr: regexp.MustCompile("expected length of [\\w]+ to be in the range \\(5 - 42\\), got foo"),
    56  		},
    57  		{
    58  			val: "!!!!!",
    59  			f: All(
    60  				StringLenBetween(5, 42),
    61  				StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
    62  			),
    63  			expectedErr: regexp.MustCompile("value must be alphanumeric"),
    64  		},
    65  	})
    66  }
    67  
    68  func TestValidationAny(t *testing.T) {
    69  	runTestCases(t, []testCase{
    70  		{
    71  			val: 43,
    72  			f: Any(
    73  				IntAtLeast(42),
    74  				IntAtMost(5),
    75  			),
    76  		},
    77  		{
    78  			val: 4,
    79  			f: Any(
    80  				IntAtLeast(42),
    81  				IntAtMost(5),
    82  			),
    83  		},
    84  		{
    85  			val: 7,
    86  			f: Any(
    87  				IntAtLeast(42),
    88  				IntAtMost(5),
    89  			),
    90  			expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"),
    91  		},
    92  		{
    93  			val: 7,
    94  			f: Any(
    95  				IntAtLeast(42),
    96  				IntAtMost(5),
    97  			),
    98  			expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"),
    99  		},
   100  	})
   101  }