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-10 03:54:06 +08:00
|
|
|
#include "../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-13 07:59:33 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.optimized_write(true);
|
|
|
|
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-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();
|
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.optimized_write(true);
|
|
|
|
auto ws = wb.create_sheet();
|
|
|
|
|
|
|
|
std::vector<std::string> letters;
|
|
|
|
|
|
|
|
for(int i = 0; i < 20; i++)
|
|
|
|
{
|
|
|
|
letters.push_back(xlnt::cell::get_column_letter(i + 1));
|
|
|
|
}
|
|
|
|
|
2014-05-14 04:32: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
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
expected_rows.push_back(std::vector<std::string>());
|
|
|
|
for(auto letter : letters)
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
expected_rows.back().push_back(letter + std::to_string(row + 1));
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 04:32:33 +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
|
|
|
|
2014-05-14 04:32:33 +08:00
|
|
|
for(int row = 0; row < 10; row++)
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
expected_rows.push_back(std::vector<std::string>());
|
|
|
|
for(auto letter : letters)
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
expected_rows.back().push_back(letter + std::to_string(row + 1));
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-14 04:32:33 +08:00
|
|
|
for(auto row : expected_rows)
|
|
|
|
{
|
|
|
|
ws.append(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.save(test_filename);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2014-05-14 04:32:33 +08:00
|
|
|
expected_row++;
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_table_builder()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
xlnt::string_table_builder sb;
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-13 07:59:33 +08:00
|
|
|
std::unordered_map<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-14 04:32:33 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-14 04:32:33 +08:00
|
|
|
auto table = sb.get_table();
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2014-05-14 04:32:33 +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-13 07:59:33 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.optimized_write(true);
|
|
|
|
|
|
|
|
for(int i = 0; i < 200; 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-13 07:59:33 +08:00
|
|
|
unlink(test_filename.c_str());
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_create_temp_file()
|
|
|
|
{
|
2014-05-14 04:32:33 +08:00
|
|
|
auto f = xlnt::writer::create_temporary_file();
|
|
|
|
TS_ASSERT(xlnt::file::exists(f));
|
|
|
|
xlnt::writer::delete_temporary_file(f);
|
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
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.optimized_write(true);
|
|
|
|
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-13 07:59:33 +08:00
|
|
|
unlink(test_filename.c_str());
|
|
|
|
wb.save(test_filename);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_append_after_save()
|
|
|
|
{
|
2014-05-13 07:59:33 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
wb.optimized_write(true);
|
|
|
|
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
|
|
|
};
|