2017-04-14 07:01:30 +08:00
|
|
|
// Copyright (c) 2014-2017 Thomas Fussell
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE
|
|
|
|
//
|
|
|
|
// @license: http://www.opensource.org/licenses/mit-license.php
|
|
|
|
// @author: see AUTHORS file
|
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-05-22 07:17:56 +08:00
|
|
|
#include <algorithm>
|
2014-05-09 03:32:12 +08:00
|
|
|
#include <iostream>
|
2017-04-14 02:51:35 +08:00
|
|
|
#include <helpers/test_suite.hpp>
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
#include <xlnt/xlnt.hpp>
|
2014-06-19 03:12:44 +08:00
|
|
|
#include "helpers/temporary_file.hpp"
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2017-04-14 07:01:30 +08:00
|
|
|
class workbook_test_suite : public test_suite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
2017-04-14 07:01:30 +08:00
|
|
|
workbook_test_suite()
|
|
|
|
{
|
|
|
|
register_test(test_active_sheet);
|
|
|
|
}
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
void test_active_sheet()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(wb.active_sheet(), wb[0]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_sheet()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2016-03-06 10:39:50 +08:00
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
auto last = std::distance(wb.begin(), wb.end()) - 1;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(new_sheet, wb[last]);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-10-30 04:31:30 +08:00
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
void test_add_correct_sheet()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2016-12-02 21:37:50 +08:00
|
|
|
new_sheet.cell("A6").value(1.498);
|
2016-07-22 10:14:00 +08:00
|
|
|
wb.copy_sheet(new_sheet);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb[1].compare(wb[2], false));
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.create_sheet().cell("A6").value(1.497);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(!wb[1].compare(wb[3], false));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
void test_add_sheet_from_other_workbook()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb1, wb2;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto new_sheet = wb1.active_sheet();
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_throws(wb2.copy_sheet(new_sheet), xlnt::invalid_parameter);
|
|
|
|
assert_throws(wb2.index(new_sheet), std::runtime_error);
|
2016-07-18 02:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_add_sheet_at_index()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
ws.cell("B3").value(2);
|
|
|
|
ws.title("Active");
|
2016-07-22 10:14:00 +08:00
|
|
|
wb.copy_sheet(ws, 0);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(wb.sheet_titles().at(0), "Sheet1");
|
|
|
|
assert_equals(wb.sheet_by_index(0).cell("B3").value<int>(), 2);
|
|
|
|
assert_equals(wb.sheet_titles().at(1), "Active");
|
|
|
|
assert_equals(wb.sheet_by_index(1).cell("B3").value<int>(), 2);
|
2016-03-06 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
void test_remove_sheet()
|
|
|
|
{
|
2016-07-18 02:59:11 +08:00
|
|
|
xlnt::workbook wb, wb2;
|
2014-05-19 09:29:19 +08:00
|
|
|
auto new_sheet = wb.create_sheet(0);
|
2016-12-02 21:37:50 +08:00
|
|
|
new_sheet.title("removed");
|
2014-05-13 01:42:28 +08:00
|
|
|
wb.remove_sheet(new_sheet);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(!wb.contains("removed"));
|
|
|
|
assert_throws(wb.remove_sheet(wb2.active_sheet()), std::runtime_error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
void test_get_sheet_by_title()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string title = "my sheet";
|
2016-12-02 21:37:50 +08:00
|
|
|
new_sheet.title(title);
|
|
|
|
auto found_sheet = wb.sheet_by_title(title);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(new_sheet, found_sheet);
|
|
|
|
assert_throws(wb.sheet_by_title("error"), xlnt::key_not_found);
|
2016-07-24 08:41:55 +08:00
|
|
|
const auto &wb_const = wb;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_throws(wb_const.sheet_by_title("error"), xlnt::key_not_found);
|
2014-05-19 09:29:19 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 13:52:05 +08:00
|
|
|
void test_get_sheet_by_title_const()
|
2016-05-26 10:44:35 +08:00
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
|
|
|
std::string title = "my sheet";
|
2016-12-02 21:37:50 +08:00
|
|
|
new_sheet.title(title);
|
2016-05-26 10:44:35 +08:00
|
|
|
const xlnt::workbook& wbconst = wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto found_sheet = wbconst.sheet_by_title(title);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(new_sheet, found_sheet);
|
2016-05-26 10:44:35 +08:00
|
|
|
}
|
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
void test_index_operator() // test_getitem
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_throws_nothing(wb["Sheet1"]);
|
|
|
|
assert_throws(wb["NotThere"], xlnt::key_not_found);
|
2016-03-06 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// void test_delitem() {} doesn't make sense in c++
|
|
|
|
|
|
|
|
void test_contains()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb.contains("Sheet1"));
|
|
|
|
assert(!wb.contains("NotThere"));
|
2016-03-06 10:39:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_iter()
|
2014-05-19 09:29:19 +08:00
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
|
2016-03-06 10:39:50 +08:00
|
|
|
for(auto ws : wb)
|
|
|
|
{
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(ws.title(), "Sheet1");
|
2016-03-06 10:39:50 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
void test_get_index()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.create_sheet().title("1");
|
|
|
|
wb.create_sheet().title("2");
|
2016-07-18 02:59:11 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
auto sheet_index = wb.index(wb.sheet_by_title("1"));
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(sheet_index, 1);
|
2016-07-18 02:59:11 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
sheet_index = wb.index(wb.sheet_by_title("2"));
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(sheet_index, 2);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_get_sheet_names()
|
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.create_sheet().title("test_get_sheet_titles");
|
2016-08-02 06:33:43 +08:00
|
|
|
|
2016-08-13 11:06:25 +08:00
|
|
|
const std::vector<std::string> expected_titles = { "Sheet1", "test_get_sheet_titles" };
|
2016-08-02 06:33:43 +08:00
|
|
|
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(wb.sheet_titles(), expected_titles);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
2014-05-09 03:32:12 +08:00
|
|
|
void test_add_named_range()
|
|
|
|
{
|
2016-07-18 02:59:11 +08:00
|
|
|
xlnt::workbook wb, wb2;
|
2014-05-19 09:29:19 +08:00
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(new_sheet.has_named_range("test_nr"));
|
|
|
|
assert(wb.has_named_range("test_nr"));
|
|
|
|
assert_throws(wb2.create_named_range("test_nr", new_sheet, "A1"), std::runtime_error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
|
|
|
void test_get_named_range()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
2016-12-02 21:37:50 +08:00
|
|
|
auto found_range = wb.named_range("test_nr");
|
|
|
|
auto expected_range = new_sheet.range("A1");
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(expected_range, found_range);
|
|
|
|
assert_throws(wb.named_range("test_nr2"), std::runtime_error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2014-05-19 09:29:19 +08:00
|
|
|
void test_remove_named_range()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-19 09:29:19 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto new_sheet = wb.create_sheet();
|
2014-05-30 08:52:14 +08:00
|
|
|
wb.create_named_range("test_nr", new_sheet, "A1");
|
|
|
|
wb.remove_named_range("test_nr");
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(!new_sheet.has_named_range("test_nr"));
|
|
|
|
assert(!wb.has_named_range("test_nr"));
|
|
|
|
assert_throws(wb.remove_named_range("test_nr2"), std::runtime_error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
2016-07-09 22:02:58 +08:00
|
|
|
void test_post_increment_iterator()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.create_sheet().title("Sheet2");
|
|
|
|
wb.create_sheet().title("Sheet3");
|
2016-07-09 22:02:58 +08:00
|
|
|
|
|
|
|
auto iter = wb.begin();
|
|
|
|
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*(iter++)).title(), "Sheet1");
|
|
|
|
assert_equals((*(iter++)).title(), "Sheet2");
|
|
|
|
assert_equals((*(iter++)).title(), "Sheet3");
|
|
|
|
assert_equals(iter, wb.end());
|
2016-07-09 22:20:08 +08:00
|
|
|
|
|
|
|
const auto wb_const = wb;
|
|
|
|
|
|
|
|
auto const_iter = wb_const.begin();
|
|
|
|
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*(const_iter++)).title(), "Sheet1");
|
|
|
|
assert_equals((*(const_iter++)).title(), "Sheet2");
|
|
|
|
assert_equals((*(const_iter++)).title(), "Sheet3");
|
|
|
|
assert_equals(const_iter, wb_const.end());
|
2016-07-09 22:02:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_copy_iterator()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.create_sheet().title("Sheet2");
|
|
|
|
wb.create_sheet().title("Sheet3");
|
2016-07-09 22:02:58 +08:00
|
|
|
|
|
|
|
auto iter = wb.begin();
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*iter).title(), "Sheet1");
|
2016-07-09 22:02:58 +08:00
|
|
|
|
|
|
|
iter++;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*iter).title(), "Sheet2");
|
2016-07-09 22:02:58 +08:00
|
|
|
|
|
|
|
auto copy = wb.begin();
|
|
|
|
copy = iter;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*iter).title(), "Sheet2");
|
|
|
|
assert_equals(iter, copy);
|
2016-07-09 22:02:58 +08:00
|
|
|
|
|
|
|
iter++;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*iter).title(), "Sheet3");
|
|
|
|
assert_differs(iter, copy);
|
2016-07-09 22:02:58 +08:00
|
|
|
|
|
|
|
copy++;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals((*iter).title(), "Sheet3");
|
|
|
|
assert_equals(iter, copy);
|
2016-07-09 22:02:58 +08:00
|
|
|
}
|
2016-03-06 10:39:50 +08:00
|
|
|
|
2016-07-17 03:57:50 +08:00
|
|
|
void test_manifest()
|
|
|
|
{
|
2016-08-05 13:52:05 +08:00
|
|
|
xlnt::manifest m;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(!m.has_default_type("xml"));
|
|
|
|
assert_throws(m.default_type("xml"), xlnt::key_not_found);
|
|
|
|
assert(!m.has_relationship(xlnt::path("/"), xlnt::relationship_type::office_document));
|
|
|
|
assert(m.relationships(xlnt::path("xl/workbook.xml")).empty());
|
2016-07-18 02:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_memory()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb, wb2;
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.active_sheet().title("swap");
|
2016-07-18 02:59:11 +08:00
|
|
|
std::swap(wb, wb2);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(wb.active_sheet().title(), "Sheet1");
|
|
|
|
assert_equals(wb2.active_sheet().title(), "swap");
|
2016-07-18 02:59:11 +08:00
|
|
|
wb = wb2;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert_equals(wb.active_sheet().title(), "swap");
|
2016-07-18 02:59:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_clear()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::style s = wb.create_style("s");
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.active_sheet().cell("B2").value("B2");
|
|
|
|
wb.active_sheet().cell("B2").style(s);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb.active_sheet().cell("B2").has_style());
|
2016-07-18 02:59:11 +08:00
|
|
|
wb.clear_styles();
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(!wb.active_sheet().cell("B2").has_style());
|
2016-08-18 19:34:18 +08:00
|
|
|
xlnt::format format = wb.create_format();
|
2016-07-18 02:59:11 +08:00
|
|
|
xlnt::font font;
|
2016-08-18 19:34:18 +08:00
|
|
|
font.size(41);
|
|
|
|
format.font(font, true);
|
2016-12-02 21:37:50 +08:00
|
|
|
wb.active_sheet().cell("B2").format(format);
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb.active_sheet().cell("B2").has_format());
|
2016-07-18 02:59:11 +08:00
|
|
|
wb.clear_formats();
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(!wb.active_sheet().cell("B2").has_format());
|
2016-07-18 02:59:11 +08:00
|
|
|
wb.clear();
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb.sheet_titles().empty());
|
2016-07-18 02:59:11 +08:00
|
|
|
}
|
2016-07-24 07:43:24 +08:00
|
|
|
|
|
|
|
void test_comparison()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb, wb2;
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb == wb);
|
|
|
|
assert(!(wb != wb));
|
|
|
|
assert(!(wb == wb2));
|
|
|
|
assert(wb != wb2);
|
2016-07-24 07:43:24 +08:00
|
|
|
|
|
|
|
const auto &wb_const = wb;
|
|
|
|
//TODO these aren't tests...
|
2016-12-02 21:37:50 +08:00
|
|
|
wb_const.manifest();
|
2016-07-24 07:43:24 +08:00
|
|
|
|
2017-04-14 02:51:35 +08:00
|
|
|
assert(wb.has_theme());
|
2016-07-24 07:43:24 +08:00
|
|
|
|
|
|
|
wb.create_style("style1");
|
2016-11-08 10:11:30 +08:00
|
|
|
wb.style("style1");
|
|
|
|
wb_const.style("style1");
|
2016-07-24 07:43:24 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|