add tests for the new types (int8, int16, int32, uint, uint8, uint16, uint32, uint64)

Former-commit-id: 812b3fdcc47abdeac271473bfdbdd15f0afd0bc0
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-09-29 04:35:09 +03:00
parent 6d9a35ddba
commit 7568da3283
5 changed files with 176 additions and 10 deletions

View File

@ -114,9 +114,15 @@ func main() {
| Param Type | Go Type | Validation | Retrieve Helper |
| -----------------|------|-------------|------|
| `:string` | string | anything | `Params().Get` |
| `:int` | uint, uint8, uint16, uint32, uint64, int, int8, int32, int64 | positive or negative number, depends on the arch | `Params().GetInt`...|
| `:int` | int | -9223372036854775808 to 9223372036854775807 (x64) or -2147483648 to 2147483647 (x32), depends on the host arch | `Params().GetInt` |
| `:int8` | int8 | -128 to 127 | `Params().GetInt8` |
| `:int16` | int16 | -32768 to 32767 | `Params().GetInt16` |
| `:int32` | int32 | -2147483648 to 2147483647 | `Params().GetInt32` |
| `:int64` | int64 | -9223372036854775808 to 9223372036854775807 | `Params().GetInt64` |
| `:uint` | uint | 0 to 18446744073709551615 (x64) or 0 to 4294967295 (x32), depends on the host arch | `Params().GetUint` |
| `:uint8` | uint8 | 0 to 255 | `Params().GetUint8` |
| `:uint16` | uint16 | 0 to 65535 | `Params().GetUint16` |
| `:uint32` | uint32 | 0 to 4294967295 | `Params().GetUint32` |
| `:uint64` | uint64 | 0 to 18446744073709551615 | `Params().GetUint64` |
| `:bool` | bool | "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False" | `Params().GetBool` |
| `:alphabetical` | string | lowercase or uppercase letters | `Params().Get` |
@ -138,9 +144,9 @@ app.Get("/users/{id:uint64}", func(ctx iris.Context){
| `prefix`(prefix string) | :string |
| `suffix`(suffix string) | :string |
| `contains`(s string) | :string |
| `min`(minValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) | :string(char length), :number, :int64, :uint8, :uint64 |
| `max`(maxValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) | :string(char length), :number, :int64, :uint8, :uint64 |
| `range`(minValue, maxValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) | :number, :int64, :uint8, :uint64 |
| `min`(minValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) | :string(char length), :int, :int8, :int16, :int32, :int64, :uint, :uint8, :uint16, :uint32, :uint64 |
| `max`(maxValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) | :string(char length), :int, :int8, :int16, :int32, :int64, :uint, :uint8, :uint16, :uint32, :uint64 |
| `range`(minValue, maxValue int or int8 or int16 or int32 or int64 or uint8 or uint16 or uint32 or uint64 or float32 or float64) | :int, :int8, :int16, :int32, :int64, :uint, :uint8, :uint16, :uint32, :uint64 |
**Usage**:

View File

@ -33,7 +33,7 @@ func WildcardParam(name string) string {
return prefix(name, WildcardParamStart)
}
func convertTmplToNodePath(tmpl *macro.Template) string {
func convertMacroTmplToNodePath(tmpl *macro.Template) string {
routePath := tmpl.Src
if len(tmpl.Params) > 0 {
if routePath[len(routePath)-1] == '/' {

View File

@ -47,7 +47,7 @@ func NewRoute(method, subdomain, unparsedPath, mainHandlerName string,
return nil, err
}
path := convertTmplToNodePath(tmpl)
path := convertMacroTmplToNodePath(tmpl)
// prepend the macro handler to the route, now,
// right before the register to the tree, so APIBuilder#UseGlobal will work as expected.
if macroEvaluatorHandler, ok := handler.MakeHandler(tmpl); ok {

View File

@ -130,6 +130,83 @@ func TestIntEvaluatorRaw(t *testing.T) {
}
}
func TestInt8EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{false, "32321"}, // 2
{true, "127" /* max int8 */}, // 3
{true, "-128" /* min int8 */}, // 4
{false, "128"}, // 5
{false, "-129"}, // 6
{false, "-18446744073709553213213213213213121615"}, // 7
{false, "42 18446744073709551615"}, // 8
{false, "--42"}, // 9
{false, "+42"}, // 10
{false, "main.css"}, // 11
{false, "/assets/main.css"}, // 12
}
for i, tt := range tests {
testEvaluatorRaw(t, Int8, tt.input, reflect.Int8, tt.pass, i)
}
}
func TestInt16EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{true, "32767" /* max int16 */}, // 3
{true, "-32768" /* min int16 */}, // 4
{false, "-32769"}, // 5
{false, "32768"}, // 6
{false, "-18446744073709553213213213213213121615"}, // 7
{false, "42 18446744073709551615"}, // 8
{false, "--42"}, // 9
{false, "+42"}, // 10
{false, "main.css"}, // 11
{false, "/assets/main.css"}, // 12
}
for i, tt := range tests {
testEvaluatorRaw(t, Int16, tt.input, reflect.Int16, tt.pass, i)
}
}
func TestInt32EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{true, "1"}, // 3
{true, "42"}, // 4
{true, "2147483647" /* max int32 */}, // 5
{true, "-2147483648" /* min int32 */}, // 6
{false, "-2147483649"}, // 7
{false, "2147483648"}, // 8
{false, "-18446744073709553213213213213213121615"}, // 9
{false, "42 18446744073709551615"}, // 10
{false, "--42"}, // 11
{false, "+42"}, // 12
{false, "main.css"}, // 13
{false, "/assets/main.css"}, // 14
}
for i, tt := range tests {
testEvaluatorRaw(t, Int32, tt.input, reflect.Int32, tt.pass, i)
}
}
func TestInt64EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
@ -155,6 +232,35 @@ func TestInt64EvaluatorRaw(t *testing.T) {
}
}
func TestUintEvaluatorRaw(t *testing.T) {
x64 := strconv.IntSize == 64
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{true, "1"}, // 3
{true, "42"}, // 4
{x64, "18446744073709551615" /* max uint64 */}, // 5
{true, "4294967295" /* max uint32 */}, // 6
{false, "-2147483649"}, // 7
{true, "2147483648"}, // 8
{false, "-18446744073709553213213213213213121615"}, // 9
{false, "42 18446744073709551615"}, // 10
{false, "--42"}, // 11
{false, "+42"}, // 12
{false, "main.css"}, // 13
{false, "/assets/main.css"}, // 14
}
for i, tt := range tests {
testEvaluatorRaw(t, Uint, tt.input, reflect.Uint, tt.pass, i)
}
}
func TestUint8EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
@ -184,6 +290,58 @@ func TestUint8EvaluatorRaw(t *testing.T) {
}
}
func TestUint16EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{true, "65535" /* max uint16 */}, // 3
{true, "0" /* min uint16 */}, // 4
{false, "-32769"}, // 5
{true, "32768"}, // 6
{false, "-18446744073709553213213213213213121615"}, // 7
{false, "42 18446744073709551615"}, // 8
{false, "--42"}, // 9
{false, "+42"}, // 10
{false, "main.css"}, // 11
{false, "/assets/main.css"}, // 12
}
for i, tt := range tests {
testEvaluatorRaw(t, Uint16, tt.input, reflect.Uint16, tt.pass, i)
}
}
func TestUint32EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{true, "1"}, // 3
{true, "42"}, // 4
{true, "4294967295" /* max uint32*/}, // 5
{true, "0" /* min uint32 */}, // 6
{false, "-2147483649"}, // 7
{true, "2147483648"}, // 8
{false, "-18446744073709553213213213213213121615"}, // 9
{false, "42 18446744073709551615"}, // 10
{false, "--42"}, // 11
{false, "+42"}, // 12
{false, "main.css"}, // 13
{false, "/assets/main.css"}, // 14
}
for i, tt := range tests {
testEvaluatorRaw(t, Uint32, tt.input, reflect.Uint32, tt.pass, i)
}
}
func TestUint64EvaluatorRaw(t *testing.T) {
tests := []struct {
pass bool

View File

@ -48,6 +48,8 @@ var (
simpleNumberEval = MustRegexp("^-?[0-9]+$")
// Int or number type
// both positive and negative numbers, actual value can be min-max int64 or min-max int32 depends on the arch.
// If x64: -9223372036854775808 to 9223372036854775807.
// If x32: -2147483648 to 2147483647 and etc..
Int = NewMacro("int", "number", false, false, func(paramValue string) (interface{}, bool) {
if !simpleNumberEval(paramValue) {
return nil, false
@ -206,8 +208,8 @@ var (
// Uint as uint type
// actual value can be min-max uint64 or min-max uint32 depends on the arch.
// if x64: 0 to 18446744073709551615
// if x32: 0 to 4294967295 and etc.
// If x64: 0 to 18446744073709551615.
// If x32: 0 to 4294967295 and etc.
Uint = NewMacro("uint", "", false, false, func(paramValue string) (interface{}, bool) {
v, err := strconv.ParseUint(paramValue, 10, strconv.IntSize) // 32,64...
if err != nil {