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