From 7568da3283aa19fbc6a269a9326f224f2fad2510 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 29 Sep 2018 04:35:09 +0300 Subject: [PATCH] add tests for the new types (int8, int16, int32, uint, uint8, uint16, uint32, uint64) Former-commit-id: 812b3fdcc47abdeac271473bfdbdd15f0afd0bc0 --- README.md | 14 ++-- core/router/path.go | 2 +- core/router/route.go | 2 +- macro/macro_test.go | 162 ++++++++++++++++++++++++++++++++++++++++++- macro/macros.go | 6 +- 5 files changed, 176 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 49125deb..23b860e9 100644 --- a/README.md +++ b/README.md @@ -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**: diff --git a/core/router/path.go b/core/router/path.go index 9b9d12a9..05ba11c3 100644 --- a/core/router/path.go +++ b/core/router/path.go @@ -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] == '/' { diff --git a/core/router/route.go b/core/router/route.go index c5c273e1..5e0e6ef0 100644 --- a/core/router/route.go +++ b/core/router/route.go @@ -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 { diff --git a/macro/macro_test.go b/macro/macro_test.go index 020f4d74..ff25c817 100644 --- a/macro/macro_test.go +++ b/macro/macro_test.go @@ -115,8 +115,8 @@ func TestIntEvaluatorRaw(t *testing.T) { {false, "astring"}, // 0 {false, "astringwith_numb3rS_and_symbol$"}, // 1 {true, "32321"}, // 2 - {x64, "9223372036854775807" /*max int64*/}, // 3 - {x64, "-9223372036854775808" /*min int64 */}, // 4 + {x64, "9223372036854775807" /* max int64 */}, // 3 + {x64, "-9223372036854775808" /* min int64 */}, // 4 {false, "-18446744073709553213213213213213121615"}, // 5 {false, "42 18446744073709551615"}, // 6 {false, "--42"}, // 7 @@ -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 diff --git a/macro/macros.go b/macro/macros.go index b1d9a96b..c92c0202 100644 --- a/macro/macros.go +++ b/macro/macros.go @@ -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 {