2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
#include <detail/relationship_serializer.hpp>
|
|
|
|
#include <detail/shared_strings_serializer.hpp>
|
|
|
|
#include <detail/workbook_serializer.hpp>
|
|
|
|
#include <detail/worksheet_serializer.hpp>
|
|
|
|
#include <helpers/temporary_file.hpp>
|
|
|
|
#include <helpers/path_helper.hpp>
|
2016-07-21 07:04:44 +08:00
|
|
|
#include <helpers/xml_helper.hpp>
|
2015-10-31 06:54:04 +08:00
|
|
|
#include <xlnt/workbook/workbook.hpp>
|
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
class test_write : public CxxTest::TestSuite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void test_write_empty_workbook()
|
|
|
|
{
|
2014-07-30 05:39:00 +08:00
|
|
|
xlnt::workbook wbk;
|
2015-10-02 06:14:42 +08:00
|
|
|
wbk.get_active_sheet().get_cell("A2").set_value("xlnt");
|
2014-07-30 05:39:00 +08:00
|
|
|
wbk.get_active_sheet().get_cell("B5").set_value(88);
|
2015-10-24 02:42:36 +08:00
|
|
|
wbk.get_active_sheet().get_cell("B5").set_number_format(xlnt::number_format::percentage_00());
|
2016-07-21 07:54:24 +08:00
|
|
|
wbk.save(temp_file.get_filename());
|
2014-07-30 05:39:00 +08:00
|
|
|
|
2016-07-21 07:54:24 +08:00
|
|
|
if(path_helper::file_exists(temp_file.get_filename()))
|
2014-06-11 05:12:15 +08:00
|
|
|
{
|
2016-07-21 07:54:24 +08:00
|
|
|
path_helper::delete_file(temp_file.get_filename());
|
2014-06-11 05:12:15 +08:00
|
|
|
}
|
2014-06-05 06:42:17 +08:00
|
|
|
|
2016-07-21 07:54:24 +08:00
|
|
|
TS_ASSERT(!path_helper::file_exists(temp_file.get_filename()));
|
|
|
|
wb_.save(temp_file.get_filename());
|
|
|
|
TS_ASSERT(path_helper::file_exists(temp_file.get_filename()));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_virtual_workbook()
|
|
|
|
{
|
2014-06-05 06:42:17 +08:00
|
|
|
xlnt::workbook old_wb;
|
2014-06-11 05:12:15 +08:00
|
|
|
std::vector<unsigned char> saved_wb;
|
|
|
|
TS_ASSERT(old_wb.save(saved_wb));
|
|
|
|
xlnt::workbook new_wb;
|
|
|
|
TS_ASSERT(new_wb.load(saved_wb));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_workbook_rels()
|
|
|
|
{
|
2014-06-05 06:42:17 +08:00
|
|
|
xlnt::workbook wb;
|
2016-05-16 03:03:02 +08:00
|
|
|
wb.add_shared_string(xlnt::text());
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::zip_file archive;
|
2015-11-04 07:26:33 +08:00
|
|
|
xlnt::relationship_serializer serializer(archive);
|
|
|
|
serializer.write_relationships(wb.get_relationships(), "xl/workbook.xml");
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
xml.load(archive.read("xl/_rels/workbook.xml.rels").c_str());
|
2014-06-05 06:42:17 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/workbook.xml.rels", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_workbook()
|
|
|
|
{
|
2014-06-05 06:42:17 +08:00
|
|
|
xlnt::workbook wb;
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook_serializer serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_workbook(xml);
|
2014-06-05 06:42:17 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/workbook.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_string_table()
|
|
|
|
{
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.get_active_sheet();
|
|
|
|
|
|
|
|
ws.get_cell("A1").set_value("hello");
|
|
|
|
ws.get_cell("A2").set_value("world");
|
|
|
|
ws.get_cell("A3").set_value("nice");
|
|
|
|
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
xlnt::shared_strings_serializer::write_shared_strings(wb.get_shared_strings(), xml);
|
2016-05-15 03:19:08 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sharedStrings.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_worksheet()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F42").set_value("hello");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hidden_worksheet()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2015-11-20 11:54:54 +08:00
|
|
|
ws.get_page_setup().set_sheet_state(xlnt::sheet_state::hidden);
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F42").set_value("hello");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_bool()
|
|
|
|
{
|
2014-07-26 04:39:25 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
|
|
|
ws.get_cell("F42").set_value(false);
|
|
|
|
ws.get_cell("F43").set_value(true);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_bool.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_formula()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F1").set_value(10);
|
|
|
|
ws.get_cell("F2").set_value(32);
|
2014-07-25 05:31:46 +08:00
|
|
|
ws.get_cell("F3").set_formula("F1+F2");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_formula.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_height()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F1").set_value(10);
|
2015-10-02 06:14:42 +08:00
|
|
|
ws.get_row_properties(ws.get_cell("F1").get_row()).height = 30;
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_height.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hyperlink()
|
|
|
|
{
|
2015-11-02 12:52:19 +08:00
|
|
|
xlnt::workbook clean_wb;
|
|
|
|
auto ws = clean_wb.get_active_sheet();
|
|
|
|
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("A1").set_value("test");
|
2014-06-11 05:12:15 +08:00
|
|
|
ws.get_cell("A1").set_hyperlink("http://test.com");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_hyperlink.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hyperlink_rels()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
TS_ASSERT_EQUALS(0, ws.get_relationships().size());
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("A1").set_value("test");
|
2014-06-11 05:12:15 +08:00
|
|
|
ws.get_cell("A1").set_hyperlink("http://test.com/");
|
2014-06-05 06:42:17 +08:00
|
|
|
TS_ASSERT_EQUALS(1, ws.get_relationships().size());
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("A2").set_value("test");
|
2014-06-11 05:12:15 +08:00
|
|
|
ws.get_cell("A2").set_hyperlink("http://test2.com/");
|
2014-06-05 06:42:17 +08:00
|
|
|
TS_ASSERT_EQUALS(2, ws.get_relationships().size());
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::zip_file archive;
|
2015-11-04 07:26:33 +08:00
|
|
|
xlnt::relationship_serializer serializer(archive);
|
|
|
|
serializer.write_relationships(ws.get_relationships(), "xl/worksheets/sheet1.xml");
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
xml.load(archive.read("xl/worksheets/_rels/sheet1.xml.rels").c_str());
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_hyperlink.xml.rels", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-27 04:19:15 +08:00
|
|
|
|
2014-08-02 04:46:54 +08:00
|
|
|
void _test_write_hyperlink_image_rels()
|
2014-07-27 04:19:15 +08:00
|
|
|
{
|
|
|
|
TS_SKIP("not implemented");
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
|
|
|
|
void test_hyperlink_value()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-05-31 06:42:25 +08:00
|
|
|
ws.get_cell("A1").set_hyperlink("http://test.com");
|
2015-11-11 07:58:54 +08:00
|
|
|
TS_ASSERT_EQUALS("http://test.com", ws.get_cell("A1").get_value<std::string>());
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("A1").set_value("test");
|
2015-11-11 07:58:54 +08:00
|
|
|
TS_ASSERT_EQUALS("test", ws.get_cell("A1").get_value<std::string>());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_auto_filter()
|
|
|
|
{
|
2014-06-11 05:12:15 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.get_sheet_by_index(0);
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F42").set_value("hello");
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.auto_filter("A1:F1");
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
xlnt::worksheet_serializer ws_serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
ws_serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_auto_filter.xml", xml));
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::workbook_serializer wb_serializer(wb);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml2;
|
|
|
|
wb_serializer.write_workbook(xml2);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/workbook_auto_filter.xml", xml2));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-27 04:19:15 +08:00
|
|
|
|
|
|
|
void test_write_auto_filter_filter_column()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_auto_filter_sort_condition()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
|
|
|
|
void test_freeze_panes_horiz()
|
|
|
|
{
|
2014-07-26 04:39:25 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
|
|
|
ws.get_cell("F42").set_value("hello");
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.freeze_panes("A4");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_freeze_panes_horiz.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_freeze_panes_vert()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F42").set_value("hello");
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.freeze_panes("D1");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_freeze_panes_vert.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_freeze_panes_both()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("F42").set_value("hello");
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.freeze_panes("D4");
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/sheet1_freeze_panes_both.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_long_number()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-27 04:19:15 +08:00
|
|
|
ws.get_cell("A1").set_value(9781231231230LL);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/long_number.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_short_number()
|
|
|
|
{
|
2014-06-16 00:16:34 +08:00
|
|
|
auto ws = wb_.create_sheet();
|
2014-07-26 04:39:25 +08:00
|
|
|
ws.get_cell("A1").set_value(1234567890);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
|
|
|
xlnt::worksheet_serializer serializer(ws);
|
2016-07-04 07:22:08 +08:00
|
|
|
pugi::xml_document xml;
|
|
|
|
serializer.write_worksheet(xml);
|
2015-10-31 06:54:04 +08:00
|
|
|
|
2016-07-21 07:04:44 +08:00
|
|
|
TS_ASSERT(xml_helper::compare_xml(path_helper::get_data_directory() + "/writer/expected/short_number.xml", xml));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-27 04:19:15 +08:00
|
|
|
|
2014-08-02 04:46:54 +08:00
|
|
|
void _test_write_images()
|
2014-07-27 04:19:15 +08:00
|
|
|
{
|
|
|
|
TS_SKIP("not implemented");
|
|
|
|
}
|
2014-06-05 06:42:17 +08:00
|
|
|
|
|
|
|
private:
|
2016-07-21 07:54:24 +08:00
|
|
|
temporary_file temp_file;
|
2014-06-16 00:16:34 +08:00
|
|
|
xlnt::workbook wb_;
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|