mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
659 lines
18 KiB
C++
659 lines
18 KiB
C++
#include <sstream>
|
|
|
|
#include <helpers/test_suite.hpp>
|
|
#include <helpers/assertions.hpp>
|
|
|
|
#include <xlnt/xlnt.hpp>
|
|
|
|
class cell_test_suite : public test_suite
|
|
{
|
|
public:
|
|
cell_test_suite()
|
|
{
|
|
register_test(test_infer_numeric);
|
|
register_test(test_constructor);
|
|
register_test(test_null);
|
|
register_test(test_string);
|
|
register_test(test_formula1);
|
|
register_test(test_formula2);
|
|
register_test(test_formula3);
|
|
register_test(test_not_formula);
|
|
register_test(test_boolean);
|
|
register_test(test_error_codes);
|
|
register_test(test_insert_datetime);
|
|
register_test(test_insert_date);
|
|
register_test(test_insert_time);
|
|
register_test(test_cell_formatted_as_date1);
|
|
register_test(test_cell_formatted_as_date2);
|
|
register_test(test_cell_formatted_as_date3);
|
|
register_test(test_illegal_characters);
|
|
register_test(test_timedelta);
|
|
register_test(test_cell_offset);
|
|
register_test(test_font);
|
|
register_test(test_fill);
|
|
register_test(test_border);
|
|
register_test(test_number_format);
|
|
register_test(test_alignment);
|
|
register_test(test_protection);
|
|
register_test(test_style);
|
|
register_test(test_print);
|
|
register_test(test_values);
|
|
register_test(test_reference);
|
|
register_test(test_anchor);
|
|
register_test(test_hyperlink);
|
|
register_test(test_comment);
|
|
}
|
|
|
|
private:
|
|
void test_infer_numeric()
|
|
{
|
|
assert(1 == 0);
|
|
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
cell.value("4.2", true);
|
|
assert(cell.value<long double>() == 4.2L);
|
|
|
|
cell.value("-42.000", true);
|
|
assert(cell.value<int>() == -42);
|
|
|
|
cell.value("0", true);
|
|
assert(cell.value<int>() == 0);
|
|
|
|
cell.value("0.9999", true);
|
|
assert(cell.value<long double>() == 0.9999L);
|
|
|
|
cell.value("99E-02", true);
|
|
assert(cell.value<long double>() == 0.99L);
|
|
|
|
cell.value("4", true);
|
|
assert(cell.value<int>() == 4);
|
|
|
|
cell.value("-1E3", true);
|
|
assert(cell.value<int>() == -1000);
|
|
|
|
cell.value("2e+2", true);
|
|
assert(cell.value<int>() == 200);
|
|
|
|
cell.value("3.1%", true);
|
|
assert(cell.value<long double>() == 0.031L);
|
|
|
|
cell.value("03:40:16", true);
|
|
assert(cell.value<xlnt::time>() == xlnt::time(3, 40, 16));
|
|
|
|
cell.value("03:", true);
|
|
assert_equals(cell.value<std::string>(), "03:");
|
|
|
|
cell.value("03:40", true);
|
|
assert(cell.value<xlnt::time>() == xlnt::time(3, 40));
|
|
|
|
cell.value("30:33.865633336", true);
|
|
assert(cell.value<xlnt::time>() == xlnt::time(0, 30, 33, 865633));
|
|
}
|
|
|
|
void test_constructor()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference("A", 1));
|
|
|
|
assert(cell.data_type() == xlnt::cell::type::null);
|
|
assert(cell.column() == "A");
|
|
assert(cell.row() == 1);
|
|
assert(cell.reference() == "A1");
|
|
assert(!cell.has_value());
|
|
}
|
|
|
|
void test_null()
|
|
{
|
|
xlnt::workbook wb;
|
|
|
|
const auto datatypes =
|
|
{
|
|
xlnt::cell::type::null,
|
|
xlnt::cell::type::boolean,
|
|
xlnt::cell::type::error,
|
|
xlnt::cell::type::formula,
|
|
xlnt::cell::type::numeric,
|
|
xlnt::cell::type::string
|
|
};
|
|
|
|
for(const auto &datatype : datatypes)
|
|
{
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.data_type(datatype);
|
|
assert(cell.data_type() == datatype);
|
|
cell.clear_value();
|
|
assert(cell.data_type() == xlnt::cell::type::null);
|
|
}
|
|
}
|
|
|
|
void test_string()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value("hello");
|
|
assert(cell.data_type() == xlnt::cell::type::string);
|
|
|
|
cell.value(".");
|
|
assert(cell.data_type() == xlnt::cell::type::string);
|
|
|
|
cell.value("0800");
|
|
assert(cell.data_type() == xlnt::cell::type::string);
|
|
}
|
|
|
|
void test_formula1()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value("=42", true);
|
|
assert(cell.data_type() == xlnt::cell::type::formula);
|
|
}
|
|
|
|
void test_formula2()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value("=if(A1<4;-1;1)", true);
|
|
assert(cell.data_type() == xlnt::cell::type::formula);
|
|
}
|
|
|
|
void test_formula3()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
assert(!cell.has_formula());
|
|
assert_throws_nothing(cell.formula(""));
|
|
assert(!cell.has_formula());
|
|
cell.formula("=42");
|
|
assert(cell.has_formula());
|
|
assert_equals(cell.formula(), "42");
|
|
cell.clear_formula();
|
|
assert(!cell.has_formula());
|
|
}
|
|
|
|
|
|
void test_not_formula()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value("=");
|
|
assert(cell.data_type() == xlnt::cell::type::string);
|
|
assert(cell.value<std::string>() == "=");
|
|
assert(!cell.has_formula());
|
|
}
|
|
|
|
void test_boolean()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
for(auto value : {true, false})
|
|
{
|
|
cell.value(value);
|
|
assert(cell.data_type() == xlnt::cell::type::boolean);
|
|
}
|
|
}
|
|
|
|
void test_error_codes()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
for(auto error_code : xlnt::cell::error_codes())
|
|
{
|
|
cell.value(error_code.first, true);
|
|
assert(cell.data_type() == xlnt::cell::type::error);
|
|
}
|
|
}
|
|
|
|
void test_insert_datetime()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::datetime(2010, 7, 13, 6, 37, 41));
|
|
|
|
assert(cell.data_type() == xlnt::cell::type::numeric);
|
|
assert(cell.value<long double>() == 40372.27616898148L);
|
|
assert(cell.is_date());
|
|
assert(cell.number_format().format_string() == "yyyy-mm-dd h:mm:ss");
|
|
}
|
|
|
|
void test_insert_date()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::date(2010, 7, 13));
|
|
assert(cell.data_type() == xlnt::cell::type::numeric);
|
|
assert(cell.value<long double>() == 40372.L);
|
|
assert(cell.is_date());
|
|
assert(cell.number_format().format_string() == "yyyy-mm-dd");
|
|
}
|
|
|
|
void test_insert_time()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::time(1, 3));
|
|
assert(cell.data_type() == xlnt::cell::type::numeric);
|
|
assert(cell.value<long double>() == 0.04375L);
|
|
assert(cell.is_date());
|
|
assert(cell.number_format().format_string() == "h:mm:ss");
|
|
}
|
|
|
|
void test_cell_formatted_as_date1()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::datetime::today());
|
|
cell.clear_value();
|
|
assert(!cell.is_date()); // disagree with openpyxl
|
|
assert(!cell.has_value());
|
|
}
|
|
|
|
void test_cell_formatted_as_date2()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::datetime::today());
|
|
cell.value("testme");
|
|
assert(!cell.is_date());
|
|
assert(cell.value<std::string>() == "testme");
|
|
}
|
|
|
|
void test_cell_formatted_as_date3()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::datetime::today());
|
|
cell.value(true);
|
|
assert(!cell.is_date());
|
|
assert(cell.value<bool>() == true);
|
|
}
|
|
|
|
void test_illegal_characters()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
// 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)
|
|
{
|
|
std::string str(1, i);
|
|
assert_throws(cell.value(str), xlnt::illegal_character);
|
|
}
|
|
|
|
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 ");
|
|
}
|
|
|
|
// void test_time_regex() {}
|
|
|
|
void test_timedelta()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
|
|
cell.value(xlnt::timedelta(1, 3, 0, 0, 0));
|
|
|
|
assert(cell.value<long double>() == 1.125);
|
|
assert(cell.data_type() == xlnt::cell::type::numeric);
|
|
assert(!cell.is_date());
|
|
assert(cell.number_format().format_string() == "[hh]:mm:ss");
|
|
}
|
|
|
|
void test_cell_offset()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell(xlnt::cell_reference(1, 1));
|
|
assert(cell.offset(1, 2).reference() == "B3");
|
|
}
|
|
|
|
void test_font()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
auto font = xlnt::font().bold(true);
|
|
|
|
cell.font(font);
|
|
|
|
assert(cell.has_format());
|
|
assert(cell.format().font_applied());
|
|
assert_equals(cell.font(), font);
|
|
}
|
|
|
|
void test_fill()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
xlnt::fill fill(xlnt::pattern_fill()
|
|
.type(xlnt::pattern_fill_type::solid)
|
|
.foreground(xlnt::color::red()));
|
|
cell.fill(fill);
|
|
|
|
assert(cell.has_format());
|
|
assert(cell.format().fill_applied());
|
|
assert_equals(cell.fill(), fill);
|
|
}
|
|
|
|
void test_border()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
xlnt::border border;
|
|
cell.border(border);
|
|
|
|
assert(cell.has_format());
|
|
assert(cell.format().border_applied());
|
|
assert_equals(cell.border(), border);
|
|
}
|
|
|
|
void test_number_format()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
xlnt::number_format format("dd--hh--mm");
|
|
cell.number_format(format);
|
|
|
|
assert(cell.has_format());
|
|
assert(cell.format().number_format_applied());
|
|
assert_equals(cell.number_format().format_string(), "dd--hh--mm");
|
|
}
|
|
|
|
void test_alignment()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
xlnt::alignment align;
|
|
align.wrap(true);
|
|
|
|
cell.alignment(align);
|
|
|
|
assert(cell.has_format());
|
|
assert(cell.format().alignment_applied());
|
|
assert_equals(cell.alignment(), align);
|
|
}
|
|
|
|
void test_protection()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
assert(!cell.has_format());
|
|
|
|
auto protection = xlnt::protection().locked(false).hidden(true);
|
|
cell.protection(protection);
|
|
|
|
assert(cell.has_format());
|
|
assert(cell.format().protection_applied());
|
|
assert_equals(cell.protection(), protection);
|
|
|
|
assert(cell.has_format());
|
|
cell.clear_format();
|
|
assert(!cell.has_format());
|
|
}
|
|
|
|
void test_style()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
assert(!cell.has_style());
|
|
|
|
auto test_style = wb.create_style("test_style");
|
|
test_style.number_format(xlnt::number_format::date_ddmmyyyy(), true);
|
|
|
|
cell.style(test_style);
|
|
assert(cell.has_style());
|
|
assert_equals(cell.style().number_format(), xlnt::number_format::date_ddmmyyyy());
|
|
assert_equals(cell.style(), test_style);
|
|
|
|
auto other_style = wb.create_style("other_style");
|
|
other_style.number_format(xlnt::number_format::date_time2(), true);
|
|
|
|
cell.style("other_style");
|
|
assert_equals(cell.style().number_format(), xlnt::number_format::date_time2());
|
|
assert_equals(cell.style(), other_style);
|
|
|
|
auto last_style = wb.create_style("last_style");
|
|
last_style.number_format(xlnt::number_format::percentage(), true);
|
|
|
|
cell.style(last_style);
|
|
assert_equals(cell.style().number_format(), xlnt::number_format::percentage());
|
|
assert_equals(cell.style(), last_style);
|
|
|
|
assert_throws(cell.style("doesn't exist"), xlnt::key_not_found);
|
|
|
|
cell.clear_style();
|
|
|
|
assert(!cell.has_style());
|
|
assert_throws(cell.style(), xlnt::invalid_attribute);
|
|
}
|
|
|
|
void test_print()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
|
|
{
|
|
auto cell = ws.cell("A1");
|
|
|
|
std::stringstream ss;
|
|
ss << cell;
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
assert_equals(cell.to_string(), stream_string);
|
|
assert_equals(stream_string, "");
|
|
}
|
|
|
|
{
|
|
auto cell = ws.cell("A2");
|
|
|
|
cell.value(false);
|
|
|
|
std::stringstream ss;
|
|
ss << cell;
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
assert_equals(cell.to_string(), stream_string);
|
|
assert_equals(stream_string, "FALSE");
|
|
}
|
|
|
|
{
|
|
auto cell = ws.cell("A3");
|
|
|
|
cell.value(true);
|
|
|
|
std::stringstream ss;
|
|
ss << cell;
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
assert_equals(cell.to_string(), stream_string);
|
|
assert_equals(stream_string, "TRUE");
|
|
}
|
|
|
|
{
|
|
auto cell = ws.cell("A4");
|
|
|
|
cell.value(1.234);
|
|
|
|
std::stringstream ss;
|
|
ss << cell;
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
assert_equals(cell.to_string(), stream_string);
|
|
assert_equals(stream_string, "1.234");
|
|
}
|
|
|
|
{
|
|
auto cell = ws.cell("A5");
|
|
|
|
cell.error("#REF");
|
|
|
|
std::stringstream ss;
|
|
ss << cell;
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
assert_equals(cell.to_string(), stream_string);
|
|
assert_equals(stream_string, "#REF");
|
|
}
|
|
|
|
{
|
|
auto cell = ws.cell("A6");
|
|
|
|
cell.value("test");
|
|
|
|
std::stringstream ss;
|
|
ss << cell;
|
|
|
|
auto stream_string = ss.str();
|
|
|
|
assert_equals(cell.to_string(), stream_string);
|
|
assert_equals(stream_string, "test");
|
|
}
|
|
}
|
|
|
|
void test_values()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
|
|
cell.value(static_cast<int>(4));
|
|
assert_equals(cell.value<int>(), 4);
|
|
|
|
cell.value(static_cast<unsigned int>(3));
|
|
assert_equals(cell.value<unsigned>(), 3);
|
|
|
|
cell.value(static_cast<unsigned long long>(4));
|
|
assert_equals(cell.value<unsigned long long>(), 4);
|
|
|
|
cell.value(static_cast<long long int>(3));
|
|
assert_equals(cell.value<long long int>(), 3);
|
|
|
|
cell.value(static_cast<float>(3.14));
|
|
assert_delta(cell.value<float>(), 3.14, 0.001);
|
|
|
|
cell.value(static_cast<double>(4.1415));
|
|
assert_equals(cell.value<double>(), 4.1415);
|
|
|
|
cell.value(static_cast<long double>(3.141592));
|
|
assert_equals(cell.value<long double>(), 3.141592);
|
|
|
|
auto cell2 = ws.cell("A2");
|
|
cell2.value(std::string(100'000, 'a'));
|
|
cell.value(cell2);
|
|
assert_equals(cell.value<std::string>(), std::string(32'767, 'a'));
|
|
}
|
|
|
|
void test_reference()
|
|
{
|
|
xlnt::cell_reference_hash hash;
|
|
assert_differs(hash(xlnt::cell_reference("A2")), hash(xlnt::cell_reference(1, 1)));
|
|
assert_equals(hash(xlnt::cell_reference("A2")), hash(xlnt::cell_reference(1, 2)));
|
|
|
|
assert_equals((xlnt::cell_reference("A1"), xlnt::cell_reference("B2")), xlnt::range_reference("A1:B2"));
|
|
|
|
assert_throws(xlnt::cell_reference("A1&"), xlnt::invalid_cell_reference);
|
|
assert_throws(xlnt::cell_reference("A"), xlnt::invalid_cell_reference);
|
|
|
|
auto ref = xlnt::cell_reference("$B$7");
|
|
assert(ref.row_absolute());
|
|
assert(ref.column_absolute());
|
|
|
|
assert(xlnt::cell_reference("A1") == "A1");
|
|
assert(xlnt::cell_reference("A1") != "A2");
|
|
}
|
|
|
|
void test_anchor()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto cell = wb.active_sheet().cell("A1");
|
|
auto anchor = cell.anchor();
|
|
assert_equals(anchor.first, 0);
|
|
assert_equals(anchor.second, 0);
|
|
}
|
|
|
|
void test_hyperlink()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto cell = wb.active_sheet().cell("A1");
|
|
assert(!cell.has_hyperlink());
|
|
assert_throws(cell.hyperlink(), xlnt::invalid_attribute);
|
|
assert_throws(cell.hyperlink("notaurl"), xlnt::invalid_parameter);
|
|
assert_throws(cell.hyperlink(""), xlnt::invalid_parameter);
|
|
cell.hyperlink("http://example.com");
|
|
assert(cell.has_hyperlink());
|
|
assert_equals(cell.hyperlink(), "http://example.com");
|
|
}
|
|
|
|
void test_comment()
|
|
{
|
|
xlnt::workbook wb;
|
|
auto ws = wb.active_sheet();
|
|
auto cell = ws.cell("A1");
|
|
assert(!cell.has_comment());
|
|
assert_throws(cell.comment(), xlnt::exception);
|
|
cell.comment(xlnt::comment("comment", "author"));
|
|
assert(cell.has_comment());
|
|
assert_equals(cell.comment(), xlnt::comment("comment", "author"));
|
|
cell.clear_comment();
|
|
assert(!cell.has_comment());
|
|
assert_throws(cell.comment(), xlnt::exception);
|
|
}
|
|
};
|