mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 19:00:55 +00:00
more tests
This commit is contained in:
parent
74dc3c7500
commit
b6a35a8944
@ -11,24 +11,58 @@ type testDefinition struct {
|
||||
}
|
||||
|
||||
func Test_Parsers(t *testing.T) {
|
||||
|
||||
//ints
|
||||
checkParser(testDefinition{"1", 1, intParser{}}, t)
|
||||
checkParser(testDefinition{"-1", int64(-1), intParser{}}, t)
|
||||
checkParser(testDefinition{"1", uint64(1), intParser{}}, t)
|
||||
|
||||
checkParser(testDefinition{"1.0", 1.0, floatParser{}}, t)
|
||||
//floats
|
||||
checkParser(testDefinition{"1.0", float32(1.0), floatParser{}}, t)
|
||||
checkParser(testDefinition{"-1.0", float64(-1.0), floatParser{}}, t)
|
||||
|
||||
checkParser(testDefinition{"1", "1", stringParser{}}, t)
|
||||
//strings
|
||||
checkParser(testDefinition{"AB", "AB", stringParser{}}, t)
|
||||
checkParser(testDefinition{"AB", []byte{65, 66}, stringParser{}}, t)
|
||||
|
||||
//bools
|
||||
checkParser(testDefinition{"true", true, boolParser{}}, t)
|
||||
checkParser(testDefinition{"0", false, boolParser{}}, t)
|
||||
|
||||
//timeParser
|
||||
checkParser(testDefinition{"2017-05-30T13:54:53Z", time.Date(2017, 5, 30, 13, 54, 53, 0, time.UTC), timeParser{}}, t)
|
||||
checkParser(testDefinition{"2017-05-30", time.Date(2017, 5, 30, 0, 0, 0, 0, time.UTC), timeParser{}}, t)
|
||||
|
||||
checkParser(testDefinition{`{"X": 5}`, struct{ X int }{5}, jsonParser{}}, t)
|
||||
//json
|
||||
checkParser(testDefinition{`{"X": 5, "Y":"Z"}`, struct {
|
||||
X int
|
||||
Y string
|
||||
}{5, "Z"}, jsonParser{}}, t)
|
||||
|
||||
//slice in query is parsed as comma delimited
|
||||
checkParser(testDefinition{`1,2`, []int{1, 2}, sliceParser(intParser{})}, t)
|
||||
|
||||
//slice in body is parsed as json
|
||||
checkParser(testDefinition{`["a","b"]`, []string{"a", "b"}, jsonParser{}}, t, MethodParam{location: body})
|
||||
|
||||
//pointers
|
||||
var someInt = 1
|
||||
checkParser(testDefinition{`1`, &someInt, ptrParser(intParser{})}, t)
|
||||
|
||||
var someStruct = struct{ X int }{5}
|
||||
checkParser(testDefinition{`{"X": 5}`, &someStruct, jsonParser{}}, t)
|
||||
|
||||
}
|
||||
|
||||
func checkParser(def testDefinition, t *testing.T) {
|
||||
func checkParser(def testDefinition, t *testing.T, methodParam ...MethodParam) {
|
||||
toType := reflect.TypeOf(def.expectedValue)
|
||||
parser := getParser(&MethodParam{}, toType)
|
||||
var mp MethodParam
|
||||
if len(methodParam) == 0 {
|
||||
mp = MethodParam{}
|
||||
} else {
|
||||
mp = methodParam[0]
|
||||
}
|
||||
parser := getParser(&mp, toType)
|
||||
|
||||
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())
|
||||
@ -39,7 +73,12 @@ func checkParser(def testDefinition, t *testing.T) {
|
||||
t.Errorf("Parsing error for value %v. Expected result: %v, error: %v", def.strValue, def.expectedValue, err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(result, def.expectedValue) {
|
||||
convResult, err := safeConvert(reflect.ValueOf(result), toType)
|
||||
if err != nil {
|
||||
t.Errorf("Convertion error for %v. from value: %v, toType: %v, error: %v", def.strValue, result, toType, err)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(convResult.Interface(), def.expectedValue) {
|
||||
t.Errorf("Parsing error for value %v. Expected result: %v, actual: %v", def.strValue, def.expectedValue, result)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user