work on cell.cpp test coverage

This commit is contained in:
Thomas Fussell 2016-06-19 19:43:41 +01:00
parent d194fe9640
commit 27cb3a089d
6 changed files with 225 additions and 24 deletions

View File

@ -212,6 +212,8 @@ public:
bool has_style(const std::string &name) const; bool has_style(const std::string &name) const;
style &get_style(const std::string &name); style &get_style(const std::string &name);
const style &get_style(const std::string &name) const; const style &get_style(const std::string &name) const;
style &get_style_by_id(std::size_t style_id);
const style &get_style_by_id(std::size_t style_id) const;
std::size_t get_style_id(const std::string &name) const; std::size_t get_style_id(const std::string &name) const;
style &create_style(const std::string &name); style &create_style(const std::string &name);
std::size_t add_style(const style &new_style); std::size_t add_style(const style &new_style);

View File

@ -399,7 +399,7 @@ XLNT_FUNCTION void cell::set_value(cell c)
d_->has_hyperlink_ = c.d_->has_hyperlink_; d_->has_hyperlink_ = c.d_->has_hyperlink_;
d_->formula_ = c.d_->formula_; d_->formula_ = c.d_->formula_;
d_->format_id_ = c.d_->format_id_; d_->format_id_ = c.d_->format_id_;
set_comment(c.get_comment()); if (c.has_comment()) set_comment(c.get_comment());
} }
template <> template <>
@ -1084,4 +1084,19 @@ void cell::set_style(const std::string &style_name)
d_->style_id_ = get_workbook().get_style_id(style_name); d_->style_id_ = get_workbook().get_style_id(style_name);
} }
const style &cell::get_style() const
{
if (!d_->has_style_)
{
throw std::runtime_error("cell has no style");
}
return get_workbook().get_style_by_id(d_->style_id_);
}
bool cell::has_style() const
{
return d_->has_style_;
}
} // namespace xlnt } // namespace xlnt

View File

@ -2,27 +2,10 @@
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
#include <sstream>
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
#include <xlnt/cell/cell.hpp> #include <xlnt/xlnt.hpp>
#include <xlnt/cell/cell_reference.hpp>
#include <xlnt/cell/comment.hpp>
#include <xlnt/serialization/encoding.hpp>
#include <xlnt/styles/alignment.hpp>
#include <xlnt/styles/border.hpp>
#include <xlnt/styles/font.hpp>
#include <xlnt/styles/fill.hpp>
#include <xlnt/styles/format.hpp>
#include <xlnt/styles/number_format.hpp>
#include <xlnt/styles/protection.hpp>
#include <xlnt/utils/date.hpp>
#include <xlnt/utils/datetime.hpp>
#include <xlnt/utils/time.hpp>
#include <xlnt/utils/timedelta.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <xlnt/worksheet/range.hpp>
#include <xlnt/worksheet/worksheet.hpp>
#include <xlnt/workbook/workbook.hpp>
class test_cell : public CxxTest::TestSuite class test_cell : public CxxTest::TestSuite
{ {
@ -434,7 +417,9 @@ public:
{ {
auto ws = wb.create_sheet(); auto ws = wb.create_sheet();
auto cell = ws.get_cell("A1"); auto cell = ws.get_cell("A1");
TS_ASSERT(!cell.has_format());
xlnt::protection prot; xlnt::protection prot;
prot.set_locked(xlnt::protection::type::protected_); prot.set_locked(xlnt::protection::type::protected_);
@ -443,5 +428,181 @@ public:
TS_ASSERT(cell.has_format()); TS_ASSERT(cell.has_format());
TS_ASSERT(cell.get_format().protection_applied()); TS_ASSERT(cell.get_format().protection_applied());
TS_ASSERT_EQUALS(cell.get_protection(), prot); TS_ASSERT_EQUALS(cell.get_protection(), prot);
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<unsigned long long>(4));
TS_ASSERT_EQUALS(cell.get_value<unsigned long long>(), 4);
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");
} }
}; };

View File

@ -47,6 +47,8 @@ cell_impl::cell_impl(worksheet_impl *parent, column_t column, row_t row)
is_merged_(false), is_merged_(false),
has_format_(false), has_format_(false),
format_id_(0), format_id_(0),
has_style_(false),
style_id_(0),
comment_(nullptr) comment_(nullptr)
{ {
} }

View File

@ -805,15 +805,26 @@ std::string format_section(long double number, const section &format, xlnt::cale
end_text = quoted_text.substr(1, quoted_text.size() - 2); end_text = quoted_text.substr(1, quoted_text.size() - 2);
} }
} }
result = before_text;
if (number == static_cast<long long int>(number)) if (number == static_cast<long long int>(number))
{ {
result = before_text + std::to_string(static_cast<long long int>(number)) + end_text; result.append(std::to_string(static_cast<long long int>(number)));
} }
else else
{ {
result = before_text + std::to_string(number) + end_text; auto number_string = std::to_string(number);
while (number_string.find('.') != std::string::npos && number_string.back() == '0' && number_string.find('.') < number_string.size() - 1)
{
number_string.erase(number_string.end() - 1);
}
result.append(number_string);
} }
result.append(end_text);
} }
else if (format.value.substr(0, 8) == "#,##0.00" || format.value.substr(0, 9) == "-#,##0.00") else if (format.value.substr(0, 8) == "#,##0.00" || format.value.substr(0, 9) == "-#,##0.00")
{ {

View File

@ -779,6 +779,16 @@ const style &workbook::get_style(const std::string &name) const
[&name](const style &s) { return s.get_name() == name; }); [&name](const style &s) { return s.get_name() == name; });
} }
style &workbook::get_style_by_id(std::size_t style_id)
{
return d_->stylesheet_.styles.at(style_id);
}
const style &workbook::get_style_by_id(std::size_t style_id) const
{
return d_->stylesheet_.styles.at(style_id);
}
std::string workbook::next_relationship_id() const std::string workbook::next_relationship_id() const
{ {
std::size_t i = 1; std::size_t i = 1;