Skip to content

Commit

Permalink
gh-674: Add more tests for celix_convert_utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Mar 29, 2024
1 parent be178de commit 81ee259
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
35 changes: 34 additions & 1 deletion libs/utils/gtest/src/ConvertUtilsErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TEST_F(ConvertUtilsWithErrorInjectionTestSuite, LongArrayToStringTest) {

//Given an error injection for fputs
celix_ei_expect_fputs((void*)celix_utils_arrayListToString, 1, -1);
//When calling celix_utils_longArrayListToString
//When calling celix_utils_arrayListToString
result = celix_utils_arrayListToString(list);
//Then the result is null
EXPECT_EQ(nullptr, result);
Expand All @@ -137,10 +137,43 @@ TEST_F(ConvertUtilsWithErrorInjectionTestSuite, StringToStringArrayTest) {
// Then the result is null and the status is ENOMEM
EXPECT_EQ(status, CELIX_ENOMEM);

// Given an error injection for fputc
celix_ei_expect_fputc((void*)celix_utils_convertStringToStringArrayList, 1, EOF, 2);
// When calling celix_utils_convertStringToStringArrayList
status = celix_utils_convertStringToStringArrayList("a,b,c", nullptr, &result);
// Then the result is null and the status is ENOMEM
EXPECT_EQ(status, CELIX_ENOMEM);

// Given an error injection for fputc
celix_ei_expect_fputc((void*)celix_utils_convertStringToStringArrayList, 1, EOF, 6);
// When calling celix_utils_convertStringToStringArrayList
status = celix_utils_convertStringToStringArrayList("a,b,c", nullptr, &result);
// Then the result is null and the status is ENOMEM
EXPECT_EQ(status, CELIX_ENOMEM);

// Given an error injection for fputc (on writing an escaped char)
celix_ei_expect_fputc((void*)celix_utils_convertStringToStringArrayList, 1, EOF);
// When calling celix_utils_convertStringToStringArrayList
status = celix_utils_convertStringToStringArrayList(R"(\\)", nullptr, &result);
// Then the result is null and the status is ENOMEM
EXPECT_EQ(status, CELIX_ENOMEM);
}

TEST_F(ConvertUtilsWithErrorInjectionTestSuite, StringArrayToStringTest) {
celix_autoptr(celix_array_list_t) list = celix_arrayList_createStringArray();
celix_arrayList_addString(list, ",hello");
celix_arrayList_addString(list, "world");

//Given an error injection for fputc
celix_ei_expect_fputc((void*)celix_utils_arrayListToString, 2, -1);
//When calling celix_utils_arrayListToString
char* result = celix_utils_arrayListToString(list);
//Then the result is null
EXPECT_EQ(nullptr, result);

celix_ei_expect_fputc((void*)celix_utils_arrayListToString, 2, -1, 2);
//When calling celix_utils_arrayListToString
result = celix_utils_arrayListToString(list);
//Then the result is null
EXPECT_EQ(nullptr, result);
}
16 changes: 16 additions & 0 deletions libs/utils/gtest/src/ConvertUtilsTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ TEST_F(ConvertUtilsTestSuite, ConvertToLongArrayTest) {
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, convertState);
EXPECT_TRUE(result == nullptr);

convertState = celix_utils_convertStringToLongArrayList(nullptr, nullptr, &result);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, convertState);
EXPECT_TRUE(result == nullptr);

celix_autoptr(celix_array_list_t) defaultList = celix_arrayList_create();
celix_arrayList_addLong(defaultList, 42L);
convertState = celix_utils_convertStringToLongArrayList("1,2,3,invalid", defaultList, &result);
Expand Down Expand Up @@ -384,6 +388,10 @@ TEST_F(ConvertUtilsTestSuite, ConvertToDoubleArrayList) {
EXPECT_DOUBLE_EQ(5.0, celix_arrayList_getDouble(result, 4));
celix_arrayList_destroy(result);

convertState = celix_utils_convertStringToDoubleArrayList("0.1,invalid,3.1,4,5", nullptr, &result);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, convertState);
EXPECT_TRUE(result == nullptr);

// NOTE celix_utils_convertStringToDoubleArrayList uses the same generic function as is used in
// celix_utils_convertStringToLongArrayList and because celix_utils_convertStringToLongArrayList is already
// tested, we only test a few cases here.
Expand Down Expand Up @@ -417,6 +425,10 @@ TEST_F(ConvertUtilsTestSuite, ConvertToBoolArrayList) {
EXPECT_TRUE(celix_arrayList_getBool(result, 2));
celix_arrayList_destroy(result);

convertState = celix_utils_convertStringToBoolArrayList("true,invalid,true", nullptr, &result);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, convertState);
EXPECT_TRUE(result == nullptr);

// NOTE celix_utils_convertStringToBoolArrayList uses the same generic function as is used in
// celix_utils_convertStringToLongArrayList and because celix_utils_convertStringToLongArrayList is already
// tested, we only test a few cases here.
Expand Down Expand Up @@ -515,6 +527,10 @@ TEST_F(ConvertUtilsTestSuite, ConvertToVersionArrayList) {
checkVersion(celix_arrayList_getVersion(result, 2), 3, 4, 5, "qualifier");
celix_arrayList_destroy(result);

convertState = celix_utils_convertStringToVersionArrayList("1.2.3,invalid,3.4.5.qualifier", nullptr, &result);
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, convertState);
EXPECT_TRUE(result == nullptr);

// NOTE celix_utils_convertStringToVersionArrayList uses the same generic function as is used in
// celix_utils_convertStringToLongArrayList and because celix_utils_convertStringToLongArrayList is already
// tested, we only test a few cases here.
Expand Down

0 comments on commit 81ee259

Please sign in to comment.