Skip to content

Commit

Permalink
created test for json util
Browse files Browse the repository at this point in the history
  • Loading branch information
ricemaking committed Nov 16, 2024
1 parent a8de6bc commit 8375449
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,15 @@ if(WITH_TESTS)
../tests/util/CoreTest.cpp
../tests/util/DataTest.cpp
../tests/util/MathTest.cpp
../tests/util/RandomTest.cpp
../tests/util/TimeTest.cpp
../tests/util/SchedulerTest.cpp
../tests/util/RandomTest.cpp
../tests/util/JsonTest.cpp
# Protocol/teleop tests
../tests/kinematics/DiffWristKinematicsTest.cpp)

target_link_libraries(tests
${rover_libs}
stub_world_interface double sample0 = stdn(0);
double sample1 = stdn(1);

REQUIRE(sample0 != sample1);
${OpenCV_LIBS})
include(CTest)
include(Catch)
Expand Down
65 changes: 65 additions & 0 deletions tests/util/JsonTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "../../src/utils/json.h"

#include <catch2/catch.hpp>

using namespace util;

TEST_CASE("Test Has Key", "[util][json]") {
json j = {{"name", "huskyRobos"}, {"location", "University of Washington"}};

REQUIRE(hasKey(j, "name")); // has key of 'name'
REQUIRE_FALSE(hasKey(j, "numLosses")); // doesn't have key of 'numLosses'
}

TEST_CASE("Test Validate Key (Given Individual Types)", "[util][json]") {
json j = {{"name", "huskyRobos"}, {"age", 11}, {"cool", true}};
REQUIRE(validateKey(j, "name", val_t::string)); // has key of 'name' that is a string
REQUIRE_FALSE(
validateKey(j, "age", val_t::string)); // doesn't have key of 'age' that is a string
REQUIRE(validateKey(j, "cool", val_t::boolean)); // has key of 'cool' that is a boolean
}

TEST_CASE("Test Validate Key (Given Set of Types)", "[util][json]") {
json j = {{"name", "huskyRobos"}, {"age", 11}, {"cool", true}};

std::unordered_set<val_t> keyTypesAllowed = {val_t::string, val_t::number_integer};

REQUIRE(validateKey(
j, "name", keyTypesAllowed)); // has key of 'name' that is a type of the allowed types
REQUIRE(validateKey(
j, "age", keyTypesAllowed)); // has key of 'age' that is a type of the allowed types
REQUIRE_FALSE(validateKey(
j, "cool",
keyTypesAllowed)); // doesn't have key of 'cool' that is a type of the allowed types
}

TEST_CASE("Test Validate One Of", "[util][json]") {
json j = {{"status", "active"}, {"subTeams", "software"}};

std::unordered_set<std::string> statusValueTypesAllowed = {"active", "inactive"};
std::unordered_set<std::string> subTeamValueTypesAllowed = {"software", "electrical",
"mechanical"};

REQUIRE(validateOneOf(j, "status",
statusValueTypesAllowed)); // value of 'status' is a string in set of
// statusValueTypesAllowed
REQUIRE_FALSE(validateOneOf(
j, "status", {"pending", "closed"})); // value of 'status' is a string but not in set
// of statusValueTypesAllowed
REQUIRE(validateOneOf(j, "subTeams",
subTeamValueTypesAllowed)); // value of 'subTeams' is a string in set
// of subTeamValueTypesAllowed
}

TEST_CASE("Test Validate Range", "[util][json]") {
json j = {
{"winLossRatio", 0.8375},
{"avgGPA", 3.6},
};

REQUIRE(validateRange(j, "winLossRatio", 0.0,
1.0)); // 'winLossRatio' is in range from 0.0 to 1.0
REQUIRE(validateRange(j, "avgGPA", 0.0, 4.0)); // 'avgGPA' is in range from 0.0 to 4.0
REQUIRE_FALSE(
validateRange(j, "avgGPA", 0.0, 2.0)); // 'avgGPA' isnt in range of 0.0 to 2.0
}

0 comments on commit 8375449

Please sign in to comment.