1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

add the stale release

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-12 23:41:20 +03:00
parent 535fb6dc0d
commit 2a4ce876b6
793 changed files with 188 additions and 52415 deletions

View File

@@ -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.

View File

@@ -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.

View File

@@ -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"

View File

@@ -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])
}
}
}
}
}