xlnt/tests/DumpTestSuite.h

168 lines
4.1 KiB
C
Raw Normal View History

2014-05-09 03:32:12 +08:00
#pragma once
#include <iostream>
#include <cxxtest/TestSuite.h>
2014-05-13 07:59:33 +08:00
#include "TemporaryFile.h"
2014-05-22 05:48:51 +08:00
#include <xlnt/xlnt.h>
2014-05-09 03:32:12 +08:00
class DumpTestSuite : public CxxTest::TestSuite
{
public:
DumpTestSuite()
{
2014-05-13 07:59:33 +08:00
2014-05-09 03:32:12 +08:00
}
2014-05-13 07:59:33 +08:00
//_COL_CONVERSION_CACHE = dict((get_column_letter(i), i) for i in range(1, 18279));
2014-05-09 03:32:12 +08:00
void test_dump_sheet_title()
{
2014-05-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::write);
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();
2014-05-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::write);
2014-05-13 07:59:33 +08:00
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
}
std::vector<std::vector<std::string>> expected_rows;
2014-05-13 07:59:33 +08:00
for(int row = 0; row < 20; row++)
2014-05-09 03:32:12 +08:00
{
expected_rows.push_back(std::vector<std::string>());
for(auto letter : letters)
2014-05-09 03:32:12 +08:00
{
expected_rows.back().push_back(letter + std::to_string(row + 1));
2014-05-09 03:32:12 +08:00
}
}
for(int row = 0; row < 20; row++)
{
expected_rows.push_back(std::vector<std::string>());
for(auto letter : letters)
{
expected_rows.back().push_back(letter + std::to_string(row + 1));
}
}
2014-05-09 03:32:12 +08:00
for(int row = 0; row < 10; row++)
2014-05-09 03:32:12 +08:00
{
expected_rows.push_back(std::vector<std::string>());
for(auto letter : letters)
2014-05-09 03:32:12 +08:00
{
expected_rows.back().push_back(letter + std::to_string(row + 1));
}
}
2014-05-09 03:32:12 +08:00
for(auto row : expected_rows)
{
ws.append(row);
}
wb.save(test_filename);
2014-05-16 05:12:09 +08:00
//xlnt::workbook wb2;
//wb2.load(test_filename);
//ws = wb2[0];
auto expected_row = expected_rows.begin();
for(auto row : ws.rows())
{
auto expected_cell = expected_row->begin();
for(auto cell : row)
{
TS_ASSERT_EQUALS(cell, *expected_cell);
expected_cell++;
2014-05-09 03:32:12 +08:00
}
expected_row++;
2014-05-13 01:42:28 +08:00
}
2014-05-09 03:32:12 +08:00
}
void test_table_builder()
{
xlnt::string_table_builder sb;
2014-05-09 03:32:12 +08:00
std::vector<std::pair<std::string, int>> result = {{"a", 0}, {"b", 1}, {"c", 2}, {"d", 3}};
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_open_too_many_files()
{
2014-05-13 07:59:33 +08:00
auto test_filename = temp_file.GetFilename();
2014-05-13 01:42:28 +08:00
2014-05-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::write);
2014-05-13 07:59:33 +08:00
2014-05-15 06:31:48 +08:00
for(int i = 0; i < 2; i++) // over 200 worksheets should raise an OSError("too many open files")
2014-05-13 01:42:28 +08:00
{
wb.create_sheet();
wb.save(test_filename);
2014-05-15 10:07:23 +08:00
std::remove(test_filename.c_str());
2014-05-13 01:42:28 +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-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::write);
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-13 07:59:33 +08:00
wb.save(test_filename);
2014-05-09 03:32:12 +08:00
}
void test_append_after_save()
{
2014-05-21 22:20:30 +08:00
xlnt::workbook wb(xlnt::optimization::write);
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 07:59:33 +08:00
{
TemporaryFile temp2;
wb.save(temp2.GetFilename());
}
2014-05-09 03:32:12 +08:00
2014-05-13 07:59:33 +08:00
ws.append(to_append);
2014-05-09 03:32:12 +08:00
}
2014-05-13 07:59:33 +08:00
private:
TemporaryFile temp_file;
2014-05-09 03:32:12 +08:00
};