xlnt/source/workbook/tests/test_produce_xlsx.hpp

189 lines
5.6 KiB
C++
Raw Normal View History

#pragma once
#include <iostream>
#include <cxxtest/TestSuite.h>
#include <detail/vector_streambuf.hpp>
#include <helpers/temporary_file.hpp>
#include <helpers/path_helper.hpp>
#include <helpers/xml_helper.hpp>
#include <xlnt/workbook/workbook.hpp>
class test_produce_xlsx : public CxxTest::TestSuite
{
public:
bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
{
std::vector<std::uint8_t> wb_data;
wb.save(wb_data);
std::ifstream file_stream(file.string(), std::ios::binary);
std::vector<std::uint8_t> file_data;
{
xlnt::detail::vector_ostreambuf file_data_buffer(file_data);
std::ostream file_data_stream(&file_data_buffer);
file_data_stream << file_stream.rdbuf();
}
return xml_helper::xlsx_archives_match(wb_data, file_data);
}
void test_produce_minimal()
{
xlnt::workbook wb = xlnt::workbook::minimal();
TS_ASSERT(workbook_matches_file(wb, path_helper::get_data_directory("8_minimal.xlsx")));
}
void test_produce_default_excel()
{
xlnt::workbook wb = xlnt::workbook::empty_excel();
TS_ASSERT(workbook_matches_file(wb, path_helper::get_data_directory("9_default-excel.xlsx")));
}
void test_produce_default_libre_office()
{
xlnt::workbook wb = xlnt::workbook::empty_libre_office();
TS_ASSERT(workbook_matches_file(wb, path_helper::get_data_directory("10_default-libre-office.xlsx")));
}
2016-08-18 19:34:18 +08:00
void test_produce_simple_excel()
{
xlnt::workbook wb = xlnt::workbook::empty_excel();
auto ws = wb.active_sheet();
2016-08-18 19:34:18 +08:00
auto bold_font = xlnt::font().bold(true);
ws.cell("A1").value("Type");
ws.cell("A1").font(bold_font);
2016-08-18 19:34:18 +08:00
ws.cell("B1").value("Value");
ws.cell("B1").font(bold_font);
2016-08-18 19:34:18 +08:00
ws.cell("A2").value("null");
ws.cell("B2").value(nullptr);
2016-08-18 19:34:18 +08:00
ws.cell("A3").value("bool (true)");
ws.cell("B3").value(true);
2016-08-18 19:34:18 +08:00
ws.cell("A4").value("bool (false)");
ws.cell("B4").value(false);
2016-08-18 19:34:18 +08:00
ws.cell("A5").value("number (std::int8_t)");
ws.cell("B5").value(std::numeric_limits<std::int8_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A6").value("number (std::uint8_t)");
ws.cell("B6").value(std::numeric_limits<std::uint8_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A7").value("number (std::uint16_t)");
ws.cell("B7").value(std::numeric_limits<std::int16_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A8").value("number (std::uint16_t)");
ws.cell("B8").value(std::numeric_limits<std::uint16_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A9").value("number (std::uint32_t)");
ws.cell("B9").value(std::numeric_limits<std::int32_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A10").value("number (std::uint32_t)");
ws.cell("B10").value(std::numeric_limits<std::uint32_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A11").value("number (std::uint64_t)");
ws.cell("B11").value(std::numeric_limits<std::int64_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A12").value("number (std::uint64_t)");
ws.cell("B12").value(std::numeric_limits<std::uint64_t>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A13").value("number (float)");
ws.cell("B13").value(std::numeric_limits<float>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A14").value("number (double)");
ws.cell("B14").value(std::numeric_limits<double>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A15").value("number (long double)");
ws.cell("B15").value(std::numeric_limits<long double>::max());
2016-08-18 19:34:18 +08:00
ws.cell("A16").value("text (char *)");
ws.cell("B16").value("string");
2016-08-18 19:34:18 +08:00
ws.cell("A17").value("text (std::string)");
ws.cell("B17").value(std::string("string"));
2016-08-18 19:34:18 +08:00
ws.cell("A18").value("date");
ws.cell("B18").value(xlnt::date(2016, 2, 3));
2016-08-18 19:34:18 +08:00
ws.cell("A19").value("time");
ws.cell("B19").value(xlnt::time(1, 2, 3, 4));
2016-08-18 19:34:18 +08:00
ws.cell("A20").value("datetime");
ws.cell("B20").value(xlnt::datetime(2016, 2, 3, 1, 2, 3, 4));
2016-08-18 19:34:18 +08:00
ws.cell("A21").value("timedelta");
ws.cell("B21").value(xlnt::timedelta(1, 2, 3, 4, 5));
2016-08-18 19:34:18 +08:00
2016-09-08 06:48:08 +08:00
ws.freeze_panes("B2");
2016-08-18 19:34:18 +08:00
std::vector<std::uint8_t> temp_buffer;
wb.save(temp_buffer);
TS_ASSERT(!temp_buffer.empty());
2016-08-18 19:34:18 +08:00
}
2016-10-23 01:53:44 +08:00
void test_save_after_sheet_deletion()
{
xlnt::workbook workbook;
TS_ASSERT_EQUALS(workbook.sheet_titles().size(), 1);
2016-10-23 01:53:44 +08:00
auto sheet = workbook.create_sheet();
sheet.title("XXX1");
TS_ASSERT_EQUALS(workbook.sheet_titles().size(), 2);
2016-10-23 01:53:44 +08:00
workbook.remove_sheet(workbook.sheet_by_title("XXX1"));
TS_ASSERT_EQUALS(workbook.sheet_titles().size(), 1);
2016-10-23 01:53:44 +08:00
std::vector<std::uint8_t> temp_buffer;
TS_ASSERT_THROWS_NOTHING(workbook.save(temp_buffer));
TS_ASSERT(!temp_buffer.empty());
2016-10-23 01:53:44 +08:00
}
void test_write_comments()
{
xlnt::workbook wb;
xlnt::formatted_text comment_text;
xlnt::text_run formatted_run;;
formatted_run.bold(true);
formatted_run.size(10);
formatted_run.color(xlnt::indexed_color(81));
formatted_run.font("Calibri");
auto sheet1 = wb.active_sheet();
sheet1.cell("A1").value("Sheet1!A1");
formatted_run.string("Sheet1 comment");
2016-11-28 02:12:38 +08:00
comment_text.add_run(formatted_run);
sheet1.cell("A1").comment(xlnt::comment(comment_text, "Microsoft Office User"));
sheet1.cell("A1").comment("Sheet1 comment");
2016-11-28 02:12:38 +08:00
sheet1.cell("A2").value("Sheet1!A2");
formatted_run.string("Sheet1 comment2");
comment_text.clear();
comment_text.add_run(formatted_run);
sheet1.cell("A2").comment(xlnt::comment(comment_text, "Microsoft Office User"));
2016-11-28 02:12:38 +08:00
auto sheet2 = wb.create_sheet();
sheet2.cell("A1").value("Sheet2!A1");
formatted_run.string("Sheet2 comment");
comment_text.clear();
comment_text.add_run(formatted_run);
sheet2.cell("A1").comment(xlnt::comment(comment_text, "Microsoft Office User"));
sheet2.cell("A2").value("Sheet2!A2");
formatted_run.string("Sheet2 comment2");
comment_text.clear();
comment_text.add_run(formatted_run);
sheet2.cell("A2").comment(xlnt::comment(comment_text, "Microsoft Office User"));
2016-11-28 00:37:12 +08:00
// TS_ASSERT(workbook_matches_file(wb, xlnt::path("data/18_basic_comments.xlsx")));
}
};