xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
rpc
Status_test.cpp
1
#include <xrpld/rpc/Status.h>
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/beast/unit_test/suite.h>
5
#include <xrpl/json/json_value.h>
6
#include <xrpl/protocol/ErrorCodes.h>
7
#include <xrpl/protocol/TER.h>
8
#include <xrpl/protocol/jss.h>
9
10
#include <
algorithm
>
11
#include <
cstddef
>
12
#include <
exception
>
13
#include <
string
>
14
15
namespace
xrpl::rpc
{
16
17
class
codeString_test
:
public
beast::unit_test::Suite
18
{
19
private
:
20
template
<
typename
Type>
21
std::string
22
codeString
(Type t)
23
{
24
return
Status
(t).
codeString
();
25
}
26
27
void
28
testOk
()
29
{
30
testcase
(
"OK"
);
31
{
32
auto
s =
codeString
(
Status
());
33
expect
(s.empty(),
"String for OK status"
);
34
}
35
36
{
37
auto
s =
codeString
(
Status::kOK
);
38
expect
(s.empty(),
"String for OK status"
);
39
}
40
41
{
42
auto
s =
codeString
(0);
43
expect
(s.empty(),
"String for 0 status"
);
44
}
45
46
{
47
auto
s =
codeString
(
tesSUCCESS
);
48
expect
(s.empty(),
"String for tesSUCCESS"
);
49
}
50
51
{
52
auto
s =
codeString
(
RpcSuccess
);
53
expect
(s.empty(),
"String for rpcSUCCESS"
);
54
}
55
}
56
57
void
58
testError
()
59
{
60
testcase
(
"error"
);
61
{
62
auto
s =
codeString
(23);
63
expect
(s ==
"23"
, s);
64
}
65
66
{
67
auto
s =
codeString
(
temBAD_AMOUNT
);
68
expect
(s ==
"temBAD_AMOUNT: Malformed: Bad amount."
, s);
69
}
70
71
{
72
auto
s =
codeString
(
RpcBadSyntax
);
73
expect
(s ==
"badSyntax: Syntax error."
, s);
74
}
75
}
76
77
public
:
78
void
79
run
()
override
80
{
81
testOk
();
82
testError
();
83
}
84
};
85
86
BEAST_DEFINE_TESTSUITE
(codeString,
rpc
, RPC);
87
88
class
fillJson_test
:
public
beast::unit_test::Suite
89
{
90
private
:
91
json::Value
value_
;
92
93
template
<
typename
Type>
94
void
95
fillJson
(Type t)
96
{
97
value_
.clear();
98
Status
(t).
fillJson
(
value_
);
99
}
100
101
void
102
testOk
()
103
{
104
testcase
(
"OK"
);
105
fillJson
(
Status
());
106
expect
(!
value_
,
"Value for empty status"
);
107
108
fillJson
(0);
109
expect
(!
value_
,
"Value for 0 status"
);
110
111
fillJson
(
Status::kOK
);
112
expect
(!
value_
,
"Value for OK status"
);
113
114
fillJson
(
tesSUCCESS
);
115
expect
(!
value_
,
"Value for tesSUCCESS"
);
116
117
fillJson
(
RpcSuccess
);
118
expect
(!
value_
,
"Value for rpcSUCCESS"
);
119
}
120
121
template
<
typename
Type>
122
void
123
expectFill
(
124
std::string
const
& label,
125
Type status,
126
Status::Strings
messages,
127
std::string
const
& message)
128
{
129
value_
.clear();
130
fillJson
(
Status
(status, messages));
131
132
auto
prefix = label +
": "
;
133
expect
(
bool
(
value_
), prefix +
"No value"
);
134
135
auto
error =
value_
[jss::error];
136
expect
(
bool
(error), prefix +
"No error."
);
137
138
auto
code = error[jss::code].asInt();
139
expect
(
140
status == code,
141
prefix +
"Wrong status "
+
std::to_string
(code) +
" != "
+
std::to_string
(status));
142
143
auto
m = error[jss::message].asString();
144
expect
(m == message, m +
" != "
+ message);
145
146
auto
d = error[jss::data];
147
size_t
const
s1 = d.size();
148
size_t
const
s2 = messages.
size
();
149
expect
(
150
s1 == s2,
151
prefix +
"Data sizes differ "
+
std::to_string
(s1) +
" != "
+
std::to_string
(s2));
152
for
(
auto
i = 0; i <
std::min
(s1, s2); ++i)
153
{
154
auto
ds = d[i].asString();
155
expect
(ds == messages[i], prefix + ds +
" != "
+ messages[i]);
156
}
157
}
158
159
void
160
testError
()
161
{
162
testcase
(
"error"
);
163
expectFill
(
"temBAD_AMOUNT"
,
temBAD_AMOUNT
, {},
"temBAD_AMOUNT: Malformed: Bad amount."
);
164
165
expectFill
(
166
"RpcBadSyntax"
,
167
RpcBadSyntax
,
168
{
"An error."
,
"Another error."
},
169
"badSyntax: Syntax error."
);
170
171
expectFill
(
"integer message"
, 23, {
"Stuff."
},
"23"
);
172
}
173
174
void
175
testThrow
()
176
{
177
testcase
(
"throw"
);
178
try
179
{
180
Throw<Status>
(
Status
(
temBAD_PATH
, {
"path=sdcdfd"
}));
181
}
182
catch
(
Status
const
& s)
183
{
184
expect
(s.
toTER
() ==
temBAD_PATH
,
"temBAD_PATH wasn't thrown"
);
185
auto
msgs = s.
messages
();
186
expect
(msgs.size() == 1,
"Wrong number of messages"
);
187
expect
(msgs[0] ==
"path=sdcdfd"
, msgs[0]);
188
}
189
catch
(
std::exception
const
&)
190
{
191
expect
(
false
,
"Didn't catch a Status"
);
192
}
193
}
194
195
public
:
196
void
197
run
()
override
198
{
199
testOk
();
200
testError
();
201
testThrow
();
202
}
203
};
204
205
BEAST_DEFINE_TESTSUITE
(fillJson,
rpc
,
xrpl
);
206
207
}
// namespace xrpl::rpc
algorithm
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::expect
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition
suite.h:235
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::rpc::codeString_test
Definition
Status_test.cpp:18
xrpl::rpc::codeString_test::testOk
void testOk()
Definition
Status_test.cpp:28
xrpl::rpc::codeString_test::run
void run() override
Runs the suite.
Definition
Status_test.cpp:79
xrpl::rpc::codeString_test::testError
void testError()
Definition
Status_test.cpp:58
xrpl::rpc::codeString_test::codeString
std::string codeString(Type t)
Definition
Status_test.cpp:22
xrpl::rpc::fillJson_test
Definition
Status_test.cpp:89
xrpl::rpc::fillJson_test::expectFill
void expectFill(std::string const &label, Type status, Status::Strings messages, std::string const &message)
Definition
Status_test.cpp:123
xrpl::rpc::fillJson_test::fillJson
void fillJson(Type t)
Definition
Status_test.cpp:95
xrpl::rpc::fillJson_test::value_
json::Value value_
Definition
Status_test.cpp:91
xrpl::rpc::fillJson_test::testOk
void testOk()
Definition
Status_test.cpp:102
xrpl::rpc::fillJson_test::testThrow
void testThrow()
Definition
Status_test.cpp:175
xrpl::rpc::fillJson_test::testError
void testError()
Definition
Status_test.cpp:160
xrpl::rpc::fillJson_test::run
void run() override
Runs the suite.
Definition
Status_test.cpp:197
cstddef
exception
std::min
T min(T... args)
xrpl::rpc
API version numbers used in later API versions.
Definition
ApiVersion.h:36
xrpl::rpc::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountLines, rpc, xrpl)
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::RpcSuccess
@ RpcSuccess
Definition
ErrorCodes.h:27
xrpl::RpcBadSyntax
@ RpcBadSyntax
Definition
ErrorCodes.h:29
xrpl::temBAD_PATH
@ temBAD_PATH
Definition
TER.h:84
xrpl::temBAD_AMOUNT
@ temBAD_AMOUNT
Definition
TER.h:77
xrpl::tesSUCCESS
@ tesSUCCESS
Definition
TER.h:250
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::vector::size
T size(T... args)
string
xrpl::rpc::Status
Status represents the results of an operation that might fail.
Definition
Status.h:27
xrpl::rpc::Status::toTER
TER toTER() const
Returns the Status as a TER.
Definition
Status.h:89
xrpl::rpc::Status::Strings
std::vector< std::string > Strings
Definition
Status.h:31
xrpl::rpc::Status::fillJson
void fillJson(json::Value &)
Fill a json::Value with an RPC 2.0 response.
Definition
Status.cpp:48
xrpl::rpc::Status::messages
Strings const & messages() const
Definition
Status.h:127
xrpl::rpc::Status::kOK
static constexpr Code kOK
Definition
Status.h:33
xrpl::rpc::Status::codeString
std::string codeString() const
If the Status is OK, the result is an empty string.
Definition
Status.cpp:15
std::to_string
T to_string(T... args)
Generated by
1.17.0