iris/core/router/macro/macro_test.go

191 lines
4.5 KiB
Go
Raw Normal View History

Publish the new version :airplane: | Look description please! # FAQ ### Looking for free support? http://support.iris-go.com https://kataras.rocket.chat/channel/iris ### Looking for previous versions? https://github.com/kataras/iris#version ### Should I upgrade my Iris? Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready. > Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`. For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework). ### About our new home page http://iris-go.com Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome! [Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him. The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please! Read more at https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package macro
import (
"reflect"
"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 int, max int) 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(macroEvaluator *Macro, input string, pass bool, i int, t *testing.T) {
if got := macroEvaluator.Evaluator(input); pass != got {
t.Fatalf("tests[%d] - expecting %v but got %v", i, pass, got)
}
}
func TestStringEvaluatorRaw(t *testing.T) {
f := NewMacroMap()
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(f.String, tt.input, tt.pass, i, t)
}
}
func TestIntEvaluatorRaw(t *testing.T) {
f := NewMacroMap()
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{false, "main.css"}, // 3
{false, "/assets/main.css"}, // 4
}
for i, tt := range tests {
testEvaluatorRaw(f.Int, tt.input, tt.pass, i, t)
}
}
func TestAlphabeticalEvaluatorRaw(t *testing.T) {
f := NewMacroMap()
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(f.Alphabetical, tt.input, tt.pass, i, t)
}
}
func TestFileEvaluatorRaw(t *testing.T) {
f := NewMacroMap()
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(f.File, tt.input, tt.pass, i, t)
}
}
func TestPathEvaluatorRaw(t *testing.T) {
f := NewMacroMap()
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(f.Path, tt.input, tt.pass, i, t)
}
}
// func TestMapRegisterFunc(t *testing.T) {
// m := NewMacroMap()
// m.String.RegisterFunc("prefix", func(prefix string) EvaluatorFunc {
// return func(paramValue string) bool {
// return strings.HasPrefix(paramValue, prefix)
// }
// })
// p, err := Parse("/user/@kataras")
// if err != nil {
// t.Fatalf(err)
// }
// // p.Params = append(p.)
// testEvaluatorRaw(m.String, p.Src, false, 0, t)
// }