Skip to content

Commit

Permalink
refactor invalid value tests
Browse files Browse the repository at this point in the history
Several tests are aimed at the handling of invalid values, resulting in
duplication of test code for each function that needs to be tested. This
change adds a common location for invalid values, so that these tests can
simply iterate through all invalid items instead of re-implementing each
test.

The helper function load_corpus_folder work generally, and this change uses
it to refactor several tests for invalid param names.
  • Loading branch information
vasiliyk authored Nov 9, 2024
1 parent b638f77 commit d54bf63
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 169 deletions.
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 3.2.3)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()
Expand Down Expand Up @@ -844,7 +847,9 @@ add_function_test(current_target
)

add_function_test(element
SOURCES test/function/element.cpp
SOURCES
test/function/element.cpp
$<TARGET_OBJECTS:test_helper_fixture>
)

add_function_test(element_leak
Expand Down Expand Up @@ -1055,7 +1060,9 @@ add_function_test(memory
)

add_function_test(param
SOURCES ${PROJECT_SOURCE_DIR}/test/function/param.cpp
SOURCES
${PROJECT_SOURCE_DIR}/test/function/param.cpp
$<TARGET_OBJECTS:test_helper_fixture>
)

add_function_test(perror
Expand Down
14 changes: 14 additions & 0 deletions include/test/helper/fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ create_nil_entry( void );
const char *
load_corpus( const std::string& name );

/**
* Returns stumpless_test_data holding the contents of all the files at the named
* location in the test/corpora folder. For example, a name of "invalid_param_name"
* will return the contents of all the files under test/corpora/invalid_param_name
* directory in the form of an array.
*
*
* @param name The directory name.
*
* @return vector of strings holding test values.
*/
std::vector<std::string>
load_corpus_folder( const std::string& name );

#endif /* __STUMPLESS_TEST_HELPER_FIXTURE_HPP */
1 change: 1 addition & 0 deletions test/corpora/invalid_param_name/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
par=am
1 change: 1 addition & 0 deletions test/corpora/invalid_param_name/2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
par]am
1 change: 1 addition & 0 deletions test/corpora/invalid_param_name/3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
par\"am
87 changes: 32 additions & 55 deletions test/function/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

#include <cstddef>
#include <cstdlib>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include "test/helper/fixture.hpp"
#include "test/helper/memory_allocation.hpp"

using::testing::HasSubstr;
Expand Down Expand Up @@ -351,18 +353,13 @@ namespace {

TEST_F( ElementTest, GetParamByNameInvalidName ) {
const struct stumpless_param *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_param_by_name( element_with_params, "par=am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_param_by_name( element_with_params, "par]am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_param_by_name( element_with_params, "par\"am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_param_by_name( element_with_params, invalid_name.c_str() );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( ElementTest, GetParamCount ) {
Expand Down Expand Up @@ -557,18 +554,13 @@ namespace {

TEST_F( ElementTest, GetParamValueByNameInvalidName ) {
const char *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_param_value_by_name( element_with_params, "par=am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_param_value_by_name( element_with_params, "par]am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_param_value_by_name( element_with_params, "par\"am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_param_value_by_name( element_with_params, invalid_name.c_str() );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( ElementTest, HasParam ) {
Expand All @@ -593,18 +585,13 @@ namespace {

TEST_F( ElementTest, HasParamInvalidName ) {
bool result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_element_has_param( element_with_params, "par=am" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_element_has_param( element_with_params, "para]m" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_element_has_param( element_with_params, "pa\"ram" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_element_has_param( element_with_params, invalid_name.c_str() );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( ElementTest, HighParamCount ) {
Expand Down Expand Up @@ -1034,19 +1021,14 @@ namespace {

TEST( NewElementTest, InvalidName ) {
struct stumpless_element *element;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

element = stumpless_new_element( "ele=ment" );
EXPECT_NULL( element );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

element = stumpless_new_element( "element]" );
EXPECT_NULL( element );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
element = stumpless_new_element( invalid_name.c_str() );
EXPECT_NULL( element );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

element = stumpless_new_element( "El\"ment" );
EXPECT_NULL( element );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

stumpless_free_all( );
}

Expand All @@ -1073,22 +1055,17 @@ namespace {
TEST( SetElementNameTest, InvalidName) {
struct stumpless_element *element;
struct stumpless_element *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

element = stumpless_new_element( "element" );
ASSERT_NOT_NULL( element );

result = stumpless_set_element_name( element, "ele=ment");
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_set_element_name( element, invalid_name.c_str() );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}

result = stumpless_set_element_name( element, "eleme]nt");
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_set_element_name( element, "element\"");
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

stumpless_destroy_element_and_contents( element );
stumpless_free_all( );
}
Expand Down
122 changes: 44 additions & 78 deletions test/function/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

#include <string>
#include <cstddef>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -596,34 +597,24 @@ namespace {

TEST_F( EntryTest, GetParamByNameInvalidParamName ) {
const struct stumpless_param *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_entry_param_by_name( basic_entry, "e-name", "par=am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_by_name( basic_entry, "e-name", "par]am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_by_name( basic_entry, "e-name", "par\"am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_entry_param_by_name( basic_entry, "e-name", invalid_name.c_str() );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, GetParamByNameInvalidElementName ) {
const struct stumpless_param *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_entry_param_by_name( basic_entry, "ele=ment", "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_by_name( basic_entry, "ele]ment", "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_by_name( basic_entry, "e-name\"", "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_entry_param_by_name( basic_entry, invalid_name.c_str(), "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, GetParamValueByIndex ) {
Expand Down Expand Up @@ -689,34 +680,24 @@ namespace {

TEST_F( EntryTest, GetParamValueByNameInvalidParamName ) {
const char *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_entry_param_value_by_name( basic_entry, "e-name", "par=am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_value_by_name( basic_entry, "e-name", "par]am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_value_by_name( basic_entry, "e-name", "par\"am" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_entry_param_value_by_name( basic_entry, "e-name", invalid_name.c_str() );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, GetParamValueByNameInvalidElementName ) {
const char *result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_entry_param_value_by_name( basic_entry, "e=name", "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_value_by_name( basic_entry, "e]name", "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_entry_param_value_by_name( basic_entry, "\"e-name", "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_entry_param_value_by_name( basic_entry, invalid_name.c_str(), "param" );
EXPECT_NULL( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, HasElement ) {
Expand Down Expand Up @@ -745,50 +726,35 @@ namespace {

TEST_F( EntryTest, HasElementInvalidName ) {
bool result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_entry_has_element( basic_entry, "ele=ment" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_entry_has_element( basic_entry, "ele]ment" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_entry_has_element( basic_entry, "element\"" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_entry_has_element( basic_entry, invalid_name.c_str() );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, GetElementInvalidName ) {
bool result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_element_by_name( basic_entry, "ele=ment" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_element_by_name( basic_entry, "ele]ment" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_element_by_name( basic_entry, "element\"" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_element_by_name( basic_entry, invalid_name.c_str() );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, GetElementIdxInvalidName ) {
bool result;
std::vector<std::string> invalid_names = load_corpus_folder("invalid_param_name");

result = stumpless_get_element_index( basic_entry, "ele=ment" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_element_index( basic_entry, "ele]ment" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );

result = stumpless_get_element_index( basic_entry, "element\"" );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
for(const auto& invalid_name : invalid_names) {
result = stumpless_get_element_index( basic_entry, invalid_name.c_str() );
EXPECT_FALSE( result );
EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_ENCODING );
}
}

TEST_F( EntryTest, SetAppName ) {
Expand Down Expand Up @@ -1740,7 +1706,7 @@ namespace {
stumpless_free_all( );
}

TEST( NewEntryStrTest, New ){
TEST( NewEntryStrTest, New ) {
struct stumpless_entry *entry;
const char *app_name = "test-app-name";
const char *msgid = "test-msgid";
Expand Down
Loading

0 comments on commit d54bf63

Please sign in to comment.