xlnt/tests/test_cell.hpp

316 lines
8.6 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_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-06 05:42:15 +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-05-31 06:42:25 +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-05-11 23:58:53 +08:00
for(auto bad_string : {"JJJJ", "", "$", "1"})
2014-05-09 03:32:12 +08:00
{
2014-06-06 05:42:15 +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
}
}
void test_column_letter_boundries()
{
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", "17.5");
2014-05-09 03:32:12 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
}
void test_null()
{
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
TS_ASSERT_EQUALS(xlnt::cell::type::null, cell.get_data_type());
}
void test_numeric()
{
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
cell = 42;
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "4.2";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "-42.000";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "0";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = 0;
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = 0.0001;
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "0.9999";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "99E-02";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = 1e1;
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "4";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = "-1E3";
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-31 06:42:25 +08:00
2014-05-09 03:32:12 +08:00
cell = 4;
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
}
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-05-09 03:32:12 +08:00
cell = "hello";
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type());
}
void test_single_dot()
{
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
cell = ".";
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type());
}
void test_formula()
{
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
cell = "=42";
TS_ASSERT_EQUALS(xlnt::cell::type::formula, cell.get_data_type());
}
void test_boolean()
{
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
cell = true;
TS_ASSERT_EQUALS(xlnt::cell::type::boolean, cell.get_data_type());
cell = false;
TS_ASSERT_EQUALS(xlnt::cell::type::boolean, cell.get_data_type());
}
void test_leading_zero()
{
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
cell = "0800";
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type());
}
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)
{
cell = error.first;
TS_ASSERT_EQUALS(xlnt::cell::type::error, cell.get_data_type());
}
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-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::null, cell.get_data_type());
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
cell = ".0e000";
2014-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
cell = "-0.e-0";
2014-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-05-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
cell = "1E";
2014-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::string, cell.get_data_type());
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-09 03:32:12 +08:00
2014-05-31 06:42:25 +08:00
TS_ASSERT_THROWS(cell.set_explicit_value("1", xlnt::cell::type::formula),
xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_explicit_value(1, xlnt::cell::type::formula),
xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_explicit_value(1.0, xlnt::cell::type::formula),
xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_formula("1"), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_error("1"), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_hyperlink("1"), xlnt::data_type_exception);
TS_ASSERT_THROWS(cell.set_formula("#REF!"), 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
cell = "03:40:16";
2014-05-31 06:42:25 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-06-06 05:42:15 +08:00
TS_ASSERT_EQUALS(cell, xlnt::time(3, 40, 16));
2014-05-13 01:42:28 +08:00
cell = "03:40";
2014-05-31 06:42:25 +08:00
TS_ASSERT_EQUALS(xlnt::cell::type::numeric, cell.get_data_type());
2014-06-06 05:42:15 +08:00
TS_ASSERT_EQUALS(cell, xlnt::time(3, 40));
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-05-31 06:42:25 +08:00
cell = xlnt::datetime::now();
2014-05-09 03:32:12 +08:00
cell = "testme";
TS_ASSERT("testme" == cell);
}
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-05-09 03:32:12 +08:00
cell = today;
TS_ASSERT(today == cell);
}
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-05-31 06:42:25 +08:00
cell = xlnt::datetime::now();
TS_ASSERT(cell.is_date());
2014-05-09 03:32:12 +08:00
cell = "testme";
2014-05-11 22:46:43 +08:00
TS_ASSERT_EQUALS("testme", cell);
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-05-13 01:42:28 +08:00
cell = -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-05-31 06:42:25 +08:00
private:
xlnt::workbook wb;
2014-05-09 03:32:12 +08:00
};