xlnt/source/cell/tests/test_cell.hpp

626 lines
19 KiB
C++
Raw Normal View History

2014-05-09 03:32:12 +08:00
#pragma once
#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
2014-06-06 04:19:31 +08:00
class test_cell : public CxxTest::TestSuite
2014-05-09 03:32:12 +08:00
{
private:
xlnt::workbook wb, wb_guess_types;
2014-05-09 03:32:12 +08:00
public:
test_cell()
{
wb_guess_types.set_guess_types(true);
}
2015-10-17 06:35:11 +08:00
void test_infer_numeric()
{
auto ws = wb_guess_types.create_sheet();
auto cell = ws.get_cell("A1");
cell.set_value("4.2");
TS_ASSERT(cell.get_value<long double>() == 4.2L);
cell.set_value("-42.000");
TS_ASSERT(cell.get_value<int>() == -42);
cell.set_value("0");
TS_ASSERT(cell.get_value<int>() == 0);
cell.set_value("0.9999");
TS_ASSERT(cell.get_value<long double>() == 0.9999L);
cell.set_value("99E-02");
TS_ASSERT(cell.get_value<long double>() == 0.99L);
cell.set_value("4");
TS_ASSERT(cell.get_value<int>() == 4);
cell.set_value("-1E3");
TS_ASSERT(cell.get_value<int>() == -1000);
cell.set_value("2e+2");
TS_ASSERT(cell.get_value<int>() == 200);
cell.set_value("3.1%");
TS_ASSERT(cell.get_value<long double>() == 0.031L);
cell.set_value("03:40:16");
TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(3, 40, 16));
cell.set_value("03:");
TS_ASSERT_EQUALS(cell.get_value<std::string>(), "03:");
cell.set_value("03:40");
TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(3, 40));
cell.set_value("30:33.865633336");
TS_ASSERT(cell.get_value<xlnt::time>() == xlnt::time(0, 30, 33, 865633));
}
void test_ctor()
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference("A", 1));
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::null);
TS_ASSERT(cell.get_column() == "A");
TS_ASSERT(cell.get_row() == 1);
TS_ASSERT(cell.get_reference() == "A1");
TS_ASSERT(!cell.has_value());
TS_ASSERT(!cell.has_comment());
}
void test_null()
2014-05-09 03:32:12 +08:00
{
2016-03-14 11:46:01 +08:00
const auto datatypes =
{
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
};
2016-03-14 11:46:01 +08:00
for(const auto &datatype : datatypes)
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_data_type(datatype);
TS_ASSERT(cell.get_data_type() == datatype);
cell.clear_value();
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::null);
}
2014-05-09 03:32:12 +08:00
}
void test_string()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value("hello");
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::string);
cell.set_value(".");
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::string);
cell.set_value("0800");
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::string);
2014-05-09 03:32:12 +08:00
}
void test_formula1()
2014-05-09 03:32:12 +08:00
{
auto ws = wb_guess_types.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value("=42");
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::formula);
2014-05-09 03:32:12 +08:00
}
void test_formula2()
2014-05-09 03:32:12 +08:00
{
2016-03-14 11:46:01 +08:00
auto ws = wb_guess_types.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value("=if(A1<4;-1;1)");
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::formula);
2014-05-09 03:32:12 +08:00
}
void test_not_formula()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value("=");
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::string);
TS_ASSERT(cell.get_value<std::string>() == "=");
TS_ASSERT(!cell.has_formula());
2014-05-09 03:32:12 +08:00
}
void test_boolean()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
for(auto value : {true, false})
{
cell.set_value(value);
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::boolean);
}
2014-05-09 03:32:12 +08:00
}
void test_error_codes()
2014-05-09 03:32:12 +08:00
{
auto ws = wb_guess_types.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
2015-10-30 07:37:07 +08:00
for(auto error_code : xlnt::cell::error_codes())
2014-05-09 03:32:12 +08:00
{
cell.set_value(error_code.first);
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::error);
2014-05-09 03:32:12 +08:00
}
}
void test_insert_datetime()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value(xlnt::datetime(2010, 7, 13, 6, 37, 41));
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::numeric);
TS_ASSERT(cell.get_value<long double>() == 40372.27616898148L);
TS_ASSERT(cell.is_date());
2015-10-19 03:30:46 +08:00
TS_ASSERT(cell.get_number_format().get_format_string() == "yyyy-mm-dd h:mm:ss");
2014-05-09 03:32:12 +08:00
}
void test_insert_date()
2014-06-11 06:36:31 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value(xlnt::date(2010, 7, 13));
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::numeric);
TS_ASSERT(cell.get_value<long double>() == 40372.L);
TS_ASSERT(cell.is_date());
2015-10-19 03:30:46 +08:00
TS_ASSERT(cell.get_number_format().get_format_string() == "yyyy-mm-dd");
2014-06-11 06:36:31 +08:00
}
void test_insert_time()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value(xlnt::time(1, 3));
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::numeric);
TS_ASSERT(cell.get_value<long double>() == 0.04375L);
TS_ASSERT(cell.is_date());
2015-10-19 03:30:46 +08:00
TS_ASSERT(cell.get_number_format().get_format_string() == "h:mm:ss");
2014-05-09 03:32:12 +08:00
}
void test_cell_formatted_as_date1()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value(xlnt::datetime::today());
cell.clear_value();
2015-10-17 06:35:11 +08:00
TS_ASSERT(!cell.is_date()); // disagree with openpyxl
TS_ASSERT(!cell.has_value());
2014-05-09 03:32:12 +08:00
}
void test_cell_formatted_as_date2()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value(xlnt::datetime::today());
cell.set_value("testme");
TS_ASSERT(!cell.is_date());
TS_ASSERT(cell.get_value<std::string>() == "testme");
2014-05-09 03:32:12 +08:00
}
void test_cell_formatted_as_date3()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
cell.set_value(xlnt::datetime::today());
2014-07-26 04:39:25 +08:00
cell.set_value(true);
TS_ASSERT(!cell.is_date());
TS_ASSERT(cell.get_value<bool>() == true);
2014-05-09 03:32:12 +08:00
}
2016-03-14 11:46:01 +08:00
void test_illegal_characters()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_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)
2014-05-13 01:42:28 +08:00
{
std::string str(1, i);
TS_ASSERT_THROWS(cell.set_value(str), xlnt::illegal_character_error);
2014-05-13 01:42:28 +08:00
}
cell.set_value(std::string(1, 33));
cell.set_value(std::string(1, 9)); // Tab
cell.set_value(std::string(1, 10)); // Newline
cell.set_value(std::string(1, 13)); // Carriage return
cell.set_value(" Leading and trailing spaces are legal ");
}
2016-03-14 11:46:01 +08:00
// void test_time_regex() {}
void test_timedelta()
2014-06-11 06:36:31 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
2015-11-23 01:41:27 +08:00
cell.set_value(xlnt::timedelta(1, 3, 0, 0, 0));
TS_ASSERT(cell.get_value<long double>() == 1.125);
TS_ASSERT(cell.get_data_type() == xlnt::cell::type::numeric);
TS_ASSERT(!cell.is_date());
2015-10-19 03:30:46 +08:00
TS_ASSERT(cell.get_number_format().get_format_string() == "[hh]:mm:ss");
2014-06-11 06:36:31 +08:00
}
void test_repr()
2014-06-11 06:36:31 +08:00
{
auto ws = wb[1];
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
2015-10-17 06:35:11 +08:00
TS_ASSERT(cell.to_repr() == "<Cell Sheet1.A1>");
2014-06-11 06:36:31 +08:00
}
void test_comment_assignment()
2014-06-11 06:36:31 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
TS_ASSERT(!cell.has_comment());
xlnt::comment comm(cell, "text", "author");
TS_ASSERT(cell.get_comment() == comm);
2014-06-11 06:36:31 +08:00
}
void test_only_one_cell_per_comment()
2014-06-11 06:36:31 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
xlnt::comment comm(cell, "text", "author");
auto c2 = ws.get_cell(xlnt::cell_reference(1, 2));
TS_ASSERT_THROWS(c2.set_comment(comm), xlnt::attribute_error);
2014-06-11 06:36:31 +08:00
}
void test_remove_comment()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
xlnt::comment comm(cell, "text", "author");
cell.clear_comment();
TS_ASSERT(!cell.has_comment());
2014-05-09 03:32:12 +08:00
}
void test_cell_offset()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell(xlnt::cell_reference(1, 1));
2015-11-23 01:41:27 +08:00
TS_ASSERT(cell.offset(1, 2).get_reference() == "B3");
}
2015-11-23 01:41:27 +08:00
void test_font()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
xlnt::font font;
font.set_bold(true);
2015-11-23 01:41:27 +08:00
cell.set_font(font);
2016-05-15 01:57:07 +08:00
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().font_applied());
TS_ASSERT_EQUALS(cell.get_font(), font);
2014-05-09 03:32:12 +08:00
}
2016-03-14 11:46:01 +08:00
void test_fill()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
xlnt::fill f = xlnt::fill::pattern(xlnt::pattern_fill::type::solid);
f.get_pattern_fill().set_foreground_color(xlnt::color::red());
2016-03-14 11:46:01 +08:00
cell.set_fill(f);
2016-05-15 01:57:07 +08:00
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().fill_applied());
TS_ASSERT_EQUALS(cell.get_fill(), f);
2014-05-09 03:32:12 +08:00
}
2016-03-14 11:46:01 +08:00
void test_border()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
xlnt::border border;
2016-03-14 11:46:01 +08:00
cell.set_border(border);
2016-05-15 01:57:07 +08:00
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().border_applied());
TS_ASSERT_EQUALS(cell.get_border(), border);
2014-05-09 03:32:12 +08:00
}
2016-03-14 11:46:01 +08:00
void test_number_format()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
xlnt::number_format format("dd--hh--mm");
cell.set_number_format(format);
2016-05-15 01:57:07 +08:00
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().number_format_applied());
TS_ASSERT_EQUALS(cell.get_number_format().get_format_string(), "dd--hh--mm");
2014-05-09 03:32:12 +08:00
}
2016-03-14 11:46:01 +08:00
void test_alignment()
2014-05-09 03:32:12 +08:00
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
xlnt::alignment align;
align.set_wrap_text(true);
2016-03-14 11:46:01 +08:00
cell.set_alignment(align);
2016-05-15 01:57:07 +08:00
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().alignment_applied());
TS_ASSERT_EQUALS(cell.get_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
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
2016-06-20 02:43:41 +08:00
TS_ASSERT(!cell.has_format());
cell.set_protection(xlnt::protection(false, true));
2016-03-14 11:46:01 +08:00
2016-05-15 01:57:07 +08:00
TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().protection_applied());
TS_ASSERT_EQUALS(cell.get_protection(), xlnt::protection(false, true));
2016-06-20 02:43:41 +08:00
TS_ASSERT(cell.has_format());
cell.clear_format();
TS_ASSERT(!cell.has_format());
}
void test_style()
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
TS_ASSERT(!cell.has_style());
auto &test_style = wb.create_style("test_style");
test_style.set_number_format(xlnt::number_format::date_ddmmyyyy());
cell.set_style(test_style);
TS_ASSERT(cell.has_style());
TS_ASSERT_EQUALS(cell.get_style().get_number_format(), xlnt::number_format::date_ddmmyyyy());
TS_ASSERT_EQUALS(cell.get_style(), test_style);
auto &other_style = wb.create_style("other_style");
other_style.set_number_format(xlnt::number_format::date_time2());
cell.set_style("other_style");
TS_ASSERT_EQUALS(cell.get_style().get_number_format(), xlnt::number_format::date_time2());
TS_ASSERT_EQUALS(cell.get_style(), other_style);
xlnt::style last_style;
last_style.set_number_format(xlnt::number_format::percentage());
cell.set_style(last_style);
TS_ASSERT_EQUALS(cell.get_style().get_number_format(), xlnt::number_format::percentage());
TS_ASSERT_EQUALS(cell.get_style(), last_style);
TS_ASSERT_THROWS(cell.set_style("doesn't exist"), std::runtime_error);
cell.clear_style();
TS_ASSERT(!cell.has_style());
TS_ASSERT_THROWS(cell.get_style(), std::runtime_error);
}
void test_print()
{
auto ws = wb.create_sheet();
{
auto cell = ws.get_cell("A1");
std::stringstream ss;
ss << cell;
auto stream_string = ss.str();
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
TS_ASSERT_EQUALS(stream_string, "");
}
{
auto cell = ws.get_cell("A2");
cell.set_value(false);
std::stringstream ss;
ss << cell;
auto stream_string = ss.str();
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
TS_ASSERT_EQUALS(stream_string, "FALSE");
}
{
auto cell = ws.get_cell("A3");
cell.set_value(true);
std::stringstream ss;
ss << cell;
auto stream_string = ss.str();
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
TS_ASSERT_EQUALS(stream_string, "TRUE");
}
{
auto cell = ws.get_cell("A4");
cell.set_value(1.234);
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");
}
{
auto cell = ws.get_cell("A5");
cell.set_error("#REF");
std::stringstream ss;
ss << cell;
auto stream_string = ss.str();
TS_ASSERT_EQUALS(cell.to_string(), stream_string);
TS_ASSERT_EQUALS(stream_string, "#REF");
}
{
auto cell = ws.get_cell("A6");
cell.set_value("test");
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()
{
auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1");
cell.set_value(static_cast<std::int8_t>(4));
TS_ASSERT_EQUALS(cell.get_value<int8_t>(), 4);
cell.set_value(static_cast<std::uint8_t>(3));
TS_ASSERT_EQUALS(cell.get_value<std::uint8_t>(), 3);
cell.set_value(static_cast<std::int16_t>(4));
TS_ASSERT_EQUALS(cell.get_value<int16_t>(), 4);
cell.set_value(static_cast<std::uint16_t>(3));
TS_ASSERT_EQUALS(cell.get_value<std::uint16_t>(), 3);
cell.set_value(static_cast<std::int32_t>(4));
TS_ASSERT_EQUALS(cell.get_value<int32_t>(), 4);
cell.set_value(static_cast<std::uint32_t>(3));
TS_ASSERT_EQUALS(cell.get_value<std::uint32_t>(), 3);
cell.set_value(static_cast<std::int64_t>(4));
TS_ASSERT_EQUALS(cell.get_value<int64_t>(), 4);
cell.set_value(static_cast<std::uint64_t>(3));
TS_ASSERT_EQUALS(cell.get_value<std::uint64_t>(), 3);
cell.set_value(static_cast<float>(3.14));
TS_ASSERT_DELTA(cell.get_value<float>(), 3.14, 0.001);
cell.set_value(static_cast<double>(4.1415));
TS_ASSERT_EQUALS(cell.get_value<double>(), 4.1415);
cell.set_value(static_cast<long double>(3.141592));
TS_ASSERT_EQUALS(cell.get_value<long double>(), 3.141592);
auto cell2 = ws.get_cell("A2");
cell2.set_value("test");
cell.set_value(cell2);
TS_ASSERT_EQUALS(cell.get_value<std::string>(), "test");
2014-07-20 02:43:48 +08:00
}
void test_operators()
{
xlnt::cell cell;
TS_ASSERT_EQUALS(cell, nullptr);
}
2016-07-19 09:26:14 +08:00
void test_comment()
{
xlnt::workbook wb;
auto cell_a1 = wb.get_active_sheet().get_cell("A1");
xlnt::comment comment(cell_a1, "text", "author");
auto cell_a2 = wb.get_active_sheet().get_cell("A2");
cell_a2 = cell_a1;
TS_ASSERT(cell_a2.has_comment());
TS_ASSERT_EQUALS(cell_a2.get_comment().get_text(), "text");
TS_ASSERT_EQUALS(cell_a2.get_comment().get_author(), "author");
xlnt::comment null;
TS_ASSERT_DIFFERS(null, comment);
2016-07-19 09:26:14 +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_THROWS(xlnt::cell_reference(10000000, 10000000), xlnt::cell_coordinates_exception);
TS_ASSERT_EQUALS((xlnt::cell_reference("A1"), xlnt::cell_reference("B2")), xlnt::range_reference("A1:B2"));
TS_ASSERT_THROWS(xlnt::cell_reference("A1A"), xlnt::cell_coordinates_exception);
TS_ASSERT_THROWS(xlnt::cell_reference("A"), xlnt::cell_coordinates_exception);
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");
TS_ASSERT(xlnt::cell_reference("A1") < xlnt::cell_reference("A2"));
TS_ASSERT(xlnt::cell_reference("A2") <= xlnt::cell_reference("A2"));
TS_ASSERT(xlnt::cell_reference("A2") > xlnt::cell_reference("A1"));
TS_ASSERT(xlnt::cell_reference("A2") >= xlnt::cell_reference("A2"));
}
2014-05-09 03:32:12 +08:00
};