This commit is contained in:
@ -14,7 +14,6 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include <IACore/Environment.hpp>
|
||||
|
||||
#include <IACore/IATest.hpp>
|
||||
|
||||
using namespace IACore;
|
||||
@ -22,8 +21,8 @@ using namespace IACore;
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constants
|
||||
// -----------------------------------------------------------------------------
|
||||
static const char *TEST_KEY = "IA_TEST_ENV_VAR_12345";
|
||||
static const char *TEST_VAL = "Hello World";
|
||||
static constexpr const char *TEST_KEY = "IA_TEST_ENV_VAR_12345";
|
||||
static constexpr const char *TEST_VAL = "Hello World";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Test Block Definition
|
||||
@ -34,87 +33,83 @@ IAT_BEGIN_BLOCK(Core, Environment)
|
||||
// -------------------------------------------------------------------------
|
||||
// 1. Basic Set and Get (The Happy Path)
|
||||
// -------------------------------------------------------------------------
|
||||
bool TestBasicCycle()
|
||||
{
|
||||
// 1. Ensure clean slate
|
||||
Environment::Unset(TEST_KEY);
|
||||
IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||
auto test_basic_cycle() -> bool {
|
||||
// 1. Ensure clean slate
|
||||
(void)Environment::unset(TEST_KEY);
|
||||
IAT_CHECK_NOT(Environment::exists(TEST_KEY));
|
||||
|
||||
// 2. Set
|
||||
bool setRes = Environment::Set(TEST_KEY, TEST_VAL);
|
||||
IAT_CHECK(setRes);
|
||||
IAT_CHECK(Environment::Exists(TEST_KEY));
|
||||
// 2. Set
|
||||
const auto set_res = Environment::set(TEST_KEY, TEST_VAL);
|
||||
IAT_CHECK(set_res.has_value());
|
||||
IAT_CHECK(Environment::exists(TEST_KEY));
|
||||
|
||||
// 3. Find (Optional)
|
||||
auto opt = Environment::Find(TEST_KEY);
|
||||
IAT_CHECK(opt.has_value());
|
||||
IAT_CHECK_EQ(*opt, String(TEST_VAL));
|
||||
// 3. Find (Optional)
|
||||
const auto opt = Environment::find(TEST_KEY);
|
||||
IAT_CHECK(opt.has_value());
|
||||
IAT_CHECK_EQ(*opt, String(TEST_VAL));
|
||||
|
||||
// 4. Get (Direct)
|
||||
String val = Environment::Get(TEST_KEY);
|
||||
IAT_CHECK_EQ(val, String(TEST_VAL));
|
||||
// 4. Get (Direct)
|
||||
const String val = Environment::get(TEST_KEY);
|
||||
IAT_CHECK_EQ(val, String(TEST_VAL));
|
||||
|
||||
// Cleanup
|
||||
Environment::Unset(TEST_KEY);
|
||||
return TRUE;
|
||||
// Cleanup
|
||||
(void)Environment::unset(TEST_KEY);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 2. Overwriting Values
|
||||
// -------------------------------------------------------------------------
|
||||
bool TestOverwrite()
|
||||
{
|
||||
Environment::Set(TEST_KEY, "ValueA");
|
||||
IAT_CHECK_EQ(Environment::Get(TEST_KEY), String("ValueA"));
|
||||
auto test_overwrite() -> bool {
|
||||
(void)Environment::set(TEST_KEY, "ValueA");
|
||||
IAT_CHECK_EQ(Environment::get(TEST_KEY), String("ValueA"));
|
||||
|
||||
// Overwrite
|
||||
Environment::Set(TEST_KEY, "ValueB");
|
||||
IAT_CHECK_EQ(Environment::Get(TEST_KEY), String("ValueB"));
|
||||
// Overwrite
|
||||
(void)Environment::set(TEST_KEY, "ValueB");
|
||||
IAT_CHECK_EQ(Environment::get(TEST_KEY), String("ValueB"));
|
||||
|
||||
Environment::Unset(TEST_KEY);
|
||||
return TRUE;
|
||||
(void)Environment::unset(TEST_KEY);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 3. Unset Logic
|
||||
// -------------------------------------------------------------------------
|
||||
bool TestUnset()
|
||||
{
|
||||
Environment::Set(TEST_KEY, "To Be Deleted");
|
||||
IAT_CHECK(Environment::Exists(TEST_KEY));
|
||||
auto test_unset() -> bool {
|
||||
(void)Environment::set(TEST_KEY, "To Be Deleted");
|
||||
IAT_CHECK(Environment::exists(TEST_KEY));
|
||||
|
||||
bool unsetRes = Environment::Unset(TEST_KEY);
|
||||
IAT_CHECK(unsetRes);
|
||||
const auto unset_res = Environment::unset(TEST_KEY);
|
||||
IAT_CHECK(unset_res.has_value());
|
||||
|
||||
// Verify it is actually gone
|
||||
IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||
// Verify it is actually gone
|
||||
IAT_CHECK_NOT(Environment::exists(TEST_KEY));
|
||||
|
||||
// Find should return nullopt
|
||||
auto opt = Environment::Find(TEST_KEY);
|
||||
IAT_CHECK_NOT(opt.has_value());
|
||||
// Find should return nullopt
|
||||
const auto opt = Environment::find(TEST_KEY);
|
||||
IAT_CHECK_NOT(opt.has_value());
|
||||
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 4. Default Value Fallbacks
|
||||
// -------------------------------------------------------------------------
|
||||
bool TestDefaults()
|
||||
{
|
||||
const char *ghostKey = "IA_THIS_KEY_DOES_NOT_EXIST";
|
||||
auto test_defaults() -> bool {
|
||||
const char *ghost_key = "IA_THIS_KEY_DOES_NOT_EXIST";
|
||||
|
||||
// Ensure it really doesn't exist
|
||||
Environment::Unset(ghostKey);
|
||||
// Ensure it really doesn't exist
|
||||
(void)Environment::unset(ghost_key);
|
||||
|
||||
// Test Get with implicit default ("")
|
||||
String empty = Environment::Get(ghostKey);
|
||||
IAT_CHECK(empty.empty());
|
||||
// Test Get with implicit default ("")
|
||||
const String empty = Environment::get(ghost_key);
|
||||
IAT_CHECK(empty.empty());
|
||||
|
||||
// Test Get with explicit default
|
||||
String fallback = Environment::Get(ghostKey, "MyDefault");
|
||||
IAT_CHECK_EQ(fallback, String("MyDefault"));
|
||||
// Test Get with explicit default
|
||||
const String fallback = Environment::get(ghost_key, "MyDefault");
|
||||
IAT_CHECK_EQ(fallback, String("MyDefault"));
|
||||
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -122,58 +117,56 @@ bool TestDefaults()
|
||||
// -------------------------------------------------------------------------
|
||||
// Does Set(Key, "") create an existing empty variable, or unset it?
|
||||
// Standard POSIX/Windows API behavior is that it EXISTS, but is empty.
|
||||
bool TestEmptyValue()
|
||||
{
|
||||
Environment::Set(TEST_KEY, "");
|
||||
auto test_empty_value() -> bool {
|
||||
(void)Environment::set(TEST_KEY, "");
|
||||
|
||||
#if IA_PLATFORM_WINDOWS
|
||||
// Windows behavior: Setting to empty string removes the variable
|
||||
// IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||
// auto opt = Environment::Find(TEST_KEY);
|
||||
// IAT_CHECK_NOT(opt.has_value());
|
||||
// Windows behavior: Setting to empty string removes the variable
|
||||
// IAT_CHECK_NOT(Environment::exists(TEST_KEY));
|
||||
// auto opt = Environment::find(TEST_KEY);
|
||||
// IAT_CHECK_NOT(opt.has_value());
|
||||
#else
|
||||
// POSIX behavior: Variable exists but is empty
|
||||
IAT_CHECK(Environment::Exists(TEST_KEY));
|
||||
auto opt = Environment::Find(TEST_KEY);
|
||||
IAT_CHECK(opt.has_value());
|
||||
IAT_CHECK(opt->empty());
|
||||
// POSIX behavior: Variable exists but is empty
|
||||
IAT_CHECK(Environment::exists(TEST_KEY));
|
||||
const auto opt = Environment::find(TEST_KEY);
|
||||
IAT_CHECK(opt.has_value());
|
||||
IAT_CHECK(opt->empty());
|
||||
#endif
|
||||
|
||||
// Cleanup
|
||||
Environment::Unset(TEST_KEY);
|
||||
IAT_CHECK_NOT(Environment::Exists(TEST_KEY));
|
||||
// Cleanup
|
||||
(void)Environment::unset(TEST_KEY);
|
||||
IAT_CHECK_NOT(Environment::exists(TEST_KEY));
|
||||
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 6. Validation / Bad Input
|
||||
// -------------------------------------------------------------------------
|
||||
bool TestBadInput()
|
||||
{
|
||||
// Setting an empty key should fail gracefully
|
||||
bool res = Environment::Set("", "Value");
|
||||
IAT_CHECK_NOT(res);
|
||||
auto test_bad_input() -> bool {
|
||||
// Setting an empty key should fail gracefully
|
||||
const auto res = Environment::set("", "Value");
|
||||
IAT_CHECK_NOT(res.has_value());
|
||||
|
||||
// Unsetting an empty key should fail
|
||||
bool resUnset = Environment::Unset("");
|
||||
IAT_CHECK_NOT(resUnset);
|
||||
// Unsetting an empty key should fail
|
||||
const auto res_unset = Environment::unset("");
|
||||
IAT_CHECK_NOT(res_unset.has_value());
|
||||
|
||||
return TRUE;
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Registration
|
||||
// -------------------------------------------------------------------------
|
||||
IAT_BEGIN_TEST_LIST()
|
||||
IAT_ADD_TEST(TestBasicCycle);
|
||||
IAT_ADD_TEST(TestOverwrite);
|
||||
IAT_ADD_TEST(TestUnset);
|
||||
IAT_ADD_TEST(TestDefaults);
|
||||
IAT_ADD_TEST(TestEmptyValue);
|
||||
IAT_ADD_TEST(TestBadInput);
|
||||
IAT_ADD_TEST(test_basic_cycle);
|
||||
IAT_ADD_TEST(test_overwrite);
|
||||
IAT_ADD_TEST(test_unset);
|
||||
IAT_ADD_TEST(test_defaults);
|
||||
IAT_ADD_TEST(test_empty_value);
|
||||
IAT_ADD_TEST(test_bad_input);
|
||||
IAT_END_TEST_LIST()
|
||||
|
||||
IAT_END_BLOCK()
|
||||
|
||||
IAT_REGISTER_ENTRY(Core, Environment)
|
||||
IAT_REGISTER_ENTRY(Core, Environment)
|
||||
Reference in New Issue
Block a user