iris/core/router/macro/macro_test.go

247 lines
6.6 KiB
Go

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(t *testing.T, macroEvaluator *Macro, input string, pass bool, i int) {
if got := macroEvaluator.Evaluator(input); pass != got {
t.Fatalf("%s - tests[%d] - expecting %v but got %v", t.Name(), i, pass, got)
}
}
func TestStringEvaluatorRaw(t *testing.T) {
f := NewMap()
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, f.String, tt.input, tt.pass, i)
}
}
func TestNumberEvaluatorRaw(t *testing.T) {
f := NewMap()
tests := []struct {
pass bool
input string
}{
{false, "astring"}, // 0
{false, "astringwith_numb3rS_and_symbol$"}, // 1
{true, "32321"}, // 2
{true, "18446744073709551615"}, // 3
{true, "-18446744073709551615"}, // 4
{true, "-18446744073709553213213213213213121615"}, // 5
{false, "42 18446744073709551615"}, // 6
{false, "--42"}, // 7
{false, "+42"}, // 9
{false, "main.css"}, // 9
{false, "/assets/main.css"}, // 10
}
for i, tt := range tests {
testEvaluatorRaw(t, f.Number, tt.input, tt.pass, i)
}
}
func TestInt64EvaluatorRaw(t *testing.T) {
f := NewMap()
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, f.Int64, tt.input, tt.pass, i)
}
}
func TestUint64EvaluatorRaw(t *testing.T) {
f := NewMap()
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, f.Uint64, tt.input, tt.pass, i)
}
}
func TestAlphabeticalEvaluatorRaw(t *testing.T) {
f := NewMap()
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, f.Alphabetical, tt.input, tt.pass, i)
}
}
func TestFileEvaluatorRaw(t *testing.T) {
f := NewMap()
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, f.File, tt.input, tt.pass, i)
}
}
func TestPathEvaluatorRaw(t *testing.T) {
f := NewMap()
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, f.Path, tt.input, tt.pass, i)
}
}
// func TestMapRegisterFunc(t *testing.T) {
// m := NewMap()
// m.String.RegisterFunc("prefix", func(prefix string) EvaluatorFunc {
// return func(paramValue string) bool {
// return strings.HasPrefix(paramValue, prefix)
// }
// })
// p, err := Parse("/user/@iris")
// if err != nil {
// t.Fatalf(err)
// }
// // p.Params = append(p.)
// testEvaluatorRaw(t, m.String, p.Src, false, 0)
// }