Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test_configuration_utils.cpp
Go to the documentation of this file.
1#define BOOST_TEST_MODULE trace_configuration_utils
2#include <boost/test/included/unit_test.hpp>
3#include <list>
4#include <boost/filesystem.hpp>
5#include <boost/filesystem/fstream.hpp>
6
9
10using namespace sysio;
12
13namespace bfs = boost::filesystem;
14
15struct temp_file_fixture {
17
19 for (const auto& p: paths) {
20 if (bfs::exists(p)) {
21 bfs::remove(p);
22 }
23 }
24 }
25
26 std::string create_temp_file( const std::string& contents ) {
27 auto path = bfs::temp_directory_path() / bfs::unique_path();
28 auto os = bfs::ofstream(path, std::ios_base::out);
29 os << contents;
30 os.close();
31 return paths.emplace_back(std::move(path)).generic_string();
32 }
33
34 std::list<bfs::path> paths;
35};
36
37BOOST_AUTO_TEST_SUITE(configuration_utils_tests)
38 BOOST_AUTO_TEST_CASE(parse_kv_pairs_test)
39 {
40 using spair = std::pair<std::string, std::string>;
41
42 // basic
43 BOOST_TEST( parse_kv_pairs("a=b") == spair("a", "b") );
44 BOOST_TEST( parse_kv_pairs("a==b") == spair("a", "=b") );
45 BOOST_TEST( parse_kv_pairs("a={}:\"=") == spair("a", "{}:\"=") );
46 BOOST_TEST( parse_kv_pairs("{}:\"=a") == spair("{}:\"", "a") );
47
48 // missing key not OK
49 BOOST_REQUIRE_THROW(parse_kv_pairs("=b"), chain::plugin_config_exception);
50
51 // missing value not OK
52 BOOST_REQUIRE_THROW(parse_kv_pairs("a="), chain::plugin_config_exception);
53
54 // missing = not OK
55 BOOST_REQUIRE_THROW(parse_kv_pairs("a"), chain::plugin_config_exception);
56
57 // emptynot OK
58 BOOST_REQUIRE_THROW(parse_kv_pairs(""), chain::plugin_config_exception);
59
60 }
61
63 {
64 auto data_dir = fc::path(bfs::temp_directory_path());
65 auto good_json = std::string("{\"version\" : \"test string please ignore\"}");
66 auto good_json_filename = create_temp_file(good_json);
67 auto relative_json_filename = bfs::path(good_json_filename).filename().generic_string();
68
69 auto good_abi = chain::abi_def();
70 good_abi.version = "test string please ignore";
71
72 auto bad_json = std::string("{{\"version\":oops\"}");
73 auto bad_json_filename = create_temp_file(bad_json);
74 auto bad_filename = (bfs::temp_directory_path() / bfs::unique_path()).generic_string();
75 auto directory_name = bfs::temp_directory_path().generic_string();
76
77 // good cases
78 BOOST_TEST( abi_def_from_file(good_json_filename, data_dir) == good_abi );
79 BOOST_TEST( abi_def_from_file(relative_json_filename, data_dir) == good_abi );
80
81 // bad cases
82 BOOST_REQUIRE_THROW( abi_def_from_file(bad_json_filename, data_dir), chain::json_parse_exception );
83 BOOST_REQUIRE_THROW( abi_def_from_file(bad_filename, data_dir), chain::plugin_config_exception );
84 BOOST_REQUIRE_THROW( abi_def_from_file(directory_name, data_dir), chain::plugin_config_exception );
85 }
86
87BOOST_AUTO_TEST_SUITE_END()
const mie::Vuint & p
Definition bn.cpp:27
wraps boost::filesystem::path to provide platform independent path manipulation.
os_t os
std::pair< std::string, std::string > parse_kv_pairs(const std::string &input)
chain::abi_def abi_def_from_file(const std::string &file_name, const fc::path &data_dir)
std::string create_temp_file(const std::string &contents)
std::list< bfs::path > paths
BOOST_FIXTURE_TEST_CASE(abi_def_from_file_test, temp_file_fixture)
BOOST_AUTO_TEST_CASE(parse_kv_pairs_test)