2014-05-09 03:32:12 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-20 08:41:21 +08:00
|
|
|
#include <cmath>
|
2014-05-09 03:32:12 +08:00
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
2016-06-20 02:43:41 +08:00
|
|
|
#include <sstream>
|
2014-05-09 03:32:12 +08:00
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
2016-06-20 02:43:41 +08:00
|
|
|
#include <xlnt/xlnt.hpp>
|
2014-05-09 03:32:12 +08:00
|
|
|
|
2016-11-10 08:52:18 +08:00
|
|
|
#include <numeric>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <random>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
int random(int min, int max)
|
|
|
|
{
|
|
|
|
static std::random_device device{};
|
|
|
|
static std::default_random_engine engine{ device() };
|
|
|
|
std::uniform_int_distribution<int> distribution{ min, max };
|
|
|
|
return distribution(engine);
|
|
|
|
}
|
|
|
|
|
2014-06-06 04:19:31 +08:00
|
|
|
class test_cell : public CxxTest::TestSuite
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-10-02 11:09:25 +08:00
|
|
|
void test_infer_numeric()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("4.2", true);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 4.2L);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("-42.000", true);
|
|
|
|
TS_ASSERT(cell.value<int>() == -42);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("0", true);
|
|
|
|
TS_ASSERT(cell.value<int>() == 0);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("0.9999", true);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 0.9999L);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("99E-02", true);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 0.99L);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("4", true);
|
|
|
|
TS_ASSERT(cell.value<int>() == 4);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("-1E3", true);
|
|
|
|
TS_ASSERT(cell.value<int>() == -1000);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("2e+2", true);
|
|
|
|
TS_ASSERT(cell.value<int>() == 200);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("3.1%", true);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 0.031L);
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("03:40:16", true);
|
|
|
|
TS_ASSERT(cell.value<xlnt::time>() == xlnt::time(3, 40, 16));
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("03:", true);
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::string>(), "03:");
|
2016-07-18 01:49:59 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("03:40", true);
|
|
|
|
TS_ASSERT(cell.value<xlnt::time>() == xlnt::time(3, 40));
|
2015-10-02 11:09:25 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("30:33.865633336", true);
|
|
|
|
TS_ASSERT(cell.value<xlnt::time>() == xlnt::time(0, 30, 33, 865633));
|
2015-10-02 11:09:25 +08:00
|
|
|
}
|
|
|
|
|
2015-10-07 00:31:49 +08:00
|
|
|
void test_ctor()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference("A", 1));
|
2015-10-07 00:31:49 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::null);
|
|
|
|
TS_ASSERT(cell.column() == "A");
|
|
|
|
TS_ASSERT(cell.row() == 1);
|
|
|
|
TS_ASSERT(cell.reference() == "A1");
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(!cell.has_value());
|
2015-10-07 00:31:49 +08:00
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
void test_null()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
const auto datatypes =
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-03-14 11:46:01 +08:00
|
|
|
xlnt::cell::type::null,
|
|
|
|
xlnt::cell::type::boolean,
|
|
|
|
xlnt::cell::type::error,
|
|
|
|
xlnt::cell::type::formula,
|
|
|
|
xlnt::cell::type::numeric,
|
|
|
|
xlnt::cell::type::string
|
2015-10-14 01:56:07 +08:00
|
|
|
};
|
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
for(const auto &datatype : datatypes)
|
2015-10-14 01:56:07 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.data_type(datatype);
|
|
|
|
TS_ASSERT(cell.data_type() == datatype);
|
2015-10-14 01:56:07 +08:00
|
|
|
cell.clear_value();
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::null);
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_string()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("hello");
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::string);
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(".");
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::string);
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("0800");
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::string);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_formula1()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2016-07-24 07:15:17 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("=42", true);
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::formula);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_formula2()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("=if(A1<4;-1;1)", true);
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::formula);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2016-07-24 07:15:17 +08:00
|
|
|
|
|
|
|
void test_formula3()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2016-07-24 07:15:17 +08:00
|
|
|
|
|
|
|
TS_ASSERT(!cell.has_formula());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_THROWS(cell.formula(""), xlnt::invalid_parameter);
|
|
|
|
TS_ASSERT(!cell.has_formula());
|
|
|
|
cell.formula("=42");
|
2016-07-24 07:15:17 +08:00
|
|
|
TS_ASSERT(cell.has_formula());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.formula(), "42");
|
2016-07-24 07:15:17 +08:00
|
|
|
cell.clear_formula();
|
|
|
|
TS_ASSERT(!cell.has_formula());
|
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_not_formula()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("=");
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::string);
|
|
|
|
TS_ASSERT(cell.value<std::string>() == "=");
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(!cell.has_formula());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_boolean()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
for(auto value : {true, false})
|
2014-05-30 08:52:14 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(value);
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::boolean);
|
2014-05-30 08:52:14 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_error_codes()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2015-10-30 07:37:07 +08:00
|
|
|
for(auto error_code : xlnt::cell::error_codes())
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(error_code.first, true);
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::error);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
void test_insert_datetime()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::datetime(2010, 7, 13, 6, 37, 41));
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 40372.27616898148L);
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(cell.is_date());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.number_format().format_string() == "yyyy-mm-dd h:mm:ss");
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_insert_date()
|
2014-06-11 06:36:31 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::date(2010, 7, 13));
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 40372.L);
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(cell.is_date());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.number_format().format_string() == "yyyy-mm-dd");
|
2014-06-11 06:36:31 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_insert_time()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::time(1, 3));
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::numeric);
|
|
|
|
TS_ASSERT(cell.value<long double>() == 0.04375L);
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(cell.is_date());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.number_format().format_string() == "h:mm:ss");
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_cell_formatted_as_date1()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::datetime::today());
|
2015-10-14 01:56:07 +08:00
|
|
|
cell.clear_value();
|
2015-10-17 06:35:11 +08:00
|
|
|
TS_ASSERT(!cell.is_date()); // disagree with openpyxl
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(!cell.has_value());
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_cell_formatted_as_date2()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::datetime::today());
|
|
|
|
cell.value("testme");
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(!cell.is_date());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.value<std::string>() == "testme");
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_cell_formatted_as_date3()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::datetime::today());
|
|
|
|
cell.value(true);
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(!cell.is_date());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.value<bool>() == true);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void test_illegal_characters()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
// The bytes 0x00 through 0x1F inclusive must be manually escaped in values.
|
|
|
|
auto illegal_chrs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
|
|
|
|
|
|
|
|
for(auto i : illegal_chrs)
|
2014-05-13 01:42:28 +08:00
|
|
|
{
|
2015-11-11 07:58:54 +08:00
|
|
|
std::string str(1, i);
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_THROWS(cell.value(str), xlnt::illegal_character);
|
2014-05-13 01:42:28 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(std::string(1, 33));
|
|
|
|
cell.value(std::string(1, 9)); // Tab
|
|
|
|
cell.value(std::string(1, 10)); // Newline
|
|
|
|
cell.value(std::string(1, 13)); // Carriage return
|
|
|
|
cell.value(" Leading and trailing spaces are legal ");
|
2015-10-14 01:56:07 +08:00
|
|
|
}
|
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
// void test_time_regex() {}
|
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
void test_timedelta()
|
2014-06-11 06:36:31 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(xlnt::timedelta(1, 3, 0, 0, 0));
|
2015-11-23 01:41:27 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.value<long double>() == 1.125);
|
|
|
|
TS_ASSERT(cell.data_type() == xlnt::cell::type::numeric);
|
2015-10-14 01:56:07 +08:00
|
|
|
TS_ASSERT(!cell.is_date());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.number_format().format_string() == "[hh]:mm:ss");
|
2014-06-11 06:36:31 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_repr()
|
2014-06-11 06:36:31 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT(cell.to_repr() == "<Cell Sheet1.A1>");
|
2014-06-11 06:36:31 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
|
|
|
void test_cell_offset()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
TS_ASSERT(cell.offset(1, 2).reference() == "B3");
|
2015-11-23 01:41:27 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2015-11-23 01:41:27 +08:00
|
|
|
void test_font()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2016-08-18 19:34:18 +08:00
|
|
|
auto font = xlnt::font().bold(true);
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.font(font);
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
TS_ASSERT(cell.has_format());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT(cell.format().font_applied());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.font(), font);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void test_fill()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2016-08-16 12:23:49 +08:00
|
|
|
xlnt::fill fill(xlnt::pattern_fill()
|
|
|
|
.type(xlnt::pattern_fill_type::solid)
|
|
|
|
.foreground(xlnt::color::red()));
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.fill(fill);
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
TS_ASSERT(cell.has_format());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT(cell.format().fill_applied());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.fill(), fill);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void test_border()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-05-04 03:37:34 +08:00
|
|
|
xlnt::border border;
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.border(border);
|
2016-03-14 11:46:01 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
TS_ASSERT(cell.has_format());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT(cell.format().border_applied());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.border(), border);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void test_number_format()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-05-04 03:37:34 +08:00
|
|
|
|
|
|
|
xlnt::number_format format("dd--hh--mm");
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.number_format(format);
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
TS_ASSERT(cell.has_format());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT(cell.format().number_format_applied());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.number_format().format_string(), "dd--hh--mm");
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void test_alignment()
|
2014-05-09 03:32:12 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-05-04 03:37:34 +08:00
|
|
|
|
2015-10-14 01:56:07 +08:00
|
|
|
xlnt::alignment align;
|
2016-08-18 19:34:18 +08:00
|
|
|
align.wrap(true);
|
2015-10-14 01:56:07 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.alignment(align);
|
2016-03-14 11:46:01 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
TS_ASSERT(cell.has_format());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT(cell.format().alignment_applied());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.alignment(), align);
|
2014-05-09 03:32:12 +08:00
|
|
|
}
|
2014-07-20 02:43:48 +08:00
|
|
|
|
2016-03-14 11:46:01 +08:00
|
|
|
void test_protection()
|
2014-07-20 02:43:48 +08:00
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
TS_ASSERT(!cell.has_format());
|
|
|
|
|
2016-08-18 19:34:18 +08:00
|
|
|
auto protection = xlnt::protection().locked(false).hidden(true);
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.protection(protection);
|
2016-03-14 11:46:01 +08:00
|
|
|
|
2016-05-15 01:57:07 +08:00
|
|
|
TS_ASSERT(cell.has_format());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT(cell.format().protection_applied());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.protection(), protection);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
TS_ASSERT(cell.has_format());
|
|
|
|
cell.clear_format();
|
|
|
|
TS_ASSERT(!cell.has_format());
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_style()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
TS_ASSERT(!cell.has_style());
|
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
auto test_style = wb.create_style("test_style");
|
2016-08-18 19:34:18 +08:00
|
|
|
test_style.number_format(xlnt::number_format::date_ddmmyyyy(), true);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
cell.style(test_style);
|
2016-06-20 02:43:41 +08:00
|
|
|
TS_ASSERT(cell.has_style());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.style().number_format(), xlnt::number_format::date_ddmmyyyy());
|
|
|
|
TS_ASSERT_EQUALS(cell.style(), test_style);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
auto other_style = wb.create_style("other_style");
|
2016-08-18 19:34:18 +08:00
|
|
|
other_style.number_format(xlnt::number_format::date_time2(), true);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
cell.style("other_style");
|
|
|
|
TS_ASSERT_EQUALS(cell.style().number_format(), xlnt::number_format::date_time2());
|
|
|
|
TS_ASSERT_EQUALS(cell.style(), other_style);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
auto last_style = wb.create_style("last_style");
|
2016-08-18 19:34:18 +08:00
|
|
|
last_style.number_format(xlnt::number_format::percentage(), true);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
cell.style(last_style);
|
|
|
|
TS_ASSERT_EQUALS(cell.style().number_format(), xlnt::number_format::percentage());
|
|
|
|
TS_ASSERT_EQUALS(cell.style(), last_style);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT_THROWS(cell.style("doesn't exist"), xlnt::key_not_found);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
cell.clear_style();
|
|
|
|
|
|
|
|
TS_ASSERT(!cell.has_style());
|
2016-11-08 10:11:30 +08:00
|
|
|
TS_ASSERT_THROWS(cell.style(), xlnt::invalid_attribute);
|
2016-06-20 02:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_print()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = ws.cell("A1");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cell;
|
|
|
|
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
|
|
|
|
TS_ASSERT_EQUALS(stream_string, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = ws.cell("A2");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(false);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cell;
|
|
|
|
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
|
|
|
|
TS_ASSERT_EQUALS(stream_string, "FALSE");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = ws.cell("A3");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(true);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cell;
|
|
|
|
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
|
|
|
|
TS_ASSERT_EQUALS(stream_string, "TRUE");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = ws.cell("A4");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(1.234);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cell;
|
|
|
|
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
|
|
|
|
TS_ASSERT_EQUALS(stream_string, "1.234");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = ws.cell("A5");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.error("#REF");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cell;
|
|
|
|
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
|
|
|
|
TS_ASSERT_EQUALS(stream_string, "#REF");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = ws.cell("A6");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value("test");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << cell;
|
|
|
|
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
|
|
|
|
TS_ASSERT_EQUALS(stream_string, "test");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_values()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::int8_t>(4));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<int8_t>(), 4);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::uint8_t>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::uint8_t>(), 3);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::int16_t>(4));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<int16_t>(), 4);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::uint16_t>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::uint16_t>(), 3);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::int32_t>(4));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<int32_t>(), 4);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::uint32_t>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::uint32_t>(), 3);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::int64_t>(4));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<int64_t>(), 4);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::uint64_t>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::uint64_t>(), 3);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-07-24 07:15:17 +08:00
|
|
|
#ifdef __linux
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<long long>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<long long>(), 3);
|
2016-07-24 07:15:17 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<unsigned long long>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<unsigned long long>(), 3);
|
2016-07-24 07:15:17 +08:00
|
|
|
#endif
|
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<std::uint64_t>(3));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::uint64_t>(), 3);
|
2016-07-24 07:15:17 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<float>(3.14));
|
|
|
|
TS_ASSERT_DELTA(cell.value<float>(), 3.14, 0.001);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<double>(4.1415));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<double>(), 4.1415);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
cell.value(static_cast<long double>(3.141592));
|
|
|
|
TS_ASSERT_EQUALS(cell.value<long double>(), 3.141592);
|
2016-06-20 02:43:41 +08:00
|
|
|
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell2 = ws.cell("A2");
|
|
|
|
cell2.value(std::string(100'000, 'a'));
|
|
|
|
cell.value(cell2);
|
|
|
|
TS_ASSERT_EQUALS(cell.value<std::string>(), std::string(32'767, 'a'));
|
2014-07-20 02:43:48 +08:00
|
|
|
}
|
2016-06-20 02:49:41 +08:00
|
|
|
|
2016-07-19 09:38:16 +08:00
|
|
|
void test_reference()
|
|
|
|
{
|
|
|
|
xlnt::cell_reference_hash hash;
|
|
|
|
TS_ASSERT_DIFFERS(hash(xlnt::cell_reference("A2")), hash(xlnt::cell_reference(1, 1)));
|
|
|
|
TS_ASSERT_EQUALS(hash(xlnt::cell_reference("A2")), hash(xlnt::cell_reference(1, 2)));
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS((xlnt::cell_reference("A1"), xlnt::cell_reference("B2")), xlnt::range_reference("A1:B2"));
|
|
|
|
|
2016-07-30 06:55:49 +08:00
|
|
|
TS_ASSERT_THROWS(xlnt::cell_reference("A1&"), xlnt::invalid_cell_reference);
|
|
|
|
TS_ASSERT_THROWS(xlnt::cell_reference("A"), xlnt::invalid_cell_reference);
|
2016-07-19 09:38:16 +08:00
|
|
|
|
|
|
|
auto ref = xlnt::cell_reference("$B$7");
|
|
|
|
TS_ASSERT(ref.row_absolute());
|
|
|
|
TS_ASSERT(ref.column_absolute());
|
|
|
|
|
|
|
|
TS_ASSERT(xlnt::cell_reference("A1") == "A1");
|
|
|
|
TS_ASSERT(xlnt::cell_reference("A1") != "A2");
|
|
|
|
}
|
2016-07-21 08:10:19 +08:00
|
|
|
|
|
|
|
void test_anchor()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = wb.active_sheet().cell("A1");
|
|
|
|
auto anchor = cell.anchor();
|
2016-07-21 08:10:19 +08:00
|
|
|
TS_ASSERT_EQUALS(anchor.first, 0);
|
|
|
|
TS_ASSERT_EQUALS(anchor.second, 0);
|
|
|
|
}
|
2016-07-24 07:15:17 +08:00
|
|
|
|
|
|
|
void test_hyperlink()
|
|
|
|
{
|
|
|
|
xlnt::workbook wb;
|
2016-12-02 21:37:50 +08:00
|
|
|
auto cell = wb.active_sheet().cell("A1");
|
2016-07-24 07:15:17 +08:00
|
|
|
TS_ASSERT(!cell.has_hyperlink());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_THROWS(cell.hyperlink(), xlnt::invalid_attribute);
|
|
|
|
TS_ASSERT_THROWS(cell.hyperlink("notaurl"), xlnt::invalid_parameter);
|
|
|
|
TS_ASSERT_THROWS(cell.hyperlink(""), xlnt::invalid_parameter);
|
|
|
|
cell.hyperlink("http://example.com");
|
2016-07-24 07:15:17 +08:00
|
|
|
TS_ASSERT(cell.has_hyperlink());
|
2016-12-02 21:37:50 +08:00
|
|
|
TS_ASSERT_EQUALS(cell.hyperlink(), "http://example.com");
|
2016-07-24 07:15:17 +08:00
|
|
|
}
|
2016-10-29 22:23:04 +08:00
|
|
|
|
|
|
|
void test_comment()
|
|
|
|
{
|
2016-12-02 21:37:50 +08:00
|
|
|
xlnt::workbook wb;
|
|
|
|
auto ws = wb.active_sheet();
|
|
|
|
auto cell = ws.cell("A1");
|
|
|
|
TS_ASSERT(!cell.has_comment());
|
|
|
|
TS_ASSERT_THROWS(cell.comment(), xlnt::exception);
|
|
|
|
cell.comment(xlnt::comment("comment", "author"));
|
|
|
|
TS_ASSERT(cell.has_comment());
|
|
|
|
TS_ASSERT_EQUALS(cell.comment(), xlnt::comment("comment", "author"));
|
|
|
|
cell.clear_comment();
|
|
|
|
TS_ASSERT(!cell.has_comment());
|
|
|
|
TS_ASSERT_THROWS(cell.comment(), xlnt::exception);
|
2016-10-29 22:23:04 +08:00
|
|
|
}
|
2014-05-09 03:32:12 +08:00
|
|
|
};
|