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 26, 2023
1 parent d7d3fa8 commit fcc3657
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
91 changes: 91 additions & 0 deletions test/libdnf5/iniparser/test_iniparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,94 @@ key1 = value1)**";

parse_and_check_results(ini_file_content, expected_items);
}


void IniparserTest::test_iniparser_missing_section_header() {
const std::string 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() {
const std::string ini_file_content =
R"**(
# Test comment1
[section1
key1 = value1
)**";

const std::vector<Item> expected_items = {
{ItemType::EMPTY_LINE, "", "", "", "\n"},
{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() {
const std::string 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() {
const std::string 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() {
const std::string 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() {
const std::string 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() {
const std::string 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 @@ -32,6 +32,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 @@ -42,6 +49,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 fcc3657

Please sign in to comment.