|
1 | 1 |
|
| 2 | +#include "simplnx/Common/ScopeGuard.hpp" |
2 | 3 | #include "simplnx/Core/Application.hpp" |
3 | 4 | #include "simplnx/Core/Preferences.hpp" |
4 | 5 |
|
| 6 | +#include "simplnx/unit_test/simplnx_test_dirs.hpp" |
| 7 | + |
5 | 8 | #include <catch2/catch.hpp> |
6 | 9 |
|
7 | 10 | #include <filesystem> |
8 | 11 | #include <fstream> |
| 12 | +#include <system_error> |
9 | 13 |
|
10 | 14 | using namespace nx::core; |
11 | 15 | namespace fs = std::filesystem; |
@@ -96,6 +100,61 @@ TEST_CASE("Application::loadPreferences", "[Application]") |
96 | 100 | } |
97 | 101 | } |
98 | 102 |
|
| 103 | +TEST_CASE("Application::loadPreferences from a specified path", "[Application]") |
| 104 | +{ |
| 105 | + const fs::path tempDir = fs::path(unit_test::k_BinaryTestOutputDir.view()) / ("ApplicationPreferences_" + Uuid::GenerateV4().str()); |
| 106 | + REQUIRE(fs::create_directories(tempDir)); |
| 107 | + auto cleanup = MakeScopeGuard([&tempDir]() noexcept { |
| 108 | + std::error_code error; |
| 109 | + fs::remove_all(tempDir, error); |
| 110 | + }); |
| 111 | + const fs::path filepath = tempDir / "preferences.json"; |
| 112 | + |
| 113 | + SECTION("A missing preferences file is a normal first run") |
| 114 | + { |
| 115 | + auto app = Application::GetOrCreateInstance(); |
| 116 | + REQUIRE_FALSE(fs::exists(filepath)); |
| 117 | + |
| 118 | + const auto result = app->loadPreferences(filepath); |
| 119 | + |
| 120 | + REQUIRE(result.valid()); |
| 121 | + REQUIRE(result.warnings().empty()); |
| 122 | + REQUIRE(app->getPreferences() != nullptr); |
| 123 | + REQUIRE_FALSE(fs::exists(filepath)); |
| 124 | + } |
| 125 | + |
| 126 | + SECTION("Malformed JSON remains an application error") |
| 127 | + { |
| 128 | + { |
| 129 | + std::ofstream file(filepath); |
| 130 | + REQUIRE(file.is_open()); |
| 131 | + file << "{ not json"; |
| 132 | + file.close(); |
| 133 | + REQUIRE(file.good()); |
| 134 | + } |
| 135 | + auto app = Application::GetOrCreateInstance(); |
| 136 | + |
| 137 | + const auto result = app->loadPreferences(filepath); |
| 138 | + |
| 139 | + REQUIRE(result.invalid()); |
| 140 | + REQUIRE_FALSE(result.errors().empty()); |
| 141 | + REQUIRE(result.errors()[0].code == -1); |
| 142 | + REQUIRE(result.errors()[0].message.find("Could not load preferences") != std::string::npos); |
| 143 | + } |
| 144 | + |
| 145 | + SECTION("Preferences reports the missing-file error code directly") |
| 146 | + { |
| 147 | + Preferences preferences; |
| 148 | + REQUIRE_FALSE(fs::exists(filepath)); |
| 149 | + |
| 150 | + const auto result = preferences.loadFromFile(filepath); |
| 151 | + |
| 152 | + REQUIRE(result.invalid()); |
| 153 | + REQUIRE_FALSE(result.errors().empty()); |
| 154 | + REQUIRE(result.errors()[0].code == Preferences::k_FileDoesNotExist_Code); |
| 155 | + } |
| 156 | +} |
| 157 | + |
99 | 158 | TEST_CASE("Application::savePreferences", "[Application]") |
100 | 159 | { |
101 | 160 | auto app = Application::GetOrCreateInstance(); |
|
0 commit comments