Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
23bb5740
Commit
23bb5740
authored
Apr 08, 2019
by
Vladimir Glavnyy
Committed by
Wouter van Oortmerssen
Apr 08, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add basic test for enum defaults (#5280)
parent
dd6daa70
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
4 deletions
+28
-4
test.cpp
tests/test.cpp
+28
-4
No files found.
tests/test.cpp
View file @
23bb5740
...
...
@@ -1305,15 +1305,20 @@ void ErrorTest() {
TestError
(
"enum X:bool { Y = true }"
,
"must be integral"
);
}
template
<
typename
T
>
T
TestValue
(
const
char
*
json
,
const
char
*
type_name
)
{
template
<
typename
T
>
T
TestValue
(
const
char
*
json
,
const
char
*
type_name
,
const
char
*
decls
=
nullptr
)
{
flatbuffers
::
Parser
parser
;
parser
.
builder_
.
ForceDefaults
(
true
);
// return defaults
auto
check_default
=
json
?
false
:
true
;
if
(
check_default
)
{
parser
.
opts
.
output_default_scalars_in_json
=
true
;
}
// Simple schema.
std
::
string
schema
=
"table X { Y:"
+
std
::
string
(
type_name
)
+
"; } root_type X;"
;
TEST_EQ
(
parser
.
Parse
(
schema
.
c_str
()),
true
);
std
::
string
schema
=
std
::
string
(
decls
?
decls
:
""
)
+
"
\n
"
+
"table X { Y:"
+
std
::
string
(
type_name
)
+
"; } root_type X;"
;
auto
schema_done
=
parser
.
Parse
(
schema
.
c_str
());
TEST_EQ_STR
(
parser
.
error_
.
c_str
(),
""
);
TEST_EQ
(
schema_done
,
true
);
auto
done
=
parser
.
Parse
(
check_default
?
"{}"
:
json
);
TEST_EQ_STR
(
parser
.
error_
.
c_str
(),
""
);
...
...
@@ -1439,6 +1444,24 @@ void EnumOutOfRangeTest() {
TestError
(
"enum X:ulong { Y = 18446744073709551615 }"
,
"constant does not fit"
);
}
void
EnumValueTest
()
{
// json: "{ Y:0 }", schema: table X { Y : "E"}
// 0 in enum (V=0) E then Y=0 is valid.
TEST_EQ
(
TestValue
<
int
>
(
"{ Y:0 }"
,
"E"
,
"enum E:int { V }"
),
0
);
TEST_EQ
(
TestValue
<
int
>
(
"{ Y:V }"
,
"E"
,
"enum E:int { V }"
),
0
);
// A default value of Y is 0.
TEST_EQ
(
TestValue
<
int
>
(
"{ }"
,
"E"
,
"enum E:int { V }"
),
0
);
TEST_EQ
(
TestValue
<
int
>
(
"{ Y:5 }"
,
"E=V"
,
"enum E:int { V=5 }"
),
5
);
// Generate json with defaults and check.
TEST_EQ
(
TestValue
<
int
>
(
nullptr
,
"E=V"
,
"enum E:int { V=5 }"
),
5
);
// 5 in enum
TEST_EQ
(
TestValue
<
int
>
(
"{ Y:5 }"
,
"E"
,
"enum E:int { Z, V=5 }"
),
5
);
TEST_EQ
(
TestValue
<
int
>
(
"{ Y:5 }"
,
"E=V"
,
"enum E:int { Z, V=5 }"
),
5
);
// Generate json with defaults and check.
TEST_EQ
(
TestValue
<
int
>
(
nullptr
,
"E"
,
"enum E:int { Z, V=5 }"
),
0
);
TEST_EQ
(
TestValue
<
int
>
(
nullptr
,
"E=V"
,
"enum E:int { Z, V=5 }"
),
5
);
}
void
IntegerOutOfRangeTest
()
{
TestError
(
"table T { F:byte; } root_type T; { F:128 }"
,
"constant does not fit"
);
...
...
@@ -2622,6 +2645,7 @@ int FlatBufferTests() {
ErrorTest
();
ValueTest
();
EnumValueTest
();
EnumStringsTest
();
EnumNamesTest
();
EnumOutOfRangeTest
();
...
...
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