Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
922f7a3d
Unverified
Commit
922f7a3d
authored
Feb 25, 2018
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✅
added more tests for SAX parsing
parent
ac230e8b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
1 deletion
+108
-1
unit-class_parser.cpp
test/src/unit-class_parser.cpp
+108
-1
No files found.
test/src/unit-class_parser.cpp
View file @
922f7a3d
...
...
@@ -34,6 +34,106 @@ using nlohmann::json;
#include <valarray>
class
SaxEventLogger
:
public
nlohmann
::
json
::
SAX
{
public
:
bool
null
()
override
{
events
.
push_back
(
"null()"
);
return
true
;
}
bool
boolean
(
bool
val
)
override
{
events
.
push_back
(
val
?
"boolean(true)"
:
"boolean(false)"
);
return
true
;
}
bool
number_integer
(
json
::
number_integer_t
val
)
override
{
events
.
push_back
(
"number_integer("
+
std
::
to_string
(
val
)
+
")"
);
return
true
;
}
bool
number_unsigned
(
json
::
number_unsigned_t
val
)
override
{
events
.
push_back
(
"number_unsigned("
+
std
::
to_string
(
val
)
+
")"
);
return
true
;
}
bool
number_float
(
json
::
number_float_t
val
,
const
std
::
string
&
s
)
override
{
events
.
push_back
(
"number_float("
+
s
+
")"
);
return
true
;
}
bool
string
(
const
std
::
string
&
val
)
override
{
events
.
push_back
(
"string("
+
val
+
")"
);
return
true
;
}
bool
start_object
(
std
::
size_t
elements
)
override
{
if
(
elements
==
-
1
)
{
events
.
push_back
(
"start_object()"
);
}
else
{
events
.
push_back
(
"start_object("
+
std
::
to_string
(
elements
)
+
")"
);
}
return
true
;
}
bool
key
(
const
std
::
string
&
val
)
override
{
events
.
push_back
(
"key("
+
val
+
")"
);
return
true
;
}
bool
end_object
()
override
{
events
.
push_back
(
"end_object()"
);
return
true
;
}
bool
start_array
(
std
::
size_t
elements
)
override
{
if
(
elements
==
-
1
)
{
events
.
push_back
(
"start_array()"
);
}
else
{
events
.
push_back
(
"start_array("
+
std
::
to_string
(
elements
)
+
")"
);
}
return
true
;
}
bool
end_array
()
override
{
events
.
push_back
(
"end_array()"
);
return
true
;
}
bool
binary
(
const
std
::
vector
<
uint8_t
>&
vec
)
override
{
events
.
push_back
(
"binary()"
);
return
true
;
}
bool
parse_error
(
int
position
,
const
std
::
string
&
last_token
)
override
{
errored
=
true
;
events
.
push_back
(
"parse_error("
+
std
::
to_string
(
position
)
+
")"
);
return
false
;
}
std
::
vector
<
std
::
string
>
events
;
bool
errored
=
false
;
};
json
parser_helper
(
const
std
::
string
&
s
);
bool
accept_helper
(
const
std
::
string
&
s
);
...
...
@@ -53,6 +153,8 @@ json parser_helper(const std::string& s)
bool
accept_helper
(
const
std
::
string
&
s
)
{
CAPTURE
(
s
);
// 1. parse s without exceptions
json
j
;
CHECK_NOTHROW
(
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
),
nullptr
,
false
).
parse
(
true
,
j
));
...
...
@@ -64,7 +166,12 @@ bool accept_helper(const std::string& s)
// 3. check if both approaches come to the same result
CHECK
(
ok_noexcept
==
ok_accept
);
// 4. return result
// 4. parse with SAX (compare with relaxed accept result)
SaxEventLogger
el
;
CHECK_NOTHROW
(
json
::
sax_parse
(
s
,
&
el
));
CHECK
(
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
)).
accept
(
false
)
==
not
el
.
errored
);
// 5. return result
return
ok_accept
;
}
...
...
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