Skip to content

Commit

Permalink
Add basic unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltrano committed Jul 27, 2024
1 parent 9ab7484 commit 4297f78
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace Microsoft::Net::Wifi
*/
struct AccessPointAttributes
{
AccessPointAttributes() = default;

/**
* @brief Attempt to deserialize a JSON string into a map of access point attributes.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/net/wifi/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(wifi-core-test-unit
PRIVATE
Main.cxx
TestAccessPoint.cxx
TestAccessPointAttributes.cxx
TestAccessPointOperationStatus.cxx
TestIeee80211.cxx
)
Expand All @@ -18,6 +19,7 @@ target_link_libraries(wifi-core-test-unit
PRIVATE
Catch2::Catch2
magic_enum::magic_enum
nlohmann_json::nlohmann_json
plog::plog
strings
wifi-core
Expand Down
75 changes: 75 additions & 0 deletions tests/unit/net/wifi/core/TestAccessPointAttributes.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

#include <string>
#include <unordered_map>

#include <catch2/catch_test_macros.hpp>
#include <microsoft/net/wifi/AccessPointAttributes.hxx>
#include <nlohmann/json.hpp>

TEST_CASE("AccessPointAttributes JSON Serialization and Deserialization", "[wifi][core][ap][serialization]")
{
using namespace Microsoft::Net::Wifi;

auto accessPointAttributesJson = R"(
{
"wlan0": {
"Properties": {
"key1": "value1",
"key2": "value2"
}
},
"wlan1": {
"Properties": {
"key1": "value1",
"key2": "value2"
}
}
}
)";

// clang-format off
std::unordered_map<std::string, AccessPointAttributes> AccessPointAttributesMap{
{
"wlan0",
AccessPointAttributes{
{
{ "key1", "value1" },
{ "key2", "value2" }
}
},
},
{
"wlan1",
AccessPointAttributes{
{
{ "key1", "value1" },
{ "key2", "value2" }
}
}
}
};
// clang-format on

SECTION("Deserialization (direct) doesn't cause a crash")
{
REQUIRE_NOTHROW(nlohmann::json::parse(accessPointAttributesJson));
}

SECTION("Deserialization (wrapped) doesn't cause a crash")
{
REQUIRE_NOTHROW(AccessPointAttributes::TryParseJson(accessPointAttributesJson));
}

SECTION("Deserialization (wrapped) populates the access point attributes")
{
auto deserializedAccessPointAttributesOpt = AccessPointAttributes::TryParseJson(accessPointAttributesJson);
REQUIRE(deserializedAccessPointAttributesOpt.has_value());
auto& deserializedAccessPointAttributes = deserializedAccessPointAttributesOpt.value();
REQUIRE(std::size(deserializedAccessPointAttributes) == std::size(AccessPointAttributesMap));

for (const auto& [interfaceName, accessPointAttributes] : AccessPointAttributesMap) {
REQUIRE(deserializedAccessPointAttributes.contains(interfaceName));
REQUIRE(deserializedAccessPointAttributes[interfaceName].Properties == accessPointAttributes.Properties);
}
}
}

0 comments on commit 4297f78

Please sign in to comment.