Skip to content

Commit

Permalink
Update C++ Properties for vector support
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Jan 8, 2024
1 parent 968cc8a commit 5963dbc
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 39 deletions.
120 changes: 120 additions & 0 deletions libs/utils/gtest/src/CxxPropertiesTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,125 @@ TEST_F(CxxPropertiesTestSuite, GetAsVersionTest) {
EXPECT_EQ(props.getAsVersion("key", celix::Version{4, 5, 6}), ver);
}


TEST_F(CxxPropertiesTestSuite, GetTest) {
celix::Properties props{};

props.set("key1", "value1"); //string
props.set("key2", 2); //long
props.set("key3", 3.3); //double
props.set("key4", true); //bool
props.set("key5", celix::Version{1, 2, 3}); //version

//Test get with valid key
EXPECT_EQ(props.get("key1"), "value1");
EXPECT_EQ(props.getAsLong("key2", -1), 2);
EXPECT_EQ(props.getAsDouble("key3", -1), 3.3);
EXPECT_EQ(props.getAsBool("key4", false), true);
celix::Version checkVersion{1, 2, 3};
EXPECT_EQ(props.getAsVersion("key5", celix::Version{1, 2, 4}), checkVersion);

// Test get with invalid key and default value
EXPECT_EQ(props.get("non_existent_key", "default_value"), "default_value");
EXPECT_EQ(props.getLong("non_existent_key", 1), 1);
EXPECT_EQ(props.getDouble("non_existent_key", 1.1), 1.1);
EXPECT_EQ(props.getBool("non_existent_key", true), true);
celix::Version checkVersion2{1, 2, 4};
EXPECT_EQ(props.getVersion("non_existent_key", checkVersion2), checkVersion2);

// Test get with an existing key, but invalid type and default value
EXPECT_EQ(props.get("key5", "default_value"), "1.2.3"); // Note get always returns the string value or string
// representation of the value (in this case the version)
EXPECT_EQ(props.getLong("key1", 1), 1); //key1 is a string
EXPECT_EQ(props.getDouble("key1", 1.1), 1.1); //key1 is a string
EXPECT_EQ(props.getBool("key1", true), true); //key1 is a string
EXPECT_EQ(props.getVersion("key1", checkVersion2), checkVersion2); //key1 is a string
}

TEST_F(CxxPropertiesTestSuite, ArrayListTest) {
celix::Properties props{};
EXPECT_EQ(0, props.size());

// Test set
props.setStrings("key1", {"value1", "value2"});
props.setLongs("key2", {1, 2});
props.setDoubles("key3", {1.1, 2.2});
props.setBooleans("key4", {true, false});
props.setVersions("key5", {celix::Version{1, 2, 3}, celix::Version{2, 3, 4}});
EXPECT_EQ(5, props.size());

// Test getAs with valid key
auto strings = props.getAsStringVector("key1");
EXPECT_EQ(strings.size(), 2);
EXPECT_EQ(strings[0], "value1");
auto longs = props.getAsLongVector("key2");
EXPECT_EQ(longs.size(), 2);
EXPECT_EQ(longs[0], 1);
auto doubles = props.getAsDoubleVector("key3");
EXPECT_EQ(doubles.size(), 2);
EXPECT_EQ(doubles[0], 1.1);
auto booleans = props.getAsBoolVector("key4");
EXPECT_EQ(booleans.size(), 2);
EXPECT_EQ(booleans[0], true);
auto versions = props.getAsVersionVector("key5");
EXPECT_EQ(versions.size(), 2);
celix::Version checkVersion{1, 2, 3};
EXPECT_EQ(versions[0], checkVersion);

// Test getAs with invalid key and default value
strings = props.getAsStringVector("non_existent_key", {"default_value"});
EXPECT_EQ(strings.size(), 1);
EXPECT_EQ(strings[0], "default_value");
longs = props.getAsLongVector("non_existent_key", {1});
EXPECT_EQ(longs.size(), 1);
EXPECT_EQ(longs[0], 1);
doubles = props.getAsDoubleVector("non_existent_key", {1.1});
EXPECT_EQ(doubles.size(), 1);
EXPECT_EQ(doubles[0], 1.1);
booleans = props.getAsBoolVector("non_existent_key", {true});
EXPECT_EQ(booleans.size(), 1);
EXPECT_EQ(booleans[0], true);
versions = props.getAsVersionVector("non_existent_key", {celix::Version{1, 2, 3}});
EXPECT_EQ(versions.size(), 1);
EXPECT_EQ(versions[0], checkVersion);


// Test get with a valid key
strings = props.getStringVector("key1");
EXPECT_EQ(strings.size(), 2);
EXPECT_EQ(strings[1], "value2");
longs = props.getLongVector("key2");
EXPECT_EQ(longs.size(), 2);
EXPECT_EQ(longs[1], 2);
doubles = props.getDoubleVector("key3");
EXPECT_EQ(doubles.size(), 2);
EXPECT_EQ(doubles[1], 2.2);
booleans = props.getBoolVector("key4");
EXPECT_EQ(booleans.size(), 2);
EXPECT_EQ(booleans[1], false);
versions = props.getVersionVector("key5");
EXPECT_EQ(versions.size(), 2);
celix::Version checkVersion2{2, 3, 4};
EXPECT_EQ(versions[1], checkVersion2);

// Test get with an existing key, but invalid type and default value
strings = props.getStringVector("key5", {"default_value"}); //key5 is a version
EXPECT_EQ(strings.size(), 1);
EXPECT_EQ(strings[0], "default_value");
longs = props.getLongVector("key1", {1}); //key1 is a string
EXPECT_EQ(longs.size(), 1);
EXPECT_EQ(longs[0], 1);
doubles = props.getDoubleVector("key1", {1.1}); //key1 is a string
EXPECT_EQ(doubles.size(), 1);
EXPECT_EQ(doubles[0], 1.1);
booleans = props.getBoolVector("key1", {true}); //key1 is a string
EXPECT_EQ(booleans.size(), 1);
EXPECT_EQ(booleans[0], true);
versions = props.getVersionVector("key1", {celix::Version{1, 2, 3}}); //key1 is a string
EXPECT_EQ(versions.size(), 1);
EXPECT_EQ(versions[0], checkVersion);
}

TEST_F(CxxPropertiesTestSuite, StoreAndLoadTest) {
std::string path{"cxx_store_and_load_test.properties"};

Expand All @@ -200,6 +319,7 @@ TEST_F(CxxPropertiesTestSuite, StoreAndLoadTest) {

try {
loadedProps = celix::Properties::load("non-existence");
(void)loadedProps;
FAIL() << "Expected exception not thrown";
} catch (const celix::IOException& e) {
EXPECT_TRUE(strstr(e.what(), "Cannot load celix::Properties"));
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ TEST_F(PropertiesErrorInjectionTestSuite, LoadFailureTest) {
fclose(memStream);
}

TEST_F(PropertiesErrorInjectionTestSuite, SetWithoutCopyFailureTest) {
TEST_F(PropertiesErrorInjectionTestSuite, AssignFailureTest) {
//Given a filled properties and a key and value
celix_autoptr(celix_properties_t) props = celix_properties_create();
fillOptimizationCache(props);
Expand Down
Loading

0 comments on commit 5963dbc

Please sign in to comment.