xlnt/tests/test_cell.hpp

415 lines
11 KiB
C++
Raw Normal View History

2014-05-09 03:32:12 +08:00
#pragma once
#include <ctime>
#include <iostream>
#include <cxxtest/TestSuite.h>
2014-06-06 04:19:31 +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
{
public:
void test_infer_numeric()
{
wb.set_guess_types(true);
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
cell.set_value("4.2");
TS_ASSERT(cell.get_value() == 4.2);
cell.set_value("-42.000");
TS_ASSERT(cell.get_value() == -42);
cell.set_value("0");
TS_ASSERT(cell.get_value() == 0);
cell.set_value("0.9999");
TS_ASSERT(cell.get_value() == 0.9999);
cell.set_value("99E-02");
TS_ASSERT(cell.get_value() == 0.99);
cell.set_value("4");
TS_ASSERT(cell.get_value() == 4);
cell.set_value("-1E3");
TS_ASSERT(cell.get_value() == -1000);
cell.set_value("2e+2");
TS_ASSERT(cell.get_value() == 200);
cell.set_value("3.1%");
TS_ASSERT(cell.get_value() == 0.031);
cell.set_value("03:40:16");
TS_ASSERT(cell.get_value() == xlnt::time(3, 40, 16));
cell.set_value("03:40");
TS_ASSERT(cell.get_value() == xlnt::time(3, 40));
cell.set_value("30:33.865633336");
TS_ASSERT(cell.get_value() == xlnt::time(0, 30, 33, 865633));
}
2014-05-09 03:32:12 +08:00
void test_coordinates()
{
2014-05-21 22:20:30 +08:00
xlnt::cell_reference coord("ZF46");
TS_ASSERT_EQUALS("ZF", coord.get_column());
TS_ASSERT_EQUALS(46, coord.get_row());
2014-05-09 03:32:12 +08:00
}
void test_invalid_coordinate()
{
2014-06-11 06:36:31 +08:00
TS_ASSERT_THROWS(xlnt::cell_reference("AAA"),
xlnt::cell_coordinates_exception);
2014-05-09 03:32:12 +08:00
}
void test_zero_row()
{
2014-06-11 06:36:31 +08:00
TS_ASSERT_THROWS(xlnt::cell_reference("AQ0"),
xlnt::cell_coordinates_exception);
2014-05-09 03:32:12 +08:00
}
void test_absolute()
{
2014-05-21 22:20:30 +08:00
TS_ASSERT_EQUALS("$ZF$51", xlnt::cell_reference::make_absolute("ZF51").to_string());
2014-05-09 03:32:12 +08:00
}
void test_absolute_multiple()
{
2014-05-21 22:20:30 +08:00
TS_ASSERT_EQUALS("$ZF$51:$ZF$53", xlnt::range_reference::make_absolute("ZF51:ZF$53").to_string());
2014-05-09 03:32:12 +08:00
}
void test_column_index()
{
static const std::unordered_map<int, std::string> expected =
{
{1, "A"},
2014-05-31 06:42:25 +08:00
{10, "J"},
{26, "Z"},
{27, "AA"},
{52, "AZ"},
{53, "BA"},
{78, "BZ"},
2014-05-31 06:42:25 +08:00
{270, "jJ"},
{677, "ZA"},
{702, "ZZ"},
{703, "AAA"},
{728, "AAZ"},
{731, "ABC"},
{1353, "AZA"},
2014-05-31 06:42:25 +08:00
{7030, "jjj"},
{18253, "ZZA"},
{18278, "ZZZ"}
};
for(auto expected_pair : expected)
{
2014-05-31 06:42:25 +08:00
TS_ASSERT_EQUALS(expected_pair.first,
xlnt::cell_reference::column_index_from_string(expected_pair.second));
}
2014-05-09 03:32:12 +08:00
}
void test_bad_column_index()
{
2014-07-20 02:43:48 +08:00
for(auto bad_string : {"JJJJ", "", "$", "1"})
2014-05-09 03:32:12 +08:00
{
2014-06-11 06:36:31 +08:00
TS_ASSERT_THROWS(xlnt::cell_reference::column_index_from_string(bad_string), xlnt::column_string_index_exception);
2014-05-09 03:32:12 +08:00
}
}
2014-07-20 02:43:48 +08:00
void test_column_letter_boundaries()
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
TS_ASSERT_THROWS(xlnt::cell_reference::column_string_from_index(0),
xlnt::column_string_index_exception);
TS_ASSERT_THROWS(xlnt::cell_reference::column_string_from_index(18279),
xlnt::column_string_index_exception);
2014-05-09 03:32:12 +08:00
}
void test_column_letter()
{
2014-05-21 22:20:30 +08:00
TS_ASSERT_EQUALS("ZZZ", xlnt::cell_reference::column_string_from_index(18278));
TS_ASSERT_EQUALS("JJJ", xlnt::cell_reference::column_string_from_index(7030));
TS_ASSERT_EQUALS("AB", xlnt::cell_reference::column_string_from_index(28));
TS_ASSERT_EQUALS("AA", xlnt::cell_reference::column_string_from_index(27));
TS_ASSERT_EQUALS("Z", xlnt::cell_reference::column_string_from_index(26));
2014-05-09 03:32:12 +08:00
}
void test_initial_value()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1", xlnt::value("17.5"));
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
2014-05-09 03:32:12 +08:00
}
2014-06-11 06:36:31 +08:00
void test_1st()
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT(cell.get_value().is(xlnt::value::type::null));
2014-05-09 03:32:12 +08:00
}
2014-06-11 06:36:31 +08:00
void test_null()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1", xlnt::value("17.5"));
2014-07-26 04:39:25 +08:00
cell.set_value(xlnt::value::null());
2014-06-11 06:36:31 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT(cell.get_value().is(xlnt::value::type::null));
2014-06-11 06:36:31 +08:00
}
2014-05-09 03:32:12 +08:00
void test_string()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-11 22:46:43 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value("hello");
TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
2014-05-09 03:32:12 +08:00
}
void test_single_dot()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value(".");
TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
2014-05-09 03:32:12 +08:00
}
void test_formula()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-25 05:31:46 +08:00
cell.set_formula("42");
TS_ASSERT(cell.has_formula());
cell.clear_formula();
TS_ASSERT(!cell.has_formula());
cell.set_formula("if(A1<4;-1;1)");
TS_ASSERT(cell.has_formula());
2014-05-09 03:32:12 +08:00
}
void test_boolean()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value(true);
TS_ASSERT(cell.get_value().is(xlnt::value::type::boolean));
cell.set_value(false);
TS_ASSERT(cell.get_value().is(xlnt::value::type::boolean));
2014-05-09 03:32:12 +08:00
}
2014-05-13 01:42:28 +08:00
void test_error_codes()
2014-05-09 03:32:12 +08:00
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
for(auto error : xlnt::cell::ErrorCodes)
{
2014-07-26 04:39:25 +08:00
cell.set_error(error.first);
TS_ASSERT(cell.get_value().is(xlnt::value::type::error));
2014-05-13 01:42:28 +08:00
}
2014-05-09 03:32:12 +08:00
}
2014-06-11 06:36:31 +08:00
void test_insert_float()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value(3.14);
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
2014-06-11 06:36:31 +08:00
}
void test_insert_percentage()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value("3.14%");
TS_ASSERT_DELTA(0.0314, cell.get_value().as<double>(), 1e-7);
2014-06-11 06:36:31 +08:00
}
void test_insert_datetime()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value(xlnt::date::today());
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
2014-06-11 06:36:31 +08:00
}
void test_insert_date()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value(xlnt::datetime::now());
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
2014-06-11 06:36:31 +08:00
}
void test_internal_date()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
xlnt::datetime dt(2010, 7, 13, 6, 37, 41);
2014-07-26 04:39:25 +08:00
cell.set_value(dt);
TS_ASSERT_EQUALS(40372.27616898148, cell.get_value().as<double>());
2014-06-11 06:36:31 +08:00
}
void test_datetime_interpretation()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
xlnt::datetime dt(2010, 7, 13, 6, 37, 41);
2014-07-26 04:39:25 +08:00
cell.set_value(dt);
TS_ASSERT_EQUALS(cell.get_value(), dt);
TS_ASSERT_DELTA(cell.get_value().as<double>(), 40372.27616898148, 1e-7);
2014-06-11 06:36:31 +08:00
}
void test_date_interpretation()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
xlnt::date dt(2010, 7, 13);
2014-07-26 04:39:25 +08:00
cell.set_value(dt);
TS_ASSERT_EQUALS(cell.get_value(), dt);
TS_ASSERT_EQUALS(cell.get_value().as<int>(), 40372);
2014-06-11 06:36:31 +08:00
}
void test_number_format_style()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value("12.6%");
2014-06-11 06:36:31 +08:00
TS_ASSERT_EQUALS(xlnt::number_format::format::percentage, cell.get_style().get_number_format().get_format_code());
}
2014-05-09 03:32:12 +08:00
void test_data_type_check()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
TS_ASSERT(cell.get_value().is(xlnt::value::type::null));
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value(".0e000");
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value("-0.e-0");
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value("1E");
TS_ASSERT(cell.get_value().is(xlnt::value::type::string));
2014-05-09 03:32:12 +08:00
}
void test_set_bad_type()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-31 06:42:25 +08:00
TS_ASSERT_THROWS(cell.set_error("1"), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_hyperlink("1"), xlnt::data_type_exception);
2014-05-09 03:32:12 +08:00
}
void test_time()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-13 01:42:28 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value("03:40:16");
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
TS_ASSERT_EQUALS(cell.get_value(), xlnt::time(3, 40, 16));
2014-05-13 01:42:28 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value("03:40");
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
TS_ASSERT_EQUALS(cell.get_value(), xlnt::time(3, 40));
2014-05-09 03:32:12 +08:00
}
2014-07-20 02:43:48 +08:00
void test_timedelta()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-07-26 04:39:25 +08:00
cell.set_value(xlnt::timedelta(1, 3, 0, 0, 0));
TS_ASSERT_EQUALS(cell.get_value(), 1.125);
TS_ASSERT(cell.get_value().is(xlnt::value::type::numeric));
2014-07-20 02:43:48 +08:00
}
2014-05-09 03:32:12 +08:00
void test_date_format_on_non_date()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-11 22:46:43 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value(xlnt::datetime::now());
cell.set_value("testme");
TS_ASSERT("testme" == cell.get_value());
2014-05-09 03:32:12 +08:00
}
void test_set_get_date()
{
2014-05-31 06:42:25 +08:00
xlnt::datetime today(2010, 1, 18, 14,15, 20 ,1600);
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-11 22:46:43 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value(today);
TS_ASSERT(today == cell.get_value());
2014-05-09 03:32:12 +08:00
}
void test_repr()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-11 22:46:43 +08:00
2014-05-31 06:42:25 +08:00
TS_ASSERT_EQUALS(cell.to_string(), "<Cell " + ws.get_title() + ".A1>");
2014-05-09 03:32:12 +08:00
}
void test_is_date()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value(xlnt::datetime::now());
2014-05-31 06:42:25 +08:00
TS_ASSERT(cell.is_date());
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value("testme");
TS_ASSERT_EQUALS("testme", cell.get_value());
2014-05-31 06:42:25 +08:00
TS_ASSERT(!cell.is_date());
2014-05-09 03:32:12 +08:00
}
void test_is_not_date_color_format()
{
2014-05-31 06:42:25 +08:00
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
2014-05-09 03:32:12 +08:00
2014-07-26 04:39:25 +08:00
cell.set_value(-13.5);
2014-05-31 06:42:25 +08:00
cell.get_style().get_number_format().set_format_code("0.00_);[Red]\\(0.00\\)");
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
TS_ASSERT(!cell.is_date());
2014-05-09 03:32:12 +08:00
}
2014-07-20 02:43:48 +08:00
void test_comment_count()
{
xlnt::worksheet ws = wb.create_sheet();
xlnt::cell cell(ws, "A1");
TS_ASSERT(ws.get_comment_count() == 0);
cell.set_comment(xlnt::comment("text", "author"));
TS_ASSERT(ws.get_comment_count() == 1);
cell.set_comment(xlnt::comment("text", "author"));
TS_ASSERT(ws.get_comment_count() == 1);
cell.clear_comment();
TS_ASSERT(ws.get_comment_count() == 0);
cell.clear_comment();
TS_ASSERT(ws.get_comment_count() == 0);
}
void test_cell_offset()
{
xlnt::worksheet ws = wb.create_sheet();
TS_ASSERT(ws.get_cell("B15").offset(1, 2).get_reference() == "C17");
}
2014-05-31 06:42:25 +08:00
private:
xlnt::workbook wb;
2014-05-09 03:32:12 +08:00
};