2016-08-06 22:40:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
#include <detail/vector_streambuf.hpp>
|
2016-08-06 22:40:17 +08:00
|
|
|
#include <helpers/path_helper.hpp>
|
|
|
|
#include <helpers/xml_helper.hpp>
|
|
|
|
#include <xlnt/workbook/workbook.hpp>
|
|
|
|
|
|
|
|
class test_round_trip : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
2016-09-08 10:39:13 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Write original to a memory as an XLSX-formatted ZIP, read it from memory back to a workbook,
|
|
|
|
/// then ensure that the resulting workbook matches the original.
|
|
|
|
/// </summary>
|
|
|
|
bool round_trip_matches_wrw(const xlnt::workbook &original)
|
2016-08-06 22:40:17 +08:00
|
|
|
{
|
2016-10-31 03:48:40 +08:00
|
|
|
std::vector<std::uint8_t> original_buffer;
|
|
|
|
original.save(original_buffer);
|
2016-12-23 07:57:22 +08:00
|
|
|
original.save("round_trip_in.xlsx");
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2016-09-08 10:39:13 +08:00
|
|
|
xlnt::workbook resulting_workbook;
|
2016-10-31 03:48:40 +08:00
|
|
|
resulting_workbook.load(original_buffer);
|
2016-09-08 10:39:13 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
std::vector<std::uint8_t> resulting_buffer;
|
|
|
|
resulting_workbook.save(resulting_buffer);
|
2016-12-23 07:57:22 +08:00
|
|
|
resulting_workbook.save("round_trip_out.xlsx");
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
return xml_helper::xlsx_archives_match(original_buffer, resulting_buffer);
|
2016-08-06 22:40:17 +08:00
|
|
|
}
|
2016-09-08 10:39:13 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Read file as an XLSX-formatted ZIP file in the filesystem to a workbook,
|
|
|
|
/// write the workbook back to memory, then ensure that the contents of the two files are equivalent.
|
|
|
|
/// </summary>
|
|
|
|
bool round_trip_matches_rw(const xlnt::path &original)
|
2016-08-06 22:40:17 +08:00
|
|
|
{
|
2016-10-31 03:48:40 +08:00
|
|
|
std::ifstream file_stream(original.string(), std::ios::binary);
|
|
|
|
std::vector<std::uint8_t> original_data;
|
|
|
|
|
|
|
|
{
|
|
|
|
xlnt::detail::vector_ostreambuf file_data_buffer(original_data);
|
|
|
|
std::ostream file_data_stream(&file_data_buffer);
|
|
|
|
file_data_stream << file_stream.rdbuf();
|
|
|
|
}
|
2016-09-08 10:39:13 +08:00
|
|
|
|
|
|
|
xlnt::workbook original_workbook;
|
|
|
|
original_workbook.load(original);
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> buffer;
|
|
|
|
original_workbook.save(buffer);
|
2016-12-23 07:57:22 +08:00
|
|
|
original_workbook.save("round_trip_out.xlsx");
|
2016-08-06 22:40:17 +08:00
|
|
|
|
2016-10-31 03:48:40 +08:00
|
|
|
return xml_helper::xlsx_archives_match(original_data, buffer);
|
2016-08-06 22:40:17 +08:00
|
|
|
}
|
|
|
|
|
2016-12-30 07:36:29 +08:00
|
|
|
void test_round_trip_empty_wrw()
|
2016-08-06 22:40:17 +08:00
|
|
|
{
|
2016-12-30 07:36:29 +08:00
|
|
|
xlnt::workbook wb = xlnt::workbook::empty();
|
2016-09-08 10:39:13 +08:00
|
|
|
TS_ASSERT(round_trip_matches_wrw(wb));
|
2016-08-06 22:40:17 +08:00
|
|
|
}
|
|
|
|
|
2016-09-08 10:39:13 +08:00
|
|
|
void test_round_trip_empty_excel_rw()
|
2016-08-06 22:40:17 +08:00
|
|
|
{
|
2017-01-21 23:12:08 +08:00
|
|
|
auto path = path_helper::data_directory("9_default-excel.xlsx");
|
2016-08-06 22:40:17 +08:00
|
|
|
TS_ASSERT(round_trip_matches_rw(path));
|
|
|
|
}
|
|
|
|
|
2016-09-12 02:57:34 +08:00
|
|
|
void test_round_trip_all_styles_rw()
|
|
|
|
{
|
2017-01-21 23:12:08 +08:00
|
|
|
auto path = path_helper::data_directory("10_all_styles.xlsx");
|
2016-11-25 21:13:55 +08:00
|
|
|
TS_ASSERT(round_trip_matches_rw(path));
|
2016-09-12 02:57:34 +08:00
|
|
|
}
|
2016-12-23 07:57:22 +08:00
|
|
|
|
|
|
|
void test_round_trip_headers_footers()
|
|
|
|
{
|
2017-01-21 23:12:08 +08:00
|
|
|
auto path = path_helper::data_directory("18_headers_and_footers.xlsx");
|
2016-12-23 07:57:22 +08:00
|
|
|
TS_ASSERT(round_trip_matches_rw(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_round_trip_row_and_col_props()
|
|
|
|
{
|
2017-01-21 23:12:08 +08:00
|
|
|
auto path = path_helper::data_directory("19_row_and_col_properties.xlsx");
|
2016-12-23 07:57:22 +08:00
|
|
|
TS_ASSERT(round_trip_matches_rw(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_round_trip_page_breaks()
|
|
|
|
{
|
2017-01-21 23:12:08 +08:00
|
|
|
auto path = path_helper::data_directory("20_page_breaks.xlsx");
|
2016-12-23 07:57:22 +08:00
|
|
|
TS_ASSERT(round_trip_matches_rw(path));
|
|
|
|
}
|
2016-08-06 22:40:17 +08:00
|
|
|
};
|