| 50 | BOOST_FIXTURE_TEST_SUITE(settings_tests, BasicTestingSetup) |
| 51 | |
| 52 | BOOST_AUTO_TEST_CASE(ReadWrite) |
| 53 | { |
| 54 | fs::path path = m_args.GetDataDirBase() / "settings.json"; |
| 55 | |
| 56 | WriteText(path, R"({ |
| 57 | "string": "string", |
| 58 | "num": 5, |
| 59 | "bool": true, |
| 60 | "null": null |
| 61 | })"); |
| 62 | |
| 63 | std::map<std::string, util::SettingsValue> expected{ |
| 64 | {"string", "string"}, |
| 65 | {"num", 5}, |
| 66 | {"bool", true}, |
| 67 | {"null", {}}, |
| 68 | }; |
| 69 | |
| 70 | // Check file read. |
| 71 | std::map<std::string, util::SettingsValue> values; |
| 72 | std::vector<std::string> errors; |
| 73 | BOOST_CHECK(util::ReadSettings(path, values, errors)); |
| 74 | BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end()); |
| 75 | BOOST_CHECK(errors.empty()); |
| 76 | |
| 77 | // Check no errors if file doesn't exist. |
| 78 | fs::remove(path); |
| 79 | BOOST_CHECK(util::ReadSettings(path, values, errors)); |
| 80 | BOOST_CHECK(values.empty()); |
| 81 | BOOST_CHECK(errors.empty()); |
| 82 | |
| 83 | // Check duplicate keys not allowed |
| 84 | WriteText(path, R"({ |
| 85 | "dupe": "string", |
| 86 | "dupe": "dupe" |
| 87 | })"); |
| 88 | BOOST_CHECK(!util::ReadSettings(path, values, errors)); |
| 89 | std::vector<std::string> dup_keys = {strprintf("Found duplicate key dupe in settings file %s", fs::PathToString(path))}; |
| 90 | BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), dup_keys.begin(), dup_keys.end()); |
| 91 | |
| 92 | // Check non-kv json files not allowed |
| 93 | WriteText(path, R"("non-kv")"); |
| 94 | BOOST_CHECK(!util::ReadSettings(path, values, errors)); |
| 95 | std::vector<std::string> non_kv = {strprintf("Found non-object value \"non-kv\" in settings file %s", fs::PathToString(path))}; |
| 96 | BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), non_kv.begin(), non_kv.end()); |
| 97 | |
| 98 | // Check invalid json not allowed |
| 99 | WriteText(path, R"(invalid json)"); |
| 100 | BOOST_CHECK(!util::ReadSettings(path, values, errors)); |
| 101 | std::vector<std::string> fail_parse = {strprintf("Unable to parse settings file %s", fs::PathToString(path))}; |
| 102 | BOOST_CHECK_EQUAL_COLLECTIONS(errors.begin(), errors.end(), fail_parse.begin(), fail_parse.end()); |
| 103 | } |
| 104 | |
| 105 | //! Check settings struct contents against expected json strings. |
| 106 | static void CheckValues(const util::Settings& settings, const std::string& single_val, const std::string& list_val) |
nothing calls this directly
no test coverage detected