mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
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:
parent
3c7122a78c
commit
3225f357dd
|
@ -681,6 +681,11 @@ worksheet workbook::sheet_by_index(std::size_t index)
|
||||||
|
|
||||||
const worksheet workbook::sheet_by_index(std::size_t index) const
|
const worksheet workbook::sheet_by_index(std::size_t index) const
|
||||||
{
|
{
|
||||||
|
if (index >= d_->worksheets_.size())
|
||||||
|
{
|
||||||
|
throw invalid_parameter();
|
||||||
|
}
|
||||||
|
|
||||||
auto iter = d_->worksheets_.begin();
|
auto iter = d_->worksheets_.begin();
|
||||||
|
|
||||||
for (std::size_t i = 0; i < index; ++i, ++iter)
|
for (std::size_t i = 0; i < index; ++i, ++iter)
|
||||||
|
|
|
@ -49,6 +49,8 @@ public:
|
||||||
register_test(test_add_sheet_at_index);
|
register_test(test_add_sheet_at_index);
|
||||||
register_test(test_get_sheet_by_title);
|
register_test(test_get_sheet_by_title);
|
||||||
register_test(test_get_sheet_by_title_const);
|
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_index_operator);
|
||||||
register_test(test_contains);
|
register_test(test_contains);
|
||||||
register_test(test_iter);
|
register_test(test_iter);
|
||||||
|
@ -151,6 +153,23 @@ public:
|
||||||
xlnt_assert_equals(new_sheet, found_sheet);
|
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
|
void test_index_operator() // test_getitem
|
||||||
{
|
{
|
||||||
xlnt::workbook wb;
|
xlnt::workbook wb;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user