Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
b6a35a89
Commit
b6a35a89
authored
May 12, 2017
by
eyalpost
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more tests
parent
74dc3c75
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
6 deletions
+45
-6
parsers_test.go
context/param/parsers_test.go
+45
-6
No files found.
context/param/parsers_test.go
View file @
b6a35a89
...
...
@@ -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
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment