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 xrpl {
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
115 expectFill(std::string const& label, Type status, Status::Strings messages, std::string const& message)
116 {
117 value_.clear();
118 fillJson(Status(status, messages));
119
120 auto prefix = label + ": ";
121 expect(bool(value_), prefix + "No value");
122
123 auto error = value_[jss::error];
124 expect(bool(error), prefix + "No error.");
125
126 auto code = error[jss::code].asInt();
127 expect(status == code, prefix + "Wrong status " + std::to_string(code) + " != " + std::to_string(status));
128
129 auto m = error[jss::message].asString();
130 expect(m == message, m + " != " + message);
131
132 auto d = error[jss::data];
133 size_t s1 = d.size(), s2 = messages.size();
134 expect(s1 == s2, prefix + "Data sizes differ " + std::to_string(s1) + " != " + std::to_string(s2));
135 for (auto i = 0; i < std::min(s1, s2); ++i)
136 {
137 auto ds = d[i].asString();
138 expect(ds == messages[i], prefix + ds + " != " + messages[i]);
139 }
140 }
141
142 void
144 {
145 testcase("error");
146 expectFill("temBAD_AMOUNT", temBAD_AMOUNT, {}, "temBAD_AMOUNT: Malformed: Bad amount.");
147
148 expectFill("rpcBAD_SYNTAX", rpcBAD_SYNTAX, {"An error.", "Another error."}, "badSyntax: Syntax error.");
149
150 expectFill("integer message", 23, {"Stuff."}, "23");
151 }
152
153 void
155 {
156 testcase("throw");
157 try
158 {
159 Throw<Status>(Status(temBAD_PATH, {"path=sdcdfd"}));
160 }
161 catch (Status const& s)
162 {
163 expect(s.toTER() == temBAD_PATH, "temBAD_PATH wasn't thrown");
164 auto msgs = s.messages();
165 expect(msgs.size() == 1, "Wrong number of messages");
166 expect(msgs[0] == "path=sdcdfd", msgs[0]);
167 }
168 catch (std::exception const&)
169 {
170 expect(false, "Didn't catch a Status");
171 }
172 }
173
174public:
175 void
176 run() override
177 {
178 test_OK();
179 test_error();
180 test_throw();
181 }
182};
183
184BEAST_DEFINE_TESTSUITE(fillJson, rpc, RPC);
185
186} // namespace RPC
187} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
void clear()
Remove all object members and array elements.
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:147
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:221
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)
void run() override
Runs the suite.
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ temBAD_PATH
Definition TER.h:76
@ temBAD_AMOUNT
Definition TER.h:69
@ tesSUCCESS
Definition TER.h:225
@ rpcBAD_SYNTAX
Definition ErrorCodes.h:26
@ rpcSUCCESS
Definition ErrorCodes.h:24
T size(T... args)
Status represents the results of an operation that might fail.
Definition Status.h:20
Strings const & messages() const
Definition Status.h:100
std::string codeString() const
Definition Status.cpp:9
static constexpr Code OK
Definition Status.h:26
TER toTER() const
Returns the Status as a TER.
Definition Status.h:70
void fillJson(Json::Value &)
Fill a Json::Value with an RPC 2.0 response.
Definition Status.cpp:42
T to_string(T... args)