1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 22:11:27 +00:00
Beego/context/param/parsers_test.go

85 lines
2.7 KiB
Go
Raw Normal View History

2017-05-11 16:32:44 +00:00
package param
import "testing"
import "reflect"
import "time"
type testDefinition struct {
strValue string
expectedValue interface{}
expectedParser paramParser
}
func Test_Parsers(t *testing.T) {
2017-05-12 06:25:12 +00:00
//ints
2017-05-11 16:32:44 +00:00
checkParser(testDefinition{"1", 1, intParser{}}, t)
2017-05-12 06:25:12 +00:00
checkParser(testDefinition{"-1", int64(-1), intParser{}}, t)
checkParser(testDefinition{"1", uint64(1), intParser{}}, t)
2017-05-11 16:32:44 +00:00
2017-05-12 06:25:12 +00:00
//floats
checkParser(testDefinition{"1.0", float32(1.0), floatParser{}}, t)
checkParser(testDefinition{"-1.0", float64(-1.0), floatParser{}}, t)
2017-05-11 16:32:44 +00:00
2017-05-12 06:25:12 +00:00
//strings
checkParser(testDefinition{"AB", "AB", stringParser{}}, t)
checkParser(testDefinition{"AB", []byte{65, 66}, stringParser{}}, t)
2017-05-11 16:32:44 +00:00
2017-05-12 06:25:12 +00:00
//bools
2017-05-11 16:32:44 +00:00
checkParser(testDefinition{"true", true, boolParser{}}, t)
2017-05-12 06:25:12 +00:00
checkParser(testDefinition{"0", false, boolParser{}}, t)
2017-05-11 16:32:44 +00:00
2017-05-12 06:25:12 +00:00
//timeParser
2017-05-11 16:32:44 +00:00
checkParser(testDefinition{"2017-05-30T13:54:53Z", time.Date(2017, 5, 30, 13, 54, 53, 0, time.UTC), timeParser{}}, t)
2017-05-12 06:25:12 +00:00
checkParser(testDefinition{"2017-05-30", time.Date(2017, 5, 30, 0, 0, 0, 0, time.UTC), timeParser{}}, t)
2017-05-11 16:32:44 +00:00
2017-05-12 06:25:12 +00:00
//json
checkParser(testDefinition{`{"X": 5, "Y":"Z"}`, struct {
X int
Y string
}{5, "Z"}, jsonParser{}}, t)
2017-05-11 16:32:44 +00:00
2017-05-12 06:25:12 +00:00
//slice in query is parsed as comma delimited
2017-05-11 16:32:44 +00:00
checkParser(testDefinition{`1,2`, []int{1, 2}, sliceParser(intParser{})}, t)
2017-05-12 06:25:12 +00:00
//slice in body is parsed as json
2017-05-12 06:28:46 +00:00
checkParser(testDefinition{`["a","b"]`, []string{"a", "b"}, jsonParser{}}, t, MethodParam{in: body})
2017-05-12 06:25:12 +00:00
//pointers
var someInt = 1
checkParser(testDefinition{`1`, &someInt, ptrParser(intParser{})}, t)
var someStruct = struct{ X int }{5}
checkParser(testDefinition{`{"X": 5}`, &someStruct, jsonParser{}}, t)
2017-05-11 16:32:44 +00:00
}
2017-05-12 06:25:12 +00:00
func checkParser(def testDefinition, t *testing.T, methodParam ...MethodParam) {
2017-05-11 16:32:44 +00:00
toType := reflect.TypeOf(def.expectedValue)
2017-05-12 06:25:12 +00:00
var mp MethodParam
if len(methodParam) == 0 {
mp = MethodParam{}
} else {
mp = methodParam[0]
}
parser := getParser(&mp, toType)
2017-05-11 16:32:44 +00:00
if reflect.TypeOf(parser) != reflect.TypeOf(def.expectedParser) {
t.Errorf("Invalid parser for value %v. Expected: %v, actual: %v", def.strValue, reflect.TypeOf(def.expectedParser).Name(), reflect.TypeOf(parser).Name())
return
}
result, err := parser.parse(def.strValue, toType)
if err != nil {
t.Errorf("Parsing error for value %v. Expected result: %v, error: %v", def.strValue, def.expectedValue, err)
return
}
2017-05-12 06:25:12 +00:00
convResult, err := safeConvert(reflect.ValueOf(result), toType)
if err != nil {
2017-10-11 06:35:31 +00:00
t.Errorf("Conversion error for %v. from value: %v, toType: %v, error: %v", def.strValue, result, toType, err)
2017-05-12 06:25:12 +00:00
return
}
if !reflect.DeepEqual(convResult.Interface(), def.expectedValue) {
2017-05-11 16:32:44 +00:00
t.Errorf("Parsing error for value %v. Expected result: %v, actual: %v", def.strValue, def.expectedValue, result)
}
}