cpp-ipfs-http-client: /media/melroy/Data/Projects/cpp-ipfs-http-client/include/ipfs/test/utils.h Source File

cpp-ipfs-http-client IPFS C++ client library

utils.h
1 /* Copyright (c) 2016-2019, The C++ IPFS client library developers
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7 the Software, and to permit persons to whom the Software is furnished to do so,
8 subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19
20 #ifndef IPFS_TEST_UTILS_H
21 #define IPFS_TEST_UTILS_H
22
23 #include <ipfs/client.h>
24
25 #include <functional>
26 #include <iomanip>
27 #include <sstream>
28 #include <stdexcept>
29 #include <string>
30 #include <vector>
31
32 namespace ipfs {
33
34 namespace test {
35
37 inline void check_if_properties_exist(
39 const std::string& label,
41 const ipfs::Json& j,
43 const std::vector<const char*>& properties) {
44 for (const char* property : properties) {
45 if (j.find(property) == j.end()) {
46 throw std::runtime_error(label + ": the property \"" + property +
47 "\" was not found in the response:\n" +
48 j.dump(2));
49 }
50 }
51 }
52
55 inline void check_if_string_contains(
57 const std::string& label,
59 const std::string& big,
61 const std::string& needle) {
62 if (big.find(needle) == big.npos) {
63 throw std::runtime_error(label + ": \"" + needle +
64 "\" was not found in the response:\n" + big);
65 }
66 }
67
69 inline std::string string_to_hex(
71 const std::string& input) {
72 std::stringstream ss;
73 ss << std::hex;
74 for (size_t i = 0; i < input.length(); ++i) {
75 ss << std::setw(2) << std::setfill('0') << static_cast<int>(input[i]);
76 }
77 return ss.str();
78 }
79
81 inline void must_fail(
83 const std::string& label,
85 std::function<void()> f) {
86 bool succeeded;
87
88 try {
89 f();
90 succeeded = true;
91 } catch (const std::exception& e) {
92 std::cout << label + " failed as expected with error message: " << e.what()
93 << std::endl;
94 succeeded = false;
95 }
96
97 if (succeeded) {
98 throw std::runtime_error(label + " succeeded but should have failed.");
99 }
100 }
101
102 } /* namespace test */
103 } /* namespace ipfs */
104 #endif /* IPFS_TEST_UTILS_H */
ipfs::Json
nlohmann::json Json
Type of the output of some methods, aliased for convenience.
Definition: client.h:36
ipfs
IPFS namespace.
Definition: client.h:32