xlnt/tests/test_dump.hpp

172 lines
4.3 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 "helpers/temporary_file.hpp"
#include <xlnt/xlnt.hpp>
2014-05-09 03:32:12 +08:00
2014-06-06 04:19:31 +08:00
class test_dump : public CxxTest::TestSuite
2014-05-09 03:32:12 +08:00
{
public:
2014-06-06 04:19:31 +08:00
test_dump() : wb(xlnt::optimization::write)
2014-05-09 03:32:12 +08:00
{
2014-05-13 07:59:33 +08:00
2014-05-09 03:32:12 +08:00
}
void test_dump_sheet_title()
{
2014-05-13 07:59:33 +08:00
auto ws = wb.create_sheet("Test1");
wb.save(temp_file.GetFilename());
xlnt::workbook wb2;
wb2.load(temp_file.GetFilename());
2014-05-09 03:32:12 +08:00
ws = wb2.get_sheet_by_name("Test1");
2014-05-15 06:31:48 +08:00
TS_ASSERT_DIFFERS(ws, nullptr);
2014-05-13 07:59:33 +08:00
TS_ASSERT_EQUALS("Test1", ws.get_title());
2014-05-09 03:32:12 +08:00
}
void test_dump_sheet()
{
2014-05-13 07:59:33 +08:00
auto test_filename = temp_file.GetFilename();
auto ws = wb.create_sheet();
std::vector<std::string> letters;
for(int i = 0; i < 20; i++)
{
2014-05-21 22:20:30 +08:00
letters.push_back(xlnt::cell_reference::column_string_from_index(i + 1));
2014-05-13 07:59:33 +08:00
}
for(int row = 0; row < 20; row++)
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
std::vector<std::string> current_row;
for(auto letter : letters)
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
current_row.push_back(letter + std::to_string(row + 1));
2014-05-09 03:32:12 +08:00
}
2014-05-31 06:42:25 +08:00
ws.append(current_row);
2014-05-09 03:32:12 +08:00
}
for(int row = 0; row < 20; row++)
{
2014-05-31 06:42:25 +08:00
std::vector<int> current_row;
for(auto letter : letters)
{
2014-05-31 06:42:25 +08:00
current_row.push_back(row + 1);
}
2014-05-31 06:42:25 +08:00
ws.append(current_row);
}
2014-05-09 03:32:12 +08:00
for(int row = 0; row < 10; row++)
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
std::vector<xlnt::date> current_row;
for(std::size_t x = 0; x < letters.size(); x++)
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
current_row.push_back(xlnt::date(2010, (x % 12) + 1, row + 1));
}
2014-05-31 06:42:25 +08:00
ws.append(current_row);
}
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
for(int row = 0; row < 20; row++)
{
2014-05-31 06:42:25 +08:00
std::vector<std::string> current_row;
for(auto letter : letters)
{
current_row.push_back("=" + letter + std::to_string(row + 1));
}
ws.append(current_row);
}
wb.save(test_filename);
2014-05-31 06:42:25 +08:00
xlnt::workbook wb2;
wb2.load(test_filename);
ws = wb2[0];
for(auto row : ws.rows())
{
for(auto cell : row)
{
2014-05-31 06:42:25 +08:00
auto row = cell.get_row();
if(row <= 20)
{
std::string expected = cell.get_reference().to_string();
TS_ASSERT_EQUALS(cell.get_data_type(), xlnt::cell::type::string);
TS_ASSERT_EQUALS(cell, expected);
}
else if(row <= 40)
{
TS_ASSERT_EQUALS(cell.get_data_type(), xlnt::cell::type::numeric);
TS_ASSERT_EQUALS(cell, (int)row);
}
else if(row <= 50)
{
xlnt::date expected(2010, (cell.get_reference().get_column_index() % 12) + 1, row + 1);
TS_ASSERT_EQUALS(cell.get_data_type(), xlnt::cell::type::numeric);
TS_ASSERT(cell.is_date());
TS_ASSERT_EQUALS(cell, expected);
}
else
{
std::string expected = "=" + cell.get_reference().to_string();
TS_ASSERT_EQUALS(cell.get_data_type(), xlnt::cell::type::formula);
TS_ASSERT_EQUALS(cell, expected);
}
2014-05-09 03:32:12 +08:00
}
2014-05-13 01:42:28 +08:00
}
2014-05-09 03:32:12 +08:00
}
void test_table_builder()
{
2014-05-31 06:42:25 +08:00
static const std::vector<std::pair<std::string, int>> result = {{"a", 0}, {"b", 1}, {"c", 2}, {"d", 3}};
xlnt::string_table_builder sb;
2014-05-09 03:32:12 +08:00
2014-05-13 07:59:33 +08:00
for(auto pair : result)
{
for(int i = 0; i < 5; i++)
2014-05-13 01:42:28 +08:00
{
2014-05-13 07:59:33 +08:00
sb.add(pair.first);
}
}
2014-05-09 03:32:12 +08:00
auto table = sb.get_table();
2014-05-09 03:32:12 +08:00
for(auto pair : result)
{
TS_ASSERT_EQUALS(pair.second, table[pair.first]);
2014-05-13 07:59:33 +08:00
}
2014-05-09 03:32:12 +08:00
}
void test_dump_twice()
{
2014-05-13 07:59:33 +08:00
auto test_filename = temp_file.GetFilename();
2014-05-09 03:32:12 +08:00
2014-05-13 07:59:33 +08:00
auto ws = wb.create_sheet();
2014-05-09 03:32:12 +08:00
2014-05-13 07:59:33 +08:00
std::vector<std::string> to_append = {"hello"};
ws.append(to_append);
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
wb.save(test_filename);
2014-05-21 22:20:30 +08:00
std::remove(test_filename.c_str());
2014-05-31 06:42:25 +08:00
TS_ASSERT_THROWS(wb.save(test_filename), xlnt::workbook_already_saved);
2014-05-09 03:32:12 +08:00
}
void test_append_after_save()
{
2014-05-13 07:59:33 +08:00
auto ws = wb.create_sheet();
2014-05-09 03:32:12 +08:00
2014-05-13 07:59:33 +08:00
std::vector<std::string> to_append = {"hello"};
ws.append(to_append);
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
wb.save(temp_file.GetFilename());
std::remove(temp_file.GetFilename().c_str());
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
TS_ASSERT_THROWS(ws.append(to_append), xlnt::workbook_already_saved);
2014-05-09 03:32:12 +08:00
}
2014-05-13 07:59:33 +08:00
private:
2014-05-31 06:42:25 +08:00
xlnt::workbook wb;
2014-05-13 07:59:33 +08:00
TemporaryFile temp_file;
2014-05-09 03:32:12 +08:00
};