mirror of
https://github.com/kataras/iris.git
synced 2025-12-26 06:17:03 +00:00
_future
Former-commit-id: c6f2c47cece55865b381df33950e6dfd8ccf882f
This commit is contained in:
@@ -2,14 +2,14 @@ package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/ast"
|
||||
"gopkg.in/kataras/iris.v6/_future/ipel/lexer"
|
||||
)
|
||||
|
||||
// Test is failing because we are not finished with the Parser yet
|
||||
// 27/03
|
||||
func TestParseError(t *testing.T) {
|
||||
// fail
|
||||
illegalChar := '$'
|
||||
@@ -46,3 +46,79 @@ func TestParseError(t *testing.T) {
|
||||
}
|
||||
//
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
valid bool
|
||||
expectedStatement ast.ParamStatement
|
||||
}{
|
||||
{"{id:int min(1) max(5) else 404}", true,
|
||||
ast.ParamStatement{
|
||||
Name: "id",
|
||||
Type: ast.ParamTypeInt,
|
||||
Funcs: []ast.ParamFunc{
|
||||
ast.ParamFunc{
|
||||
Name: "min",
|
||||
Args: []ast.ParamFuncArg{1}},
|
||||
ast.ParamFunc{
|
||||
Name: "max",
|
||||
Args: []ast.ParamFuncArg{5}},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
}}, // 0
|
||||
{"{id:int range(1,5)}", true,
|
||||
ast.ParamStatement{
|
||||
Name: "id",
|
||||
Type: ast.ParamTypeInt,
|
||||
Funcs: []ast.ParamFunc{
|
||||
ast.ParamFunc{
|
||||
Name: "range",
|
||||
Args: []ast.ParamFuncArg{1, 5}},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
}}, // 1
|
||||
{"{file:path contains(.)}", true,
|
||||
ast.ParamStatement{
|
||||
Name: "file",
|
||||
Type: ast.ParamTypePath,
|
||||
Funcs: []ast.ParamFunc{
|
||||
ast.ParamFunc{
|
||||
Name: "contains",
|
||||
Args: []ast.ParamFuncArg{"."}},
|
||||
},
|
||||
ErrorCode: 404,
|
||||
}}, // 2
|
||||
{"{username:alphabetical", true,
|
||||
ast.ParamStatement{
|
||||
Name: "username",
|
||||
Type: ast.ParamTypeAlphabetical,
|
||||
ErrorCode: 404,
|
||||
}}, // 3
|
||||
{"{username:thisianunexpected", false,
|
||||
ast.ParamStatement{
|
||||
Name: "username",
|
||||
Type: ast.ParamTypeUnExpected,
|
||||
ErrorCode: 404,
|
||||
}}, // 4
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
l := lexer.New(tt.input)
|
||||
p := New(l)
|
||||
resultStmt, err := p.Parse()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user