Skip to content

Commit

Permalink
Added unit testing and function comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Jun 11, 2024
1 parent f026ca5 commit b1393ec
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/tmx/TmxUtils/src/simulation/SimulationEnvUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <simulation/SimulationEnvUtils.h>
#include "simulation/SimulationEnvUtils.h"

namespace tmx::utils::sim{
bool is_simulation_mode() {
Expand Down
8 changes: 7 additions & 1 deletion src/tmx/TmxUtils/src/simulation/SimulationEnvUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ namespace tmx::utils::sim{
* @return true if SIMULATION_MODE is "true" or "TRUE" and false otherwise.
*/
bool is_simulation_mode();

/**
* @brief Get simulation configuration.
* @param config_name Name of configuration parameter
* @param required bool flag whether configuration paramter is required (Default: true)
* @throws tmx::TmxException if required configuration is unset or system not in simulation mode.
* @return string value of sim configuration
*/
std::string get_sim_config(const char *config_name, bool required = true);

}
71 changes: 71 additions & 0 deletions src/tmx/TmxUtils/test/TestSimulationEnvUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <simulation/SimulationEnvUtils.h>
#include <gtest/gtest.h>
#include <stdlib.h>


TEST(testSimulationEnvUtils, is_simulation_mode_unset)
{
// Unset any environment set SIMULATION_MODE
unsetenv(tmx::utils::sim::SIMULATION_MODE);
EXPECT_FALSE(tmx::utils::sim::is_simulation_mode());
}

TEST(testSimulationEnvUtils, is_simulation_mode_true) {
setenv(tmx::utils::sim::SIMULATION_MODE, "true", 1);
EXPECT_TRUE(tmx::utils::sim::is_simulation_mode());
setenv(tmx::utils::sim::SIMULATION_MODE, "TRUE", 1);
EXPECT_TRUE(tmx::utils::sim::is_simulation_mode());
}

TEST(testSimulationEnvUtils, is_simulation_mode_false) {
setenv(tmx::utils::sim::SIMULATION_MODE, "false", 1);
EXPECT_FALSE(tmx::utils::sim::is_simulation_mode());
setenv(tmx::utils::sim::SIMULATION_MODE, "FALSE", 1);
EXPECT_FALSE(tmx::utils::sim::is_simulation_mode());
}

TEST(testSimulationEnvUtils, get_sim_config_non_simulation_mode) {
// Precondition for test (ASSERT)
ASSERT_FALSE(tmx::utils::sim::is_simulation_mode());
EXPECT_THROW(tmx::utils::sim::get_sim_config(tmx::utils::sim::SIMULATION_IP), tmx::TmxException );

}

TEST(testSimulationEnvUtils, get_sim_config_unset) {
// Precondition for test (ASSERT)
setenv(tmx::utils::sim::SIMULATION_MODE, "true", 1);
unsetenv(tmx::utils::sim::SIMULATION_IP);
ASSERT_TRUE(tmx::utils::sim::is_simulation_mode());

EXPECT_THROW(tmx::utils::sim::get_sim_config(tmx::utils::sim::SIMULATION_IP), tmx::TmxException );
}

TEST(testSimulationEnvUtils, get_sim_config_set) {
// Precondition for test (ASSERT)
std::string simulation_ip = "127.0.0.1";
setenv(tmx::utils::sim::SIMULATION_MODE, "true", 1);
setenv(tmx::utils::sim::SIMULATION_IP, simulation_ip.c_str(), 1);
ASSERT_TRUE(tmx::utils::sim::is_simulation_mode());

EXPECT_EQ(tmx::utils::sim::get_sim_config(tmx::utils::sim::SIMULATION_IP), simulation_ip );
}

TEST(testSimulationEnvUtils, get_sim_config_optional_set) {
// Precondition for test (ASSERT)
std::string simulation_ip = "127.0.0.1";
setenv(tmx::utils::sim::SIMULATION_MODE, "true", 1);
setenv(tmx::utils::sim::SIMULATION_IP, simulation_ip.c_str(), 1);
ASSERT_TRUE(tmx::utils::sim::is_simulation_mode());

EXPECT_EQ(tmx::utils::sim::get_sim_config(tmx::utils::sim::SIMULATION_IP,false), simulation_ip );
}

TEST(testSimulationEnvUtils, get_sim_config_optional_unset) {
// Precondition for test (ASSERT)
std::string simulation_ip = "127.0.0.1";
setenv(tmx::utils::sim::SIMULATION_MODE, "true", 1);
unsetenv(tmx::utils::sim::SIMULATION_IP);
ASSERT_TRUE(tmx::utils::sim::is_simulation_mode());

EXPECT_TRUE(tmx::utils::sim::get_sim_config(tmx::utils::sim::SIMULATION_IP,false).empty());
}

0 comments on commit b1393ec

Please sign in to comment.