mirror of
https://github.com/kataras/iris.git
synced 2026-01-24 04:15:56 +00:00
add the stale release
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
"github.com/kataras/iris/v12/macro"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/memstore"
|
||||
"github.com/kataras/iris/macro"
|
||||
)
|
||||
|
||||
// CanMakeHandler reports whether a macro template needs a special macro's evaluator handler to be validated
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/macro"
|
||||
)
|
||||
|
||||
func TestCanMakeHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
src string
|
||||
needsHandler bool
|
||||
}{
|
||||
{"/static/static", false},
|
||||
{"/{myparam}", false},
|
||||
{"/{myparam min(1)}", true},
|
||||
{"/{myparam else 500}", true},
|
||||
{"/{myparam else 404}", false},
|
||||
{"/{myparam:string}/static", false},
|
||||
{"/{myparam:int}", true},
|
||||
{"/static/{myparam:int}/static", true},
|
||||
{"/{myparam:path}", false},
|
||||
{"/{myparam:path min(1) else 404}", true},
|
||||
}
|
||||
|
||||
availableMacros := *macro.Defaults
|
||||
for i, tt := range tests {
|
||||
tmpl, err := macro.Parse(tt.src, availableMacros)
|
||||
if err != nil {
|
||||
t.Fatalf("[%d] '%s' failed to be parsed: %v", i, tt.src, err)
|
||||
}
|
||||
|
||||
if got := CanMakeHandler(tmpl); got != tt.needsHandler {
|
||||
if tt.needsHandler {
|
||||
t.Fatalf("[%d] '%s' expected to be able to generate an evaluator handler instead of a nil one", i, tt.src)
|
||||
} else {
|
||||
t.Fatalf("[%d] '%s' should not need an evaluator handler", i, tt.src)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package lexer
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/macro/interpreter/token"
|
||||
"github.com/kataras/iris/macro/interpreter/token"
|
||||
)
|
||||
|
||||
// Lexer helps us to read/scan characters of a source and resolve their token types.
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package lexer
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/macro/interpreter/token"
|
||||
)
|
||||
|
||||
func TestNextToken(t *testing.T) {
|
||||
input := `{id:int min(1) max(5) else 404}`
|
||||
|
||||
tests := []struct {
|
||||
expectedType token.Type
|
||||
expectedLiteral string
|
||||
}{
|
||||
{token.LBRACE, "{"}, // 0
|
||||
{token.IDENT, "id"}, // 1
|
||||
{token.COLON, ":"}, // 2
|
||||
{token.IDENT, "int"}, // 3
|
||||
{token.IDENT, "min"}, // 4
|
||||
{token.LPAREN, "("}, // 5
|
||||
{token.INT, "1"}, // 6
|
||||
{token.RPAREN, ")"}, // 7
|
||||
{token.IDENT, "max"}, // 8
|
||||
{token.LPAREN, "("}, // 9
|
||||
{token.INT, "5"}, // 10
|
||||
{token.RPAREN, ")"}, // 11
|
||||
{token.ELSE, "else"}, // 12
|
||||
{token.INT, "404"}, // 13
|
||||
{token.RBRACE, "}"}, // 14
|
||||
}
|
||||
|
||||
l := New(input)
|
||||
|
||||
for i, tt := range tests {
|
||||
tok := l.NextToken()
|
||||
|
||||
if tok.Type != tt.expectedType {
|
||||
t.Fatalf("tests[%d] - tokentype wrong. expected=%q, got=%q",
|
||||
i, tt.expectedType, tok.Type)
|
||||
}
|
||||
|
||||
if tok.Literal != tt.expectedLiteral {
|
||||
t.Fatalf("tests[%d] - literal wrong. expected=%q, got=%q",
|
||||
i, tt.expectedLiteral, tok.Literal)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// EMEINA STO:
|
||||
// 30/232 selida apto making a interpeter in Go.
|
||||
// den ekana to skipWhitespaces giati skeftomai
|
||||
// an borei na to xreiastw 9a dw aurio.
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||
"github.com/kataras/iris/v12/macro/interpreter/lexer"
|
||||
"github.com/kataras/iris/v12/macro/interpreter/token"
|
||||
"github.com/kataras/iris/macro/interpreter/ast"
|
||||
"github.com/kataras/iris/macro/interpreter/lexer"
|
||||
"github.com/kataras/iris/macro/interpreter/token"
|
||||
)
|
||||
|
||||
// Parse takes a route "fullpath"
|
||||
|
||||
@@ -1,399 +0,0 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||
)
|
||||
|
||||
type simpleParamType string
|
||||
|
||||
func (pt simpleParamType) Indent() string { return string(pt) }
|
||||
|
||||
type masterParamType simpleParamType
|
||||
|
||||
func (pt masterParamType) Indent() string { return string(pt) }
|
||||
func (pt masterParamType) Master() bool { return true }
|
||||
|
||||
type wildcardParamType string
|
||||
|
||||
func (pt wildcardParamType) Indent() string { return string(pt) }
|
||||
func (pt wildcardParamType) Trailing() bool { return true }
|
||||
|
||||
type aliasedParamType []string
|
||||
|
||||
func (pt aliasedParamType) Indent() string { return string(pt[0]) }
|
||||
func (pt aliasedParamType) Alias() string { return pt[1] }
|
||||
|
||||
var (
|
||||
paramTypeString = masterParamType("string")
|
||||
paramTypeNumber = aliasedParamType{"number", "int"}
|
||||
paramTypeInt64 = aliasedParamType{"int64", "long"}
|
||||
paramTypeUint8 = simpleParamType("uint8")
|
||||
paramTypeUint64 = simpleParamType("uint64")
|
||||
paramTypeBool = aliasedParamType{"bool", "boolean"}
|
||||
paramTypeAlphabetical = simpleParamType("alphabetical")
|
||||
paramTypeFile = simpleParamType("file")
|
||||
paramTypePath = wildcardParamType("path")
|
||||
)
|
||||
|
||||
var testParamTypes = []ast.ParamType{
|
||||
paramTypeString,
|
||||
paramTypeNumber, paramTypeInt64, paramTypeUint8, paramTypeUint64,
|
||||
paramTypeBool,
|
||||
paramTypeAlphabetical, paramTypeFile, paramTypePath,
|
||||
}
|
||||
|
||||
func TestParseParamError(t *testing.T) {
|
||||
// fail
|
||||
illegalChar := '$'
|
||||
|
||||
input := "{id" + string(illegalChar) + "int range(1,5) else 404}"
|
||||
p := NewParamParser(input)
|
||||
|
||||
_, err := p.Parse(testParamTypes)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("expecting not empty error on input '%s'", input)
|
||||
}
|
||||
|
||||
illIdx := strings.IndexRune(input, illegalChar)
|
||||
expectedErr := fmt.Sprintf("[%d:%d] illegal token: %s", illIdx, illIdx, "$")
|
||||
if got := err.Error(); got != expectedErr {
|
||||
t.Fatalf("expecting error to be '%s' but got: %s", expectedErr, got)
|
||||
}
|
||||
//
|
||||
|
||||
// success
|
||||
input2 := "{id:uint64 range(1,5) else 404}"
|
||||
p.Reset(input2)
|
||||
_, err = p.Parse(testParamTypes)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expecting empty error on input '%s', but got: %s", input2, err.Error())
|
||||
}
|
||||
//
|
||||
}
|
||||
|
||||
// mustLookupParamType same as `ast.LookupParamType` but it panics if "indent" does not match with a valid Param Type.
|
||||
func mustLookupParamType(indent string) ast.ParamType {
|
||||
pt, found := ast.LookupParamType(indent, testParamTypes...)
|
||||
if !found {
|
||||
panic("param type '" + indent + "' is not part of the provided param types")
|
||||
}
|
||||
|
||||
return pt
|
||||
}
|
||||
|
||||
func TestParseParam(t *testing.T) {
|
||||
tests := []struct {
|
||||
valid bool
|
||||
expectedStatement ast.ParamStatement
|
||||
}{
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{id:int min(1) max(5) else 404}",
|
||||
Name: "id",
|
||||
Type: mustLookupParamType("number"),
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "min",
|
||||
Args: []string{"1"},
|
||||
},
|
||||
{
|
||||
Name: "max",
|
||||
Args: []string{"5"},
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 0
|
||||
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
// test alias of int.
|
||||
Src: "{id:number range(1,5)}",
|
||||
Name: "id",
|
||||
Type: mustLookupParamType("number"),
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "range",
|
||||
Args: []string{"1", "5"},
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 1
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{file:path contains(.)}",
|
||||
Name: "file",
|
||||
Type: mustLookupParamType("path"),
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "contains",
|
||||
Args: []string{"."},
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 2
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{username:alphabetical}",
|
||||
Name: "username",
|
||||
Type: mustLookupParamType("alphabetical"),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 3
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{myparam}",
|
||||
Name: "myparam",
|
||||
Type: mustLookupParamType("string"),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 4
|
||||
{
|
||||
false,
|
||||
ast.ParamStatement{
|
||||
Src: "{myparam_:thisianunexpected}",
|
||||
Name: "myparam_",
|
||||
Type: nil,
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 5
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{myparam2}",
|
||||
Name: "myparam2", // we now allow integers to the parameter names.
|
||||
Type: ast.GetMasterParamType(testParamTypes...),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 6
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{id:int even()}", // test param funcs without any arguments (LPAREN peek for RPAREN)
|
||||
Name: "id",
|
||||
Type: mustLookupParamType("number"),
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "even",
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 7
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{id:int64 else 404}",
|
||||
Name: "id",
|
||||
Type: mustLookupParamType("int64"),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 8
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{id:long else 404}", // backwards-compatible test.
|
||||
Name: "id",
|
||||
Type: mustLookupParamType("int64"),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 9
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{id:long else 404}",
|
||||
Name: "id",
|
||||
Type: mustLookupParamType("int64"), // backwards-compatible test of LookupParamType.
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 10
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{has:bool else 404}",
|
||||
Name: "has",
|
||||
Type: mustLookupParamType("bool"),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 11
|
||||
{
|
||||
true,
|
||||
ast.ParamStatement{
|
||||
Src: "{has:boolean else 404}", // backwards-compatible test.
|
||||
Name: "has",
|
||||
Type: mustLookupParamType("bool"),
|
||||
ErrorCode: 404,
|
||||
},
|
||||
}, // 12
|
||||
|
||||
}
|
||||
|
||||
p := new(ParamParser)
|
||||
for i, tt := range tests {
|
||||
p.Reset(tt.expectedStatement.Src)
|
||||
resultStmt, err := p.Parse(testParamTypes)
|
||||
|
||||
if tt.valid && err != nil {
|
||||
t.Fatalf("tests[%d] - error %s", i, err.Error())
|
||||
} else if !tt.valid && err == nil {
|
||||
t.Fatalf("tests[%d] - expected to be a failure", i)
|
||||
}
|
||||
|
||||
if resultStmt != nil { // is valid here
|
||||
if !reflect.DeepEqual(tt.expectedStatement, *resultStmt) {
|
||||
t.Fatalf("tests[%d] - wrong statement, expected and result differs. Details:\n%#v\n%#v", i, tt.expectedStatement, *resultStmt)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
valid bool
|
||||
expectedStatements []ast.ParamStatement
|
||||
}{
|
||||
{
|
||||
"/api/users/{id:int min(1) max(5) else 404}", true,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{id:int min(1) max(5) else 404}",
|
||||
Name: "id",
|
||||
Type: paramTypeNumber,
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "min",
|
||||
Args: []string{"1"},
|
||||
},
|
||||
{
|
||||
Name: "max",
|
||||
Args: []string{"5"},
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 0
|
||||
{
|
||||
"/admin/{id:uint64 range(1,5)}", true,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{id:uint64 range(1,5)}",
|
||||
Name: "id",
|
||||
Type: paramTypeUint64,
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "range",
|
||||
Args: []string{"1", "5"},
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 1
|
||||
{
|
||||
"/files/{file:path contains(.)}", true,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{file:path contains(.)}",
|
||||
Name: "file",
|
||||
Type: paramTypePath,
|
||||
Funcs: []ast.ParamFunc{
|
||||
{
|
||||
Name: "contains",
|
||||
Args: []string{"."},
|
||||
},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 2
|
||||
{
|
||||
"/profile/{username:alphabetical}", true,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{username:alphabetical}",
|
||||
Name: "username",
|
||||
Type: paramTypeAlphabetical,
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 3
|
||||
{
|
||||
"/something/here/{myparam}", true,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{myparam}",
|
||||
Name: "myparam",
|
||||
Type: paramTypeString,
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 4
|
||||
{
|
||||
"/unexpected/{myparam_:thisianunexpected}", false,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{myparam_:thisianunexpected}",
|
||||
Name: "myparam_",
|
||||
Type: nil,
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 5
|
||||
{
|
||||
"/p2/{myparam2}", true,
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{myparam2}",
|
||||
Name: "myparam2", // we now allow integers to the parameter names.
|
||||
Type: paramTypeString,
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 6
|
||||
{
|
||||
"/assets/{file:path}/invalid", false, // path should be in the end segment
|
||||
[]ast.ParamStatement{
|
||||
{
|
||||
Src: "{file:path}",
|
||||
Name: "file",
|
||||
Type: paramTypePath,
|
||||
ErrorCode: 404,
|
||||
},
|
||||
},
|
||||
}, // 7
|
||||
}
|
||||
for i, tt := range tests {
|
||||
statements, err := Parse(tt.path, testParamTypes)
|
||||
|
||||
if tt.valid && err != nil {
|
||||
t.Fatalf("tests[%d] - error %s", i, err.Error())
|
||||
} else if !tt.valid && err == nil {
|
||||
t.Fatalf("tests[%d] - expected to be a failure", i)
|
||||
}
|
||||
for j := range statements {
|
||||
for l := range tt.expectedStatements {
|
||||
if !reflect.DeepEqual(tt.expectedStatements[l], *statements[j]) {
|
||||
t.Fatalf("tests[%d] - wrong statements, expected and result differs. Details:\n%#v\n%#v", i, tt.expectedStatements[l], *statements[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,471 +0,0 @@
|
||||
package macro
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Most important tests to look:
|
||||
// ../parser/parser_test.go
|
||||
// ../lexer/lexer_test.go
|
||||
|
||||
func TestGoodParamFunc(t *testing.T) {
|
||||
good1 := func(min int, max int) func(string) bool {
|
||||
return func(paramValue string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
good2 := func(min uint64, max uint64) func(string) bool {
|
||||
return func(paramValue string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
notgood1 := func(min int, max int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if !goodParamFunc(reflect.TypeOf(good1)) {
|
||||
t.Fatalf("expected good1 func to be good but it's not")
|
||||
}
|
||||
|
||||
if !goodParamFunc(reflect.TypeOf(good2)) {
|
||||
t.Fatalf("expected good2 func to be good but it's not")
|
||||
}
|
||||
|
||||
if goodParamFunc(reflect.TypeOf(notgood1)) {
|
||||
t.Fatalf("expected notgood1 func to be the worst")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodParamFuncName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
good bool
|
||||
}{
|
||||
{"range", true},
|
||||
{"_range", true},
|
||||
{"range_", true},
|
||||
{"r_ange", true},
|
||||
// numbers or other symbols are invalid.
|
||||
{"range1", false},
|
||||
{"2range", false},
|
||||
{"r@nge", false},
|
||||
{"rang3", false},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
isGood := goodParamFuncName(tt.name)
|
||||
if tt.good && !isGood {
|
||||
t.Fatalf("tests[%d] - expecting valid name but got invalid for name %s", i, tt.name)
|
||||
} else if !tt.good && isGood {
|
||||
t.Fatalf("tests[%d] - expecting invalid name but got valid for name %s", i, tt.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testEvaluatorRaw(t *testing.T, macroEvaluator *Macro, input string, expectedType reflect.Kind, pass bool, i int) {
|
||||
if macroEvaluator.Evaluator == nil && pass {
|
||||
return // if not evaluator defined then it should allow everything.
|
||||
}
|
||||
value, passed := macroEvaluator.Evaluator(input)
|
||||
if pass != passed {
|
||||
t.Fatalf("%s - tests[%d] - expecting[pass] %v but got %v", t.Name(), i, pass, passed)
|
||||
}
|
||||
|
||||
if !passed {
|
||||
return
|
||||
}
|
||||
|
||||
if value == nil && expectedType != reflect.Invalid {
|
||||
t.Fatalf("%s - tests[%d] - expecting[value] to not be nil", t.Name(), i)
|
||||
}
|
||||
|
||||
if v := reflect.ValueOf(value); v.Kind() != expectedType {
|
||||
t.Fatalf("%s - tests[%d] - expecting[value.Kind] %v but got %v", t.Name(), i, expectedType, v.Kind())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringEvaluatorRaw(t *testing.T) {
|
||||
tests := []struct {
|
||||
pass bool
|
||||
input string
|
||||
}{
|
||||
{true, "astring"}, // 0
|
||||
{true, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{true, "32321"}, // 2
|
||||
{true, "main.css"}, // 3
|
||||
{true, "/assets/main.css"}, // 4
|
||||
// false never
|
||||
} // 0
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, String, tt.input, reflect.String, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntEvaluatorRaw(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
|
||||
{x64, "9223372036854775807" /* max int64 */}, // 3
|
||||
{x64, "-9223372036854775808" /* min int64 */}, // 4
|
||||
{false, "-18446744073709553213213213213213121615"}, // 5
|
||||
{false, "42 18446744073709551615"}, // 6
|
||||
{false, "--42"}, // 7
|
||||
{false, "+42"}, // 8
|
||||
{false, "main.css"}, // 9
|
||||
{false, "/assets/main.css"}, // 10
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, Int, tt.input, reflect.Int, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
input string
|
||||
}{
|
||||
{false, "astring"}, // 0
|
||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{false, "18446744073709551615"}, // 2
|
||||
{false, "92233720368547758079223372036854775807"}, // 3
|
||||
{false, "9223372036854775808 9223372036854775808"}, // 4
|
||||
{false, "main.css"}, // 5
|
||||
{false, "/assets/main.css"}, // 6
|
||||
{true, "9223372036854775807"}, // 7
|
||||
{true, "-9223372036854775808"}, // 8
|
||||
{true, "-0"}, // 9
|
||||
{true, "1"}, // 10
|
||||
{true, "-042"}, // 11
|
||||
{true, "142"}, // 12
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, Int64, tt.input, reflect.Int64, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
input string
|
||||
}{
|
||||
{false, "astring"}, // 0
|
||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{false, "-9223372036854775808"}, // 2
|
||||
{false, "main.css"}, // 3
|
||||
{false, "/assets/main.css"}, // 4
|
||||
{false, "92233720368547758079223372036854775807"}, // 5
|
||||
{false, "9223372036854775808 9223372036854775808"}, // 6
|
||||
{false, "-1"}, // 7
|
||||
{false, "-0"}, // 8
|
||||
{false, "+1"}, // 9
|
||||
{false, "18446744073709551615"}, // 10
|
||||
{false, "9223372036854775807"}, // 11
|
||||
{false, "021"}, // 12 - no leading zeroes are allowed.
|
||||
{false, "300"}, // 13
|
||||
{true, "0"}, // 14
|
||||
{true, "255"}, // 15
|
||||
{true, "21"}, // 16
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, Uint8, tt.input, reflect.Uint8, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
input string
|
||||
}{
|
||||
{false, "astring"}, // 0
|
||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{false, "-9223372036854775808"}, // 2
|
||||
{false, "main.css"}, // 3
|
||||
{false, "/assets/main.css"}, // 4
|
||||
{false, "92233720368547758079223372036854775807"}, // 5
|
||||
{false, "9223372036854775808 9223372036854775808"}, // 6
|
||||
{false, "-1"}, // 7
|
||||
{false, "-0"}, // 8
|
||||
{false, "+1"}, // 9
|
||||
{true, "18446744073709551615"}, // 10
|
||||
{true, "9223372036854775807"}, // 11
|
||||
{true, "0"}, // 12
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, Uint64, tt.input, reflect.Uint64, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlphabeticalEvaluatorRaw(t *testing.T) {
|
||||
tests := []struct {
|
||||
pass bool
|
||||
input string
|
||||
}{
|
||||
{true, "astring"}, // 0
|
||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{false, "32321"}, // 2
|
||||
{false, "main.css"}, // 3
|
||||
{false, "/assets/main.css"}, // 4
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, Alphabetical, tt.input, reflect.String, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileEvaluatorRaw(t *testing.T) {
|
||||
tests := []struct {
|
||||
pass bool
|
||||
input string
|
||||
}{
|
||||
{true, "astring"}, // 0
|
||||
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{true, "32321"}, // 2
|
||||
{true, "main.css"}, // 3
|
||||
{false, "/assets/main.css"}, // 4
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
testEvaluatorRaw(t, File, tt.input, reflect.String, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathEvaluatorRaw(t *testing.T) {
|
||||
pathTests := []struct {
|
||||
pass bool
|
||||
input string
|
||||
}{
|
||||
{true, "astring"}, // 0
|
||||
{true, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||
{true, "32321"}, // 2
|
||||
{true, "main.css"}, // 3
|
||||
{true, "/assets/main.css"}, // 4
|
||||
{true, "disk/assets/main.css"}, // 5
|
||||
}
|
||||
|
||||
for i, tt := range pathTests {
|
||||
testEvaluatorRaw(t, Path, tt.input, reflect.String, tt.pass, i)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertBuilderFunc(t *testing.T) {
|
||||
fn := func(min uint64, slice []string) func(string) bool {
|
||||
return func(paramValue string) bool {
|
||||
if expected, got := "ok", paramValue; expected != got {
|
||||
t.Fatalf("paramValue is not the expected one: %s vs %s", expected, got)
|
||||
}
|
||||
|
||||
if expected, got := uint64(1), min; expected != got {
|
||||
t.Fatalf("min argument is not the expected one: %d vs %d", expected, got)
|
||||
}
|
||||
|
||||
if expected, got := []string{"name1", "name2"}, slice; len(expected) == len(got) {
|
||||
if expected, got := "name1", slice[0]; expected != got {
|
||||
t.Fatalf("slice argument[%d] does not contain the expected value: %s vs %s", 0, expected, got)
|
||||
}
|
||||
|
||||
if expected, got := "name2", slice[1]; expected != got {
|
||||
t.Fatalf("slice argument[%d] does not contain the expected value: %s vs %s", 1, expected, got)
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("slice argument is not the expected one, the length is difference: %d vs %d", len(expected), len(got))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
evalFunc := convertBuilderFunc(fn)
|
||||
if !evalFunc([]string{"1", "[name1,name2]"}).Call([]reflect.Value{reflect.ValueOf("ok")})[0].Interface().(bool) {
|
||||
t.Fatalf("failed, it should fail already")
|
||||
}
|
||||
|
||||
fnSimplify := func(requestParamValue string) bool {
|
||||
return requestParamValue == "kataras"
|
||||
}
|
||||
|
||||
evalFunc = convertBuilderFunc(fnSimplify)
|
||||
if !evalFunc([]string{}).Call([]reflect.Value{reflect.ValueOf("kataras")})[0].Interface().(bool) {
|
||||
t.Fatalf("it should pass, the combile arguments are empty and the given request value is the expected one")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("it should panic, the combile arguments are more than one")
|
||||
}
|
||||
}()
|
||||
|
||||
// should panic.
|
||||
evalFunc([]string{"1"}).Call([]reflect.Value{reflect.ValueOf("kataras")})
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||
"github.com/kataras/iris/macro/interpreter/ast"
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -3,8 +3,8 @@ package macro
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||
"github.com/kataras/iris/v12/macro/interpreter/parser"
|
||||
"github.com/kataras/iris/macro/interpreter/ast"
|
||||
"github.com/kataras/iris/macro/interpreter/parser"
|
||||
)
|
||||
|
||||
// Template contains a route's path full parsed template.
|
||||
|
||||
Reference in New Issue
Block a user