Skip to content

Commit

Permalink
unit test: IniParser: Add tests for exceptions on incorrect input
Browse files Browse the repository at this point in the history
  • Loading branch information
jrohel committed Aug 27, 2023
1 parent 4fc8a6c commit 72cb511
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/libdnf5/iniparser/test_iniparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,89 @@ void IniparserTest::test_iniparser2() {

parse_and_check_results(ini_file_content, expected_items);
}


void IniparserTest::test_iniparser_missing_section_header() {
constexpr std::string_view ini_file_content = "# Test comment1\nkey1 = value1";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"},
{ItemType::KEY_VAL, "", "key1", "value1", "key1 = value1\n"}};

CPPUNIT_ASSERT_THROW(
parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingSectionHeaderError);
}


void IniparserTest::test_iniparser_missing_bracket() {
constexpr std::string_view ini_file_content = "# Test comment1\n[section1\nkey1 = value1\n";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"},
{ItemType::SECTION, "section1", "", "", "[section1\n"}};

CPPUNIT_ASSERT_THROW(
parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingBracketError);
}


void IniparserTest::test_iniparser_empty_section_name() {
constexpr std::string_view ini_file_content = "# Test comment1\n[] # Test\nkey1 = value1";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, {ItemType::SECTION, "", "", "", "[] # Test\n"}};

CPPUNIT_ASSERT_THROW(
parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserEmptySectionNameError);
}


void IniparserTest::test_iniparser_text_after_section() {
constexpr std::string_view ini_file_content = "# Test comment1\n[section1] Test\nkey1 = value1";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"},
{ItemType::SECTION, "section1", "", "", "[section1] Test\n"}};

CPPUNIT_ASSERT_THROW(
parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserTextAfterSectionError);
}


void IniparserTest::test_iniparser_illegal_continuation_line() {
constexpr std::string_view ini_file_content =
"# Test comment1\n[section1] # Test\n key1 = value1\nkey2 = value2";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"},
{ItemType::SECTION, "section1", "", "", "[section1] # Test\n"},
{ItemType::KEY_VAL, "section1", "key1", "value1", " key1 = value1\n"}};

CPPUNIT_ASSERT_THROW(
parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserIllegalContinuationLineError);
}


void IniparserTest::test_iniparser_missing_key() {
constexpr std::string_view ini_file_content = "# Test comment1\n[section1] # Test\n= value1\nkey2 = value2";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"},
{ItemType::SECTION, "section1", "", "", "[section1] # Test\n"},
{ItemType::KEY_VAL, "section1", "", "value1", "= value1\n"}};

CPPUNIT_ASSERT_THROW(parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingKeyError);
}


void IniparserTest::test_iniparser_missing_equal() {
constexpr std::string_view ini_file_content = "# Test comment1\n[section1] # Test\nkey1 \nkey2 = value2";

const std::vector<Item> expected_items = {
{ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"},
{ItemType::SECTION, "section1", "", "", "[section1] # Test\n"},
{ItemType::KEY_VAL, "section1", "key1", "", "key1 \n"}};

CPPUNIT_ASSERT_THROW(
parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingEqualError);
}
14 changes: 14 additions & 0 deletions test/libdnf5/iniparser/test_iniparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class IniparserTest : public CppUnit::TestCase {
#ifndef WITH_PERFORMANCE_TESTS
CPPUNIT_TEST(test_iniparser);
CPPUNIT_TEST(test_iniparser2);
CPPUNIT_TEST(test_iniparser_missing_section_header);
CPPUNIT_TEST(test_iniparser_missing_bracket);
CPPUNIT_TEST(test_iniparser_empty_section_name);
CPPUNIT_TEST(test_iniparser_text_after_section);
CPPUNIT_TEST(test_iniparser_illegal_continuation_line);
CPPUNIT_TEST(test_iniparser_missing_key);
CPPUNIT_TEST(test_iniparser_missing_equal);
#endif

CPPUNIT_TEST_SUITE_END();
Expand All @@ -40,6 +47,13 @@ class IniparserTest : public CppUnit::TestCase {

void test_iniparser();
void test_iniparser2();
void test_iniparser_missing_section_header();
void test_iniparser_missing_bracket();
void test_iniparser_empty_section_name();
void test_iniparser_text_after_section();
void test_iniparser_illegal_continuation_line();
void test_iniparser_missing_key();
void test_iniparser_missing_equal();
};

#endif

0 comments on commit 72cb511

Please sign in to comment.