Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xs-test
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patrick Chen
xs-test
Commits
1fbd1b19
Commit
1fbd1b19
authored
Aug 29, 2018
by
Patrick Chen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use pgjson to access JSON strings
parent
e59eb345
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
76 additions
and
81 deletions
+76
-81
.gitmodules
.gitmodules
+0
-3
Makefile
Makefile
+1
-3
Config.h
include/test/Config.h
+2
-2
Expectation.h
include/test/Expectation.h
+7
-6
Service.h
include/test/Service.h
+9
-8
Config.cc
src/test/Config.cc
+5
-5
Expectation.cc
src/test/Expectation.cc
+21
-21
Service.cc
src/test/Service.cc
+14
-15
Tester.cc
src/test/Tester.cc
+11
-11
xs-test.cc
src/xs-test.cc
+6
-6
json
submodule/json
+0
-1
No files found.
.gitmodules
View file @
1fbd1b19
[submodule "submodule/json"]
path = submodule/json
url = https://github.com/nlohmann/json.git
Makefile
View file @
1fbd1b19
REPO_DIR
=
$(
shell
pwd
)
INCLUDE_DIR
=
$(REPO_DIR)
/include
SRC_DIR
=
$(REPO_DIR)
/src
SUBMOD_DIR
=
$(REPO_DIR)
/submodule
BUILD_DIR
=
$(REPO_DIR)
/build
CXX
=
clang++
CXX_FLAGS
=
-g
-std
=
c++14
\
-I
/usr/local/include
\
-I
$(INCLUDE_DIR)
\
-I
$(SUBMOD_DIR)
/json/single_include
-I
$(INCLUDE_DIR)
LD_FLAGS
=
-L
/usr/local/lib
\
-lboost_system
\
-lboost_filesystem
\
...
...
include/test/Config.h
View file @
1fbd1b19
...
...
@@ -6,8 +6,8 @@
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <nlohmann/json.hpp>
#include "pgjson/pgjson.hpp"
#include "test/Expectation.h"
#include "test/Service.h"
...
...
@@ -39,7 +39,7 @@ namespace xs { namespace test
>
getExpectations
()
const
noexcept
;
private
:
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
static
bool
verify
(
const
pgjson
::
CValue
&
object
)
noexcept
;
private
:
service_range_type
services_
;
...
...
include/test/Expectation.h
View file @
1fbd1b19
#ifndef XS_TEST_EXPECTATION_H_INCLUDED
#define XS_TEST_EXPECTATION_H_INCLUDED
#include "pgjson/pgjson.hpp"
#include <boost/optional.hpp>
#include <nlohmann/json.hpp>
#include <string>
#include <unordered_set>
...
...
@@ -15,19 +16,19 @@ namespace xs { namespace test
public
:
Expectation
()
=
delete
;
Expectation
(
const
Expectation
&
)
=
delete
;
Expectation
(
Expectation
&&
)
noexcept
=
default
;
Expectation
(
Expectation
&&
)
=
default
;
~
Expectation
()
noexcept
=
default
;
Expectation
&
operator
=
(
const
Expectation
&
)
=
delete
;
Expectation
&
operator
=
(
Expectation
&&
)
=
delete
;
explicit
Expectation
(
const
nlohmann
::
json
&
object
);
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
explicit
Expectation
(
const
pgjson
::
CValue
&
object
);
static
bool
verify
(
const
pgjson
::
CValue
&
object
)
noexcept
;
public
:
std
::
string
name_
;
nlohmann
::
json
request_
;
boost
::
optional
<
nlohmann
::
json
>
response_
;
pgjson
::
CValue
request_
;
boost
::
optional
<
pgjson
::
CValue
>
response_
;
std
::
unordered_set
<
std
::
string
>
runs_
;
std
::
unordered_set
<
std
::
string
>
posts_
;
};
...
...
include/test/Service.h
View file @
1fbd1b19
#ifndef XS_TEST_SERVER_H_INCLUDED
#define XS_TEST_SERVER_H_INCLUDED
#include <string>
#include <boost/optional.hpp>
#include <nlohmann/json.hpp>
#include
<string>
#include
"pgjson/pgjson.hpp"
namespace
xs
{
namespace
test
...
...
@@ -14,26 +15,26 @@ namespace xs { namespace test
public
:
Service
()
=
delete
;
Service
(
const
Service
&
)
=
delete
;
Service
(
Service
&&
)
noexcept
=
default
;
Service
(
Service
&&
)
=
default
;
~
Service
()
noexcept
=
default
;
Service
&
operator
=
(
const
Service
&
)
=
delete
;
Service
&
operator
=
(
Service
&&
)
=
delete
;
explicit
Service
(
const
nlohmann
::
json
&
object
);
static
bool
verify
(
const
nlohmann
::
json
&
object
)
noexcept
;
explicit
Service
(
const
pgjson
::
CValue
&
object
);
static
bool
verify
(
const
pgjson
::
CValue
&
object
)
noexcept
;
bool
isCommand
()
const
noexcept
;
bool
isEvent
()
const
noexcept
;
private
:
Service
(
std
::
string
name
,
nlohmann
::
json
response
)
noexcept
;
Service
(
std
::
string
name
,
pgjson
::
CValue
response
)
noexcept
;
Service
(
std
::
string
name
)
noexcept
;
public
:
std
::
string
name_
;
boost
::
optional
<
nlohmann
::
json
>
request_
;
boost
::
optional
<
nlohmann
::
json
>
response_
;
boost
::
optional
<
pgjson
::
CValue
>
request_
;
boost
::
optional
<
pgjson
::
CValue
>
response_
;
};
}
}
...
...
src/test/Config.cc
View file @
1fbd1b19
#include "pgjson/pgjson.hpp"
#include "test/Config.h"
#include <boost/filesystem.hpp>
#include <nlohmann/json.hpp>
#include <fstream>
#include <iterator>
...
...
@@ -30,7 +30,7 @@ namespace detail
Config
::
Config
(
const
boost
::
filesystem
::
path
&
file
)
{
const
auto
object
=
nlohmann
::
json
::
pars
e
(
const
auto
object
=
pgjson
::
toCValu
e
(
detail
::
readAsString
(
file
)
);
...
...
@@ -65,13 +65,13 @@ boost::iterator_range<
);
}
bool
Config
::
verify
(
const
nlohmann
::
json
&
object
)
noexcept
bool
Config
::
verify
(
const
pgjson
::
CValue
&
object
)
noexcept
{
if
(
!
(
object
.
count
(
"mockData"
)
&&
object
[
"mockData"
].
is_a
rray
()))
{
if
(
!
(
object
.
hasMember
(
"mockData"
)
&&
object
[
"mockData"
].
isA
rray
()))
{
return
false
;
}
if
(
!
(
object
.
count
(
"script"
)
&&
object
[
"script"
].
is_a
rray
()))
{
if
(
!
(
object
.
hasMember
(
"script"
)
&&
object
[
"script"
].
isA
rray
()))
{
return
false
;
}
...
...
src/test/Expectation.cc
View file @
1fbd1b19
#include "pgjson/pgjson.hpp"
#include "test/Expectation.h"
#include <nlohmann/json.hpp>
#include <stdexcept>
namespace
xs
{
namespace
test
{
Expectation
::
Expectation
(
const
nlohmann
::
json
&
object
)
:
name_
(
object
[
"command"
].
get
<
std
::
string
>
())
Expectation
::
Expectation
(
const
pgjson
::
CValue
&
object
)
:
name_
(
object
[
"command"
].
as
<
std
::
string
>
())
{
request_
=
object
[
"param"
];
if
(
!
object
.
count
(
"expect"
))
{
if
(
!
object
.
hasMember
(
"expect"
))
{
return
;
}
const
auto
&
expect
=
object
[
"expect"
];
response_
=
expect
[
"response"
];
if
(
expect
.
count
(
"runs"
))
{
if
(
expect
.
hasMember
(
"runs"
))
{
for
(
const
auto
&
element
:
expect
[
"runs"
])
{
if
(
!
element
.
is
_s
tring
())
{
if
(
!
element
.
is
S
tring
())
{
throw
std
::
runtime_error
(
"element in
\"
runs
\"
is not string"
);
}
runs_
.
emplace
(
element
.
get
<
std
::
string
>
());
runs_
.
emplace
(
element
.
as
<
std
::
string
>
());
}
}
if
(
expect
.
count
(
"posts"
))
{
if
(
expect
.
hasMember
(
"posts"
))
{
for
(
const
auto
&
element
:
expect
[
"posts"
])
{
if
(
!
element
.
is
_s
tring
())
{
if
(
!
element
.
is
S
tring
())
{
throw
std
::
runtime_error
(
"element in
\"
posts
\"
is not string"
);
}
posts_
.
emplace
(
element
.
get
<
std
::
string
>
());
posts_
.
emplace
(
element
.
as
<
std
::
string
>
());
}
}
}
bool
Expectation
::
verify
(
const
nlohmann
::
json
&
object
)
noexcept
bool
Expectation
::
verify
(
const
pgjson
::
CValue
&
object
)
noexcept
{
if
(
!
object
.
is
_o
bject
())
{
if
(
!
object
.
is
O
bject
())
{
return
false
;
}
if
(
!
(
object
.
count
(
"command"
)
&&
object
[
"command"
].
is_s
tring
()))
{
if
(
!
(
object
.
hasMember
(
"command"
)
&&
object
[
"command"
].
isS
tring
()))
{
return
false
;
}
if
(
!
object
.
count
(
"param"
))
{
if
(
!
object
.
hasMember
(
"param"
))
{
return
false
;
}
if
(
!
(
object
[
"param"
].
is
_string
()
||
object
[
"param"
].
is_o
bject
()))
{
if
(
!
(
object
[
"param"
].
is
String
()
||
object
[
"param"
].
isO
bject
()))
{
return
false
;
}
if
(
!
object
.
count
(
"expect"
))
{
if
(
!
object
.
hasMember
(
"expect"
))
{
return
true
;
}
if
(
!
object
[
"expect"
].
is
_o
bject
())
{
if
(
!
object
[
"expect"
].
is
O
bject
())
{
return
false
;
}
const
auto
&
expect
=
object
[
"expect"
];
if
(
!
expect
.
count
(
"response"
))
{
if
(
!
expect
.
hasMember
(
"response"
))
{
return
false
;
}
if
(
!
(
expect
[
"response"
].
is
_string
()
||
expect
[
"response"
].
is_o
bject
()))
{
if
(
!
(
expect
[
"response"
].
is
String
()
||
expect
[
"response"
].
isO
bject
()))
{
return
false
;
}
if
(
expect
.
count
(
"runs"
)
&&
!
expect
[
"runs"
].
is_a
rray
())
{
if
(
expect
.
hasMember
(
"runs"
)
&&
!
expect
[
"runs"
].
isA
rray
())
{
return
false
;
}
if
(
expect
.
count
(
"posts"
)
&&
!
expect
[
"posts"
].
is_a
rray
())
{
if
(
expect
.
hasMember
(
"posts"
)
&&
!
expect
[
"posts"
].
isA
rray
())
{
return
false
;
}
...
...
src/test/Service.cc
View file @
1fbd1b19
#include "pgjson/pgjson.hpp"
#include "test/Service.h"
#include <nlohmann/json.hpp>
#include <memory>
namespace
xs
{
namespace
test
{
Service
::
Service
(
const
nlohmann
::
json
&
object
)
Service
::
Service
(
const
pgjson
::
CValue
&
object
)
:
Service
(
object
[
object
.
count
(
"command"
)
?
"command"
:
"event"
].
get
<
std
::
string
>
()
object
[
object
.
hasMember
(
"command"
)
?
"command"
:
"event"
].
as
<
std
::
string
>
()
)
{
if
(
object
.
count
(
"command"
))
{
if
(
object
.
hasMember
(
"command"
))
{
response_
=
object
[
"response"
];
if
(
object
.
count
(
"param"
))
{
if
(
object
.
hasMember
(
"param"
))
{
request_
=
object
[
"param"
];
}
}
}
bool
Service
::
verify
(
const
nlohmann
::
json
&
object
)
noexcept
bool
Service
::
verify
(
const
pgjson
::
CValue
&
object
)
noexcept
{
if
(
!
object
.
is
_o
bject
())
{
if
(
!
object
.
is
O
bject
())
{
return
false
;
}
static
const
auto
isStringOrObject
=
[](
const
nlohmann
::
json
&
object
)
{
return
object
.
is
_string
()
||
object
.
is_o
bject
();
static
const
auto
isStringOrObject
=
[](
const
pgjson
::
CValue
&
object
)
{
return
object
.
is
String
()
||
object
.
isO
bject
();
};
if
(
object
.
count
(
"command"
))
{
if
(
object
.
count
(
"param"
))
{
if
(
object
.
hasMember
(
"command"
))
{
if
(
object
.
hasMember
(
"param"
))
{
if
(
!
isStringOrObject
(
object
[
"param"
]))
{
return
false
;
}
}
return
object
[
"command"
].
is
_s
tring
()
return
object
[
"command"
].
is
S
tring
()
&&
isStringOrObject
(
object
[
"response"
])
;
}
else
if
(
object
.
count
(
"event"
))
{
return
object
[
"event"
].
is
_s
tring
();
}
else
if
(
object
.
hasMember
(
"event"
))
{
return
object
[
"event"
].
is
S
tring
();
}
return
false
;
...
...
src/test/Tester.cc
View file @
1fbd1b19
#include "pgjson/pgjson.hpp"
#include "test/Service.h"
#include "test/Tester.h"
#include <boost/algorithm/string/join.hpp>
#include <boost/format.hpp>
#include <boost/range/adaptors.hpp>
#include <nlohmann/json.hpp>
#include <chrono>
#include <functional>
...
...
@@ -14,10 +14,10 @@
namespace
xs
{
namespace
test
{
namespace
detail
{
std
::
string
toString
(
const
nlohmann
::
json
&
object
)
std
::
string
toString
(
const
pgjson
::
CValue
&
object
)
{
return
object
.
is
_string
()
?
object
.
get
<
std
::
string
>
()
:
object
.
dump
();
return
object
.
is
String
()
?
object
.
as
<
std
::
string
>
()
:
object
.
toStdString
();
}
}
...
...
@@ -62,11 +62,11 @@ Tester::Report Tester::createReport(const std::string& response, const Expectati
return
report
;
}
if
(
expectation
.
response_
->
is
_s
tring
())
{
report
.
pass
=
(
response
==
expectation
.
response_
->
get
<
std
::
string
>
());
if
(
expectation
.
response_
->
is
S
tring
())
{
report
.
pass
=
(
response
==
expectation
.
response_
->
as
<
std
::
string
>
());
}
else
{
try
{
report
.
pass
=
(
nlohmann
::
json
::
pars
e
(
response
)
==
*
expectation
.
response_
);
report
.
pass
=
(
pgjson
::
toCValu
e
(
response
)
==
*
expectation
.
response_
);
}
catch
(...)
{
}
...
...
@@ -112,14 +112,14 @@ std::string Tester::onCommand(const std::string& name, const std::string& params
auto
&
receiveCount
=
receivedCommandCount_
[
name
];
const
auto
&
request
=
*
handler
.
request_
;
const
bool
isString
=
request
.
is
_s
tring
();
const
bool
isString
=
request
.
is
S
tring
();
if
(
isString
)
{
if
(
params
==
request
.
get
<
std
::
string
>
())
{
if
(
params
==
request
.
as
<
std
::
string
>
())
{
receiveCount
++
;
return
detail
::
toString
(
*
handler
.
response_
);
}
}
else
{
if
(
nlohmann
::
json
::
pars
e
(
params
)
==
request
)
{
if
(
pgjson
::
toCValu
e
(
params
)
==
request
)
{
receiveCount
++
;
return
detail
::
toString
(
*
handler
.
response_
);
}
...
...
@@ -139,7 +139,7 @@ void Tester::run() noexcept
for
(
const
auto
&
expectation
:
config_
.
getExpectations
())
{
resetCounters
();
const
auto
response
=
server_
.
runCmd
(
expectation
.
name_
,
expectation
.
request_
.
dump
());
const
auto
response
=
server_
.
runCmd
(
expectation
.
name_
,
expectation
.
request_
.
toStdString
());
// wait 100ms for upcoming events
using
namespace
std
::
chrono_literals
;
...
...
src/xs-test.cc
View file @
1fbd1b19
#include "pgjson/pgjson.hpp"
#include "positivegrid/exchange_server.h"
#include "test/Tester.h"
#include <boost/filesystem.hpp>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <string>
#include <thread>
#include <boost/filesystem.hpp>
#include "test/Tester.h"
#include "nlohmann/json.hpp"
#include "positivegrid/exchange_server.h"
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
...
...
json
@
d713727f
Subproject commit d713727f2277f2eb919a2dbbfdd534f8988aa493
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