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

     1  package validation
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
     8  )
     9  
    10  func TestValidateFloatBetween(t *testing.T) {
    11  	cases := map[string]struct {
    12  		Value                  interface{}
    13  		ValidateFunc           schema.SchemaValidateFunc
    14  		ExpectValidationErrors bool
    15  	}{
    16  		"accept valid value": {
    17  			Value:                  1.5,
    18  			ValidateFunc:           FloatBetween(1.0, 2.0),
    19  			ExpectValidationErrors: false,
    20  		},
    21  		"accept valid value inclusive upper bound": {
    22  			Value:                  1.0,
    23  			ValidateFunc:           FloatBetween(0.0, 1.0),
    24  			ExpectValidationErrors: false,
    25  		},
    26  		"accept valid value inclusive lower bound": {
    27  			Value:                  0.0,
    28  			ValidateFunc:           FloatBetween(0.0, 1.0),
    29  			ExpectValidationErrors: false,
    30  		},
    31  		"reject out of range value": {
    32  			Value:                  -1.0,
    33  			ValidateFunc:           FloatBetween(0.0, 1.0),
    34  			ExpectValidationErrors: true,
    35  		},
    36  		"reject incorrectly typed value": {
    37  			Value:                  1,
    38  			ValidateFunc:           FloatBetween(0.0, 1.0),
    39  			ExpectValidationErrors: true,
    40  		},
    41  	}
    42  
    43  	for tn, tc := range cases {
    44  		_, errors := tc.ValidateFunc(tc.Value, tn)
    45  		if len(errors) > 0 && !tc.ExpectValidationErrors {
    46  			t.Errorf("%s: unexpected errors %s", tn, errors)
    47  		} else if len(errors) == 0 && tc.ExpectValidationErrors {
    48  			t.Errorf("%s: expected errors but got none", tn)
    49  		}
    50  	}
    51  }
    52  
    53  func TestValidateFloatAtLeast(t *testing.T) {
    54  	runTestCases(t, []testCase{
    55  		{
    56  			val: 2.5,
    57  			f:   FloatAtLeast(1.5),
    58  		},
    59  		{
    60  			val: -1.0,
    61  			f:   FloatAtLeast(-1.5),
    62  		},
    63  		{
    64  			val:         1.5,
    65  			f:           FloatAtLeast(2.5),
    66  			expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\.5\\d*\\), got 1\\.5\\d*"),
    67  		},
    68  		{
    69  			val:         "2.5",
    70  			f:           FloatAtLeast(1.5),
    71  			expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"),
    72  		},
    73  	})
    74  }
    75  
    76  func TestValidateFloatAtMost(t *testing.T) {
    77  	runTestCases(t, []testCase{
    78  		{
    79  			val: 2.5,
    80  			f:   FloatAtMost(3.5),
    81  		},
    82  		{
    83  			val: -1.0,
    84  			f:   FloatAtMost(-0.5),
    85  		},
    86  		{
    87  			val:         2.5,
    88  			f:           FloatAtMost(1.5),
    89  			expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(1\\.5\\d*\\), got 2\\.5\\d*"),
    90  		},
    91  		{
    92  			val:         "2.5",
    93  			f:           FloatAtMost(3.5),
    94  			expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"),
    95  		},
    96  	})
    97  }