xlnt/tests/test_write.hpp

232 lines
8.2 KiB
C++
Raw Normal View History

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()
{
xlnt::workbook wbk;
wbk.get_active_sheet().get_cell("A2").set_value("xlnt");
wbk.get_active_sheet().get_cell("B5").set_value(88);
wbk.get_active_sheet().get_cell("B5").get_style().set_number_format(xlnt::number_format(xlnt::number_format::format::percentage_00));
wbk.save(temp_file.GetFilename());
2014-06-05 06:42:17 +08:00
if(PathHelper::FileExists(temp_file.GetFilename()))
2014-06-11 05:12:15 +08:00
{
PathHelper::DeleteFile(temp_file.GetFilename());
}
2014-06-05 06:42:17 +08:00
TS_ASSERT(!PathHelper::FileExists(temp_file.GetFilename()));
wb_.save(temp_file.GetFilename());
2014-06-05 06:42:17 +08:00
TS_ASSERT(PathHelper::FileExists(temp_file.GetFilename()));
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;
2015-10-14 12:03:48 +08:00
auto content = xlnt::write_workbook_rels(wb);
2014-06-05 06:42:17 +08:00
2014-06-11 05:12:15 +08:00
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;
2015-10-14 12:03:48 +08:00
auto content = xlnt::write_workbook(wb);
2014-06-05 06:42:17 +08:00
2014-06-11 05:12:15 +08:00
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-10 12:29:49 +08:00
std::vector<std::string> table = {"hello", "world", "nice"};
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_shared_strings(table);
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sharedStrings.xml", content));
2014-05-09 03:32:12 +08:00
}
void test_write_worksheet()
{
auto ws = wb_.create_sheet();
2014-07-26 04:39:25 +08:00
ws.get_cell("F42").set_value("hello");
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"hello"}, {});
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content));
2014-05-09 03:32:12 +08:00
}
void test_write_hidden_worksheet()
{
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);
2014-07-26 04:39:25 +08:00
ws.get_cell("F42").set_value("hello");
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"hello"}, {});
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1.xml", content));
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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {}, {});
2014-07-26 04:39:25 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_bool.xml", content));
2014-05-09 03:32:12 +08:00
}
void test_write_formula()
{
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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {}, {});
2014-06-11 05:12:15 +08:00
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-07-25 05:31:46 +08:00
xlnt::workbook wb_guess_types;
wb_guess_types.set_guess_types(true);
auto ws = wb_guess_types.create_sheet();
2014-07-26 04:39:25 +08:00
ws.get_cell("F1").set_value("13%");
2014-07-25 05:31:46 +08:00
auto style_id_by_hash = xlnt::style_writer(wb_guess_types).get_style_by_hash();
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {}, style_id_by_hash);
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_style.xml", content));
2014-05-09 03:32:12 +08:00
}
void test_write_height()
{
auto ws = wb_.create_sheet();
2014-07-26 04:39:25 +08:00
ws.get_cell("F1").set_value(10);
ws.get_row_properties(ws.get_cell("F1").get_row()).height = 30;
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {}, {});
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_height.xml", content));
2014-05-09 03:32:12 +08:00
}
void test_write_hyperlink()
{
auto ws = wb_.create_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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"test"}, {});
2014-06-11 05:12:15 +08:00
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()
{
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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet_rels(ws);
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_hyperlink.xml.rels", content));
2014-05-09 03:32:12 +08:00
}
2014-08-02 04:46:54 +08:00
void _test_write_hyperlink_image_rels()
{
TS_SKIP("not implemented");
}
2014-05-09 03:32:12 +08:00
void test_hyperlink_value()
{
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").get_value<std::string>());
2014-07-26 04:39:25 +08:00
ws.get_cell("A1").set_value("test");
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");
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"hello"}, {});
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/sheet1_auto_filter.xml", content));
2014-05-09 03:32:12 +08:00
2015-10-14 12:03:48 +08:00
content = xlnt::write_workbook(wb);
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/workbook_auto_filter.xml", content));
2014-05-09 03:32:12 +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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"hello"}, {});
2014-07-26 04:39:25 +08:00
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()
{
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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"hello"}, {});
2014-06-11 05:12:15 +08:00
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()
{
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-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {"hello"}, {});
2014-06-11 05:12:15 +08:00
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()
{
auto ws = wb_.create_sheet();
ws.get_cell("A1").set_value(9781231231230LL);
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {}, {});
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/long_number.xml", content));
2014-05-09 03:32:12 +08:00
}
void test_short_number()
{
auto ws = wb_.create_sheet();
2014-07-26 04:39:25 +08:00
ws.get_cell("A1").set_value(1234567890);
2015-10-15 06:05:13 +08:00
auto content = xlnt::write_worksheet(ws, {}, {});
2014-06-11 05:12:15 +08:00
TS_ASSERT(Helper::EqualsFileContent(PathHelper::GetDataDirectory() + "/writer/expected/short_number.xml", content));
2014-05-09 03:32:12 +08:00
}
2014-08-02 04:46:54 +08:00
void _test_write_images()
{
TS_SKIP("not implemented");
}
2014-06-05 06:42:17 +08:00
private:
TemporaryFile temp_file;
xlnt::workbook wb_;
2014-05-09 03:32:12 +08:00
};