2023-05-22 08:43:43 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-23 17:25:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-22 08:43:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2023-05-23 17:25:51 +00:00
|
|
|
err := validateNumber(true)
|
2023-05-22 08:43:43 +00:00
|
|
|
assert.ErrorContains(t, err, "expected type float, but value is true")
|
|
|
|
|
2023-05-23 17:25:51 +00:00
|
|
|
err = validateNumber(int32(1))
|
|
|
|
require.NoError(t, err)
|
2023-05-22 08:43:43 +00:00
|
|
|
|
2023-05-23 17:25:51 +00:00
|
|
|
err = validateNumber(int64(1))
|
|
|
|
require.NoError(t, err)
|
2023-05-22 08:43:43 +00:00
|
|
|
|
2023-05-23 17:25:51 +00:00
|
|
|
err = validateNumber(float32(1))
|
2023-05-22 08:43:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-05-23 17:25:51 +00:00
|
|
|
err = validateNumber(float64(1))
|
2023-05-22 08:43:43 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-05-23 17:25:51 +00:00
|
|
|
err = validateNumber("abc")
|
2023-05-22 08:43:43 +00:00
|
|
|
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\"")
|
|
|
|
}
|