-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// FloatAtLeast returns a SchemaValidateFunc which tests if the provided value | ||
// is of type float64 and is at least min (inclusive) | ||
func FloatAtLeast(min float64) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (_ []string, errors []error) { | ||
v, ok := i.(float64) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %s to be float64", k)) | ||
return nil, errors | ||
} | ||
|
||
if v < min { | ||
errors = append(errors, fmt.Errorf("expected %s to be at least (%f), got %f", k, min, v)) | ||
return nil, errors | ||
} | ||
|
||
return nil, errors | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package validate | ||
|
||
import "testing" | ||
|
||
func TestAzureFloatAtLeast(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
MinValue float64 | ||
ActualValue float64 | ||
Errors int | ||
}{ | ||
{ | ||
Name: "Min_Full_Stop_Zero_Greater", | ||
MinValue: 0.0, | ||
ActualValue: 1.0, | ||
Errors: 0, | ||
}, | ||
{ | ||
Name: "Min_One_Full_Stop_Zero_Lesser", | ||
MinValue: 1.0, | ||
ActualValue: 0.0, | ||
Errors: 1, | ||
}, | ||
{ | ||
Name: "Min_Full_Stop_Two_Five_Greater", | ||
MinValue: 0.25, | ||
ActualValue: 0.26, | ||
Errors: 0, | ||
}, | ||
{ | ||
Name: "Min_Full_Stop_Two_Five_Equal", | ||
MinValue: 0.25, | ||
ActualValue: 0.25, | ||
Errors: 0, | ||
}, | ||
{ | ||
Name: "Min_Full_Stop_Two_Five_Lesser", | ||
MinValue: 0.25, | ||
ActualValue: 0.24, | ||
Errors: 1, | ||
}, | ||
{ | ||
Name: "Min_Full_Stop_Long_Zero_Lesser", | ||
MinValue: 0.0000000000000000000000000000000000000001, | ||
ActualValue: 0, | ||
Errors: 1, | ||
}, | ||
{ | ||
Name: "Min_Full_Stop_Long_Greater", | ||
MinValue: 0.0000000000000000000000000000000000000001, | ||
ActualValue: -0, | ||
Errors: 1, | ||
}, | ||
{ | ||
Name: "Min_Negative_Full_Stop_Two_Five_Greater", | ||
MinValue: -0.25, | ||
ActualValue: 1, | ||
Errors: 0, | ||
}, | ||
{ | ||
Name: "Min_Zero_No_Full_Stop_Equal", | ||
MinValue: 0, | ||
ActualValue: -0, | ||
Errors: 0, | ||
}, | ||
{ | ||
Name: "Min_Negative_Full_Stop_Two_Five_Lesser", | ||
MinValue: -0.25, | ||
ActualValue: -0.26, | ||
Errors: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
_, errors := FloatAtLeast(tc.MinValue)(tc.ActualValue, "floatValue") | ||
|
||
if len(errors) < tc.Errors { | ||
t.Fatalf("Expected FloatAtLeast to have %d not %d errors for %q", tc.Errors, len(errors), tc.Name) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters