2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
#include <xlnt/xlnt.hpp>
|
|
|
|
#include "helpers/temporary_file.hpp"
|
|
|
|
#include "helpers/path_helper.hpp"
|
|
|
|
#include "helpers/helper.hpp"
|
2014-05-09 03:32:12 +08:00
|
|
|
|
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-06-05 06:42:17 +08:00
|
|
|
if(PathHelper::FileExists(temp_file.GetFilename()))
|
|
|
|
{
|
|
|
|
PathHelper::DeleteFile(temp_file.GetFilename());
|
|
|
|
}
|
|
|
|
|
|
|
|
TS_ASSERT(!PathHelper::FileExists(temp_file.GetFilename()));
|
|
|
|
wb.save(temp_file.GetFilename());
|
|
|
|
TS_ASSERT(PathHelper::FileExists(temp_file.GetFilename()));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_virtual_workbook()
|
|
|
|
{
|
2014-06-06 05:42:15 +08:00
|
|
|
TS_SKIP("");
|
2014-06-05 06:42:17 +08:00
|
|
|
xlnt::workbook old_wb;
|
|
|
|
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;
|
|
|
|
auto content = xlnt::writer::write_workbook_rels(wb);
|
|
|
|
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/workbook.xml.rels", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_workbook()
|
|
|
|
{
|
2014-06-05 06:42:17 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto content = xlnt::writer::write_workbook(wb);
|
|
|
|
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/workbook.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void test_write_string_table()
|
|
|
|
{
|
2014-06-05 06:42:17 +08:00
|
|
|
std::unordered_map<std::string, int> table = {{"hello", 1}, {"world", 2}, {"nice", 3}};
|
|
|
|
auto content = xlnt::writer::write_string_table(table);
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sharedStrings.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_worksheet()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F42") = "hello";
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"hello", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hidden_worksheet()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_page_setup().set_sheet_state(xlnt::page_setup::sheet_state::hidden);
|
|
|
|
ws.get_cell("F42") = "hello";
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"hello", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_bool()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F42") = false;
|
|
|
|
ws.get_cell("F43") = true;
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_bool.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_formula()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F1") = 10;
|
|
|
|
ws.get_cell("F2") = 32;
|
|
|
|
ws.get_cell("F3") = "=F1+F2";
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_formula.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_style()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F1") = "13%";
|
|
|
|
auto style_id_by_hash = xlnt::style_writer(wb).get_style_by_hash();
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {}, style_id_by_hash);
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_style.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_height()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F1") = 10;
|
|
|
|
ws.get_row_properties(ws.get_cell("F1").get_row()).set_height(30);
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_height.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hyperlink()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("A1") = "test";
|
|
|
|
ws.get_cell("A1").set_hyperlink("http:test.com");
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"test", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_hyperlink.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_hyperlink_rels()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
TS_ASSERT_EQUALS(0, ws.get_relationships().size());
|
|
|
|
ws.get_cell("A1") = "test";
|
|
|
|
ws.get_cell("A1").set_hyperlink("http:test.com/");
|
|
|
|
TS_ASSERT_EQUALS(1, ws.get_relationships().size());
|
|
|
|
ws.get_cell("A2") = "test";
|
|
|
|
ws.get_cell("A2").set_hyperlink("http:test2.com/");
|
|
|
|
TS_ASSERT_EQUALS(2, ws.get_relationships().size());
|
|
|
|
auto content = xlnt::writer::write_worksheet_rels(ws, 1);
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_hyperlink.xml.rels", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_hyperlink_value()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-05-31 06:42:25 +08:00
|
|
|
ws.get_cell("A1").set_hyperlink("http://test.com");
|
|
|
|
TS_ASSERT_EQUALS("http://test.com", ws.get_cell("A1"));
|
2014-05-21 22:20:30 +08:00
|
|
|
ws.get_cell("A1") = "test";
|
|
|
|
TS_ASSERT_EQUALS("test", ws.get_cell("A1"));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_write_auto_filter()
|
|
|
|
{
|
2014-06-05 06:42:17 +08:00
|
|
|
auto ws = wb.create_sheet();
|
|
|
|
ws.get_cell("F42") = "hello";
|
|
|
|
ws.auto_filter("A1:F1");
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"hello", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_auto_filter.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-06-05 06:42:17 +08:00
|
|
|
content = xlnt::writer::write_workbook(wb);
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/workbook_auto_filter.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_freeze_panes_horiz()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F42") = "hello";
|
|
|
|
ws.freeze_panes("A4");
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"hello", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_horiz.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_freeze_panes_vert()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F42") = "hello";
|
|
|
|
ws.freeze_panes("D1");
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"hello", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_vert.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_freeze_panes_both()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("F42") = "hello";
|
|
|
|
ws.freeze_panes("D4");
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {{"hello", 0}}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_freeze_panes_both.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_long_number()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("A1") = 9781231231230;
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/long_number.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_short_number()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto ws = wb.create_sheet();
|
2014-06-05 06:42:17 +08:00
|
|
|
ws.get_cell("A1") = 1234567890;
|
|
|
|
auto content = xlnt::writer::write_worksheet(ws, {}, {});
|
|
|
|
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/short_number.xml", content));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-06-05 06:42:17 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
TemporaryFile temp_file;
|
|
|
|
xlnt::workbook wb;
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|