workbook: Throw if index in (const) sheet_by_index is out of range

The documentation of (const) sheet_by_index already mentions that
invalid_parameter exception will be thrown if the index is out of
range, but the implementation was missing.
This commit is contained in:
Emmanuel Pescosta 2020-04-15 12:30:26 +02:00
parent 3c7122a78c
commit 3225f357dd
No known key found for this signature in database
GPG Key ID: 67C7A6C81D838246
2 changed files with 24 additions and 0 deletions

View File

@ -681,6 +681,11 @@ worksheet workbook::sheet_by_index(std::size_t index)
const worksheet workbook::sheet_by_index(std::size_t index) const
{
if (index >= d_->worksheets_.size())
{
throw invalid_parameter();
}
auto iter = d_->worksheets_.begin();
for (std::size_t i = 0; i < index; ++i, ++iter)

View File

@ -49,6 +49,8 @@ public:
register_test(test_add_sheet_at_index);
register_test(test_get_sheet_by_title);
register_test(test_get_sheet_by_title_const);
register_test(test_get_sheet_by_index);
register_test(test_get_sheet_by_index_const);
register_test(test_index_operator);
register_test(test_contains);
register_test(test_iter);
@ -151,6 +153,23 @@ public:
xlnt_assert_equals(new_sheet, found_sheet);
}
void test_get_sheet_by_index()
{
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
xlnt_assert_equals(new_sheet, wb.sheet_by_index(1)); // in range
xlnt_assert_throws(wb.sheet_by_index(2), xlnt::invalid_parameter); // out of range
}
void test_get_sheet_by_index_const()
{
xlnt::workbook wb;
auto new_sheet = wb.create_sheet();
const auto &wb_const = wb;
xlnt_assert_equals(new_sheet, wb_const.sheet_by_index(1)); // in range
xlnt_assert_throws(wb_const.sheet_by_index(2), xlnt::invalid_parameter); // out of range
}
void test_index_operator() // test_getitem
{
xlnt::workbook wb;