cpp-ipfs-http-client
IPFS C++ client library
Loading...
Searching...
No Matches
utils.h
1/* Copyright (c) 2016-2019, The C++ IPFS client library developers
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of
4this software and associated documentation files (the "Software"), to deal in
5the Software without restriction, including without limitation the rights to
6use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7the Software, and to permit persons to whom the Software is furnished to do so,
8subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all
11copies or substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18CONNECTION 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
32namespace ipfs {
33
34namespace test {
35
37inline 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
55inline 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
69inline 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
81inline 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 namespace.
Definition client.h:33
nlohmann::json Json
Type of the output of some methods, aliased for convenience.
Definition client.h:37