add tests for individual validators

This commit is contained in:
Shreyas Goenka 2023-05-22 10:43:43 +02:00
parent 2125f256e5
commit 8a9e59df3e
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
5 changed files with 75 additions and 5 deletions

View File

@ -14,7 +14,6 @@ var initCmd = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
// initialize default value for config file path // initialize default value for config file path
// TODO: add a test for this
if configFile == "" { if configFile == "" {
configFile = filepath.Join(targetDir, template.ConfigFileName) configFile = filepath.Join(targetDir, template.ConfigFileName)
} }

View File

@ -26,8 +26,6 @@ func executeTemplate(config map[string]any, templateDefination string) (string,
return result.String(), nil return result.String(), nil
} }
// TODO: allow skipping directories
func generateFile(config map[string]any, pathTemplate, contentTemplate string) error { func generateFile(config map[string]any, pathTemplate, contentTemplate string) error {
// compute file content // compute file content
fileContent, err := executeTemplate(config, contentTemplate) fileContent, err := executeTemplate(config, contentTemplate)

View File

@ -74,7 +74,6 @@ func validateType(v any, fieldType FieldType) error {
return validateFunc(v) return validateFunc(v)
} }
// TODO: add validation check for regex for string types
func (schema Schema) ValidateConfig(config map[string]any) error { func (schema Schema) ValidateConfig(config map[string]any) error {
// validate types defined in config // validate types defined in config
for k, v := range config { for k, v := range config {

View File

@ -9,7 +9,6 @@ import (
type Validator func(v any) error type Validator func(v any) error
// TODO: refactor tests into individual tests for individual validators
func validateString(v any) error { func validateString(v any) error {
if _, ok := v.(string); !ok { if _, ok := v.(string); !ok {
return fmt.Errorf("expected type string, but value is %#v", v) return fmt.Errorf("expected type string, but value is %#v", v)

View File

@ -0,0 +1,75 @@
package template
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidatorString(t *testing.T) {
err := validateString("abc")
assert.NoError(t, err)
err = validateString(1)
assert.ErrorContains(t, err, "expected type string, but value is 1")
err = validateString(true)
assert.ErrorContains(t, err, "expected type string, but value is true")
err = validateString("false")
assert.NoError(t, err)
}
func TestValidatorBoolean(t *testing.T) {
err := validateBoolean(true)
assert.NoError(t, err)
err = validateBoolean(1)
assert.ErrorContains(t, err, "expected type boolean, but value is 1")
err = validateBoolean("abc")
assert.ErrorContains(t, err, "expected type boolean, but value is \"abc\"")
err = validateBoolean("false")
assert.ErrorContains(t, err, "expected type boolean, but value is \"false\"")
}
func TestValidatorFloat(t *testing.T) {
err := validateFloat(true)
assert.ErrorContains(t, err, "expected type float, but value is true")
err = validateFloat(int32(1))
assert.ErrorContains(t, err, "expected type float, but value is 1")
err = validateFloat(int64(1))
assert.ErrorContains(t, err, "expected type float, but value is 1")
err = validateFloat(float32(1))
assert.NoError(t, err)
err = validateFloat(float64(1))
assert.NoError(t, err)
err = validateFloat("abc")
assert.ErrorContains(t, err, "expected type float, but value is \"abc\"")
}
func TestValidatorInt(t *testing.T) {
err := validateInteger(true)
assert.ErrorContains(t, err, "expected type integer, but value is true")
err = validateInteger(int32(1))
assert.NoError(t, err)
err = validateInteger(int64(1))
assert.NoError(t, err)
err = validateInteger(float32(1))
assert.ErrorContains(t, err, "expected type integer, but value is 1")
err = validateInteger(float64(1))
assert.ErrorContains(t, err, "expected type integer, but value is 1")
err = validateInteger("abc")
assert.ErrorContains(t, err, "expected type integer, but value is \"abc\"")
}