diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index 8b85625b..9ff5fb93 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -608,9 +608,9 @@ public: bool operator==(const cell &comparand) const; /// - /// Returns true if this cell is uninitialized. + /// Returns false if this cell the same cell as comparand (compared by reference). /// - bool operator==(std::nullptr_t) const; + bool operator!=(const cell &comparand) const; private: friend class style; @@ -646,6 +646,11 @@ private: /// XLNT_API bool operator==(std::nullptr_t, const cell &cell); +/// +/// Returns true if this cell is uninitialized. +/// +XLNT_API bool operator==(const cell &cell, std::nullptr_t); + /// /// Convenience function for writing cell to an ostream. /// Uses cell::to_string() internally. diff --git a/include/xlnt/cell/rich_text_run.hpp b/include/xlnt/cell/rich_text_run.hpp index 0988a7e7..dc31fe52 100644 --- a/include/xlnt/cell/rich_text_run.hpp +++ b/include/xlnt/cell/rich_text_run.hpp @@ -34,7 +34,7 @@ namespace xlnt { /// /// Typedef a rich_text_run as a pair of string and optional font. /// -struct rich_text_run +struct XLNT_API rich_text_run { std::string first; optional second; diff --git a/include/xlnt/styles/color.hpp b/include/xlnt/styles/color.hpp index 609ede8a..db35e6ef 100644 --- a/include/xlnt/styles/color.hpp +++ b/include/xlnt/styles/color.hpp @@ -252,19 +252,37 @@ public: /// Returns the internal indexed color representing this color. If this is not an RGB color, /// an invalid_attribute exception will be thrown. /// - rgb_color rgb() const; + const rgb_color& rgb() const; + + /// + /// Returns the internal indexed color representing this color. If this is not an RGB color, + /// an invalid_attribute exception will be thrown. + /// + rgb_color &rgb(); /// /// Returns the internal indexed color representing this color. If this is not an indexed color, /// an invalid_attribute exception will be thrown. /// - indexed_color indexed() const; + const indexed_color& indexed() const; + + /// + /// Returns the internal indexed color representing this color. If this is not an indexed color, + /// an invalid_attribute exception will be thrown. + /// + indexed_color &indexed(); /// /// Returns the internal indexed color representing this color. If this is not a theme color, /// an invalid_attribute exception will be thrown. /// - theme_color theme() const; + const theme_color& theme() const; + + /// + /// Returns the internal indexed color representing this color. If this is not a theme color, + /// an invalid_attribute exception will be thrown. + /// + theme_color& theme(); /// /// Returns the tint of this color. diff --git a/include/xlnt/utils/path.hpp b/include/xlnt/utils/path.hpp index beff0165..ba16526f 100644 --- a/include/xlnt/utils/path.hpp +++ b/include/xlnt/utils/path.hpp @@ -107,7 +107,7 @@ public: /// Create a string representing this path separated by the provided /// separator or the system-default separator if not provided. /// - std::string string() const; + const std::string& string() const; #ifdef _MSC_VER /// @@ -176,6 +176,11 @@ public: /// bool operator==(const path &other) const; + /// + /// Returns true if left path is equal to right path. + /// + bool operator!=(const path &other) const; + private: /// /// Returns the character that separates directories in the path. diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index f080107b..bc5f558f 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -341,32 +341,18 @@ cell_reference cell::reference() const return {d_->column_, d_->row_}; } -bool cell::operator==(std::nullptr_t) const -{ - return d_ == nullptr; -} - bool cell::operator==(const cell &comparand) const { return d_ == comparand.d_; } -cell &cell::operator=(const cell &rhs) +bool cell::operator!=(const cell &comparand) const { - d_->column_ = rhs.d_->column_; - d_->format_ = rhs.d_->format_; - d_->formula_ = rhs.d_->formula_; - d_->hyperlink_ = rhs.d_->hyperlink_; - d_->is_merged_ = rhs.d_->is_merged_; - d_->parent_ = rhs.d_->parent_; - d_->row_ = rhs.d_->row_; - d_->type_ = rhs.d_->type_; - d_->value_numeric_ = rhs.d_->value_numeric_; - d_->value_text_ = rhs.d_->value_text_; - - return *this; + return d_ != comparand.d_; } +cell &cell::operator=(const cell &rhs) = default; + hyperlink cell::hyperlink() const { return xlnt::hyperlink(&d_->hyperlink_.get()); @@ -437,7 +423,7 @@ void cell::hyperlink(xlnt::cell target, const std::string& display) void cell::hyperlink(xlnt::range target, const std::string &display) { // TODO: should this computed value be a method on a cell? - const auto range_address = target.worksheet().title() + "!" + target.reference().to_string(); + const auto range_address = target.target_worksheet().title() + "!" + target.reference().to_string(); d_->hyperlink_ = detail::hyperlink_impl(); d_->hyperlink_.get().relationship = xlnt::relationship("", relationship_type::hyperlink, @@ -778,6 +764,11 @@ bool operator==(std::nullptr_t, const cell &cell) return cell.data_type() == cell::type::empty; } +bool operator==(const cell &cell, std::nullptr_t) +{ + return nullptr == cell; +} + XLNT_API std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) { return stream << cell.to_string(); diff --git a/source/detail/implementations/cell_impl.hpp b/source/detail/implementations/cell_impl.hpp index ed1833ed..64756770 100644 --- a/source/detail/implementations/cell_impl.hpp +++ b/source/detail/implementations/cell_impl.hpp @@ -27,22 +27,26 @@ #include #include -#include #include +#include #include #include +#include #include namespace xlnt { namespace detail { -struct format_impl; struct worksheet_impl; struct cell_impl { cell_impl(); - + cell_impl(const cell_impl &other) = default; + cell_impl(cell_impl &&other) = default; + cell_impl &operator=(const cell_impl &other) = default; + cell_impl &operator=(cell_impl &&other) = default; + cell_type type_; worksheet_impl *parent_; @@ -72,9 +76,8 @@ inline bool operator==(const cell_impl &lhs, const cell_impl &rhs) && lhs.value_numeric_ == rhs.value_numeric_ && lhs.formula_ == rhs.formula_ && lhs.hyperlink_ == rhs.hyperlink_ - // comparing pointers is unlikely to be what is wanted - /*&& lhs.format_ == rhs.format_ - && lhs.comment_ == rhs.comment_*/; + && (lhs.format_.is_set() == rhs.format_.is_set() && (!lhs.format_.is_set() || *lhs.format_.get() == *rhs.format_.get())) + && (lhs.comment_.is_set() == rhs.comment_.is_set() && (!lhs.comment_.is_set() || *lhs.comment_.get() == *rhs.comment_.get())); } } // namespace detail diff --git a/source/styles/color.cpp b/source/styles/color.cpp index 3eccd394..01561eec 100644 --- a/source/styles/color.cpp +++ b/source/styles/color.cpp @@ -233,19 +233,37 @@ void color::auto_(bool value) auto__ = value; } -indexed_color color::indexed() const +const indexed_color& color::indexed() const { assert_type(color_type::indexed); return indexed_; } -theme_color color::theme() const +indexed_color &color::indexed() +{ + assert_type(color_type::indexed); + return indexed_; +} + +const theme_color& color::theme() const { assert_type(color_type::theme); return theme_; } -rgb_color color::rgb() const +theme_color &color::theme() +{ + assert_type(color_type::theme); + return theme_; +} + +const rgb_color& color::rgb() const +{ + assert_type(color_type::rgb); + return rgb_; +} + +rgb_color &color::rgb() { assert_type(color_type::rgb); return rgb_; diff --git a/source/styles/style.cpp b/source/styles/style.cpp index 5ba4db51..4dd5418d 100755 --- a/source/styles/style.cpp +++ b/source/styles/style.cpp @@ -92,6 +92,11 @@ bool style::operator==(const style &other) const return name() == other.name(); } +bool style::operator!=(const style &other) const +{ + return !operator==(other); +} + xlnt::alignment style::alignment() const { return d_->parent->alignments.at(d_->alignment_id.get()); diff --git a/source/utils/path.cpp b/source/utils/path.cpp index b67ab977..34c36cf0 100644 --- a/source/utils/path.cpp +++ b/source/utils/path.cpp @@ -221,7 +221,7 @@ std::pair path::split_extension() const // conversion -std::string path::string() const +const std::string& path::string() const { return internal_; } @@ -347,4 +347,9 @@ bool path::operator==(const path &other) const return internal_ == other.internal_; } +bool path::operator!=(const path &other) const +{ + return !operator==(other); +} + } // namespace xlnt diff --git a/source/utils/variant.cpp b/source/utils/variant.cpp index 5565fe94..4c420eea 100644 --- a/source/utils/variant.cpp +++ b/source/utils/variant.cpp @@ -27,9 +27,8 @@ namespace xlnt { variant::variant() -{ - -} + : type_(type::null) +{} variant::variant(const std::string &value) : type_(type::lpstr), diff --git a/tests/cell/cell_test_suite.cpp b/tests/cell/cell_test_suite.cpp index 8729134e..510786b3 100644 --- a/tests/cell/cell_test_suite.cpp +++ b/tests/cell/cell_test_suite.cpp @@ -29,20 +29,20 @@ #include #include #include -#include #include #include #include #include +#include #include #include -#include -#include #include #include #include #include - +#include +#include +#include class cell_test_suite : public test_suite { @@ -81,6 +81,7 @@ public: register_test(test_anchor); register_test(test_hyperlink); register_test(test_comment); + register_test(test_copy_and_compare); } private: @@ -253,11 +254,27 @@ private: xlnt::workbook wb; auto ws = wb.active_sheet(); auto cell = ws.cell(xlnt::cell_reference(1, 1)); + // error string can't be empty + xlnt_assert_throws(cell.error(""), xlnt::exception); + // error string has to have a leading '#' + xlnt_assert_throws(cell.error("not an error"), xlnt::exception); for (auto error_code : xlnt::cell::error_codes()) { + // error type from the string format cell.value(error_code.first, true); xlnt_assert(cell.data_type() == xlnt::cell::type::error); + std::string error; + xlnt_assert_throws_nothing(error = cell.error()); + xlnt_assert_equals(error, error_code.first); + // clearing the value clears the error state + cell.clear_value(); + xlnt_assert_throws(cell.error(), xlnt::exception); + // can explicitly set the error + xlnt_assert_throws_nothing(cell.error(error_code.first)); + std::string error2; + xlnt_assert_throws_nothing(error2 = cell.error()); + xlnt_assert_equals(error2, error_code.first); } } @@ -667,7 +684,7 @@ private: { xlnt::workbook wb; auto cell = wb.active_sheet().cell("A1"); - cell.value(123); + xlnt_assert(!cell.has_hyperlink()); xlnt_assert_throws(cell.hyperlink(), xlnt::invalid_attribute); xlnt_assert_throws(cell.hyperlink("notaurl"), xlnt::invalid_parameter); @@ -680,7 +697,8 @@ private: xlnt_assert_equals(cell.hyperlink().url(), link1); xlnt_assert_equals(cell.hyperlink().relationship().target().to_string(), link1); xlnt_assert_equals(cell.hyperlink().display(), link1); - // link with display + cell.clear_value(); + // link with display const std::string link2("http://example2.com"); const std::string display_txt("notaurl"); cell.hyperlink(link2, display_txt); @@ -689,6 +707,14 @@ private: xlnt_assert_equals(cell.hyperlink().url(), link2); xlnt_assert_equals(cell.hyperlink().relationship().target().to_string(), link2); xlnt_assert_equals(cell.hyperlink().display(), display_txt); + // value + int cell_test_val = 123; + cell.value(cell_test_val); + std::string cell_value_str = std::to_string(cell_test_val); + cell.hyperlink(link2, display_txt); + xlnt_assert_equals(cell.value(), 123); + xlnt_assert_equals(cell.hyperlink().display(), cell_value_str); // display text ignored + cell.clear_value(); // cell overload without display const std::string cell_target_str("A2"); auto cell_target = wb.active_sheet().cell(cell_target_str); @@ -698,12 +724,19 @@ private: xlnt_assert(!cell.hyperlink().external()); xlnt_assert_equals(cell.hyperlink().target_range(), link3); xlnt_assert_equals(cell.hyperlink().display(), link3); + cell.clear_value(); // cell overload with display cell.hyperlink(cell_target, display_txt); xlnt_assert(cell.has_hyperlink()); xlnt_assert(!cell.hyperlink().external()); xlnt_assert_equals(cell.hyperlink().target_range(), link3); xlnt_assert_equals(cell.hyperlink().display(), display_txt); + // value + cell.value(cell_test_val); + cell.hyperlink(cell_target, display_txt); + xlnt_assert_equals(cell.value(), 123); + xlnt_assert_equals(cell.hyperlink().display(), cell_value_str); // display text ignored + cell.clear_value(); // range overload without display const std::string range_target_str("A2:A5"); xlnt::range range_target(wb.active_sheet(), xlnt::range_reference(range_target_str)); @@ -713,14 +746,19 @@ private: xlnt_assert(!cell.hyperlink().external()); xlnt_assert_equals(cell.hyperlink().target_range(), link4); xlnt_assert_equals(cell.hyperlink().display(), link4); + cell.clear_value(); // range overload with display cell.hyperlink(range_target, display_txt); xlnt_assert(cell.has_hyperlink()); xlnt_assert(!cell.hyperlink().external()); xlnt_assert_equals(cell.hyperlink().target_range(), link4); xlnt_assert_equals(cell.hyperlink().display(), display_txt); - // value not touched + // value + cell.value(cell_test_val); + cell.hyperlink(range_target, display_txt); xlnt_assert_equals(cell.value(), 123); + xlnt_assert_equals(cell.hyperlink().display(), cell_value_str); // display text ignored + cell.clear_value(); } void test_comment() @@ -737,6 +775,31 @@ private: xlnt_assert(!cell.has_comment()); xlnt_assert_throws(cell.comment(), xlnt::exception); } + + void test_copy_and_compare() + { + xlnt::workbook wb; + auto ws = wb.active_sheet(); + auto cell1 = ws.cell("A1"); + auto cell2 = ws.cell("A2"); + + xlnt_assert_equals(cell1, cell1); + xlnt_assert_equals(cell2, cell2); + xlnt_assert_differs(cell1, cell2); + xlnt_assert_differs(cell2, cell1); + // nullptr equality + xlnt_assert(nullptr == cell1); + xlnt_assert(cell1 == nullptr); + cell1.value("test"); + xlnt_assert(!(nullptr == cell1)); + xlnt_assert(!(cell1 == nullptr)); + // copy + xlnt::cell cell3(cell1); + xlnt_assert_equals(cell1, cell3); + // assign + cell3 = cell2; + xlnt_assert_equals(cell2, cell3); + } }; static cell_test_suite x{}; \ No newline at end of file diff --git a/tests/cell/rich_text_test_suite.cpp b/tests/cell/rich_text_test_suite.cpp index af44f815..3af26383 100644 --- a/tests/cell/rich_text_test_suite.cpp +++ b/tests/cell/rich_text_test_suite.cpp @@ -35,6 +35,7 @@ public: rich_text_test_suite() { register_test(test_operators); + register_test(test_runs); } void test_operators() @@ -100,5 +101,19 @@ public: text_family_differs.add_run(run_family_differs); xlnt_assert_differs(text_formatted, text_family_differs); } + + void test_runs() + { + xlnt::rich_text rt; + xlnt_assert(rt.runs().empty()); + std::vector test_runs{xlnt::rich_text_run{"1_abc_test_123"}, xlnt::rich_text_run{"2_abc_test_123"}, xlnt::rich_text_run{"3_abc_test_123"}}; + // just one + rt.add_run(test_runs[0]); + xlnt_assert_equals(1, rt.runs().size()); + xlnt_assert_equals(test_runs[0], rt.runs()[0]); + // whole set + rt.runs(test_runs); + xlnt_assert_equals(test_runs, rt.runs()); + } }; static rich_text_test_suite x{}; \ No newline at end of file diff --git a/tests/styles/color_test_suite.cpp b/tests/styles/color_test_suite.cpp index ac61291b..e65f540a 100644 --- a/tests/styles/color_test_suite.cpp +++ b/tests/styles/color_test_suite.cpp @@ -38,37 +38,39 @@ public: void test_known_colors() { - const std::vector> known_colors - { - { xlnt::color::black(), "FF000000" }, - { xlnt::color::white(), "FFFFFFFF" }, - { xlnt::color::red(), "FFFF0000" }, - { xlnt::color::darkred(), "FF8B0000" }, - { xlnt::color::blue(), "FF0000FF" }, - { xlnt::color::darkblue(), "FF00008B" }, - { xlnt::color::green(), "FF00FF00" }, - { xlnt::color::darkgreen(), "FF008B00" }, - { xlnt::color::yellow(), "FFFFFF00" }, - { xlnt::color::darkyellow(), "FFCCCC00" } - }; - + const std::vector> known_colors{ + {xlnt::color::black(), "FF000000"}, + {xlnt::color::white(), "FFFFFFFF"}, + {xlnt::color::red(), "FFFF0000"}, + {xlnt::color::darkred(), "FF8B0000"}, + {xlnt::color::blue(), "FF0000FF"}, + {xlnt::color::darkblue(), "FF00008B"}, + {xlnt::color::green(), "FF00FF00"}, + {xlnt::color::darkgreen(), "FF008B00"}, + {xlnt::color::yellow(), "FFFFFF00"}, + {xlnt::color::darkyellow(), "FFCCCC00"}}; + for (auto pair : known_colors) { xlnt_assert_equals(pair.first.rgb().hex_string(), pair.second); } } - + void test_non_rgb_colors() { - xlnt::color indexed = xlnt::indexed_color(1); - xlnt_assert(!indexed.auto_()); + xlnt::color indexed = xlnt::indexed_color(1); + xlnt_assert(!indexed.auto_()); xlnt_assert_equals(indexed.indexed().index(), 1); + indexed.indexed().index(2); + xlnt_assert_equals(indexed.indexed().index(), 2); xlnt_assert_throws(indexed.theme(), xlnt::invalid_attribute); xlnt_assert_throws(indexed.rgb(), xlnt::invalid_attribute); - xlnt::color theme = xlnt::theme_color(3); - xlnt_assert(!theme.auto_()); + xlnt::color theme = xlnt::theme_color(3); + xlnt_assert(!theme.auto_()); xlnt_assert_equals(theme.theme().index(), 3); + theme.theme().index(4); + xlnt_assert_equals(theme.theme().index(), 4); xlnt_assert_throws(theme.indexed(), xlnt::invalid_attribute); xlnt_assert_throws(theme.rgb(), xlnt::invalid_attribute); } diff --git a/tests/styles/conditional_format_test_suite.cpp b/tests/styles/conditional_format_test_suite.cpp new file mode 100644 index 00000000..1410774f --- /dev/null +++ b/tests/styles/conditional_format_test_suite.cpp @@ -0,0 +1,64 @@ +// Copyright (c) 2014-2018 Thomas Fussell +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE +// +// @license: http://www.opensource.org/licenses/mit-license.php +// @author: see AUTHORS file + +#include +#include +#include + +class conditional_format_test_suite : public test_suite +{ +public: + conditional_format_test_suite() + { + register_test(test_all); + } + + void test_all() + { + xlnt::workbook wb; + auto ws = wb.active_sheet(); + auto format = ws.conditional_format(xlnt::range_reference("A1:A10"), xlnt::condition::text_contains("test")); + xlnt_assert(!format.has_border()); + xlnt_assert(!format.has_fill()); + xlnt_assert(!format.has_font()); + // set border + auto border = xlnt::border().diagonal(xlnt::diagonal_direction::both); + format.border(border); + xlnt_assert(format.has_border()); + xlnt_assert_equals(format.border(), border); + // set fill + auto fill = xlnt::fill(xlnt::gradient_fill().type(xlnt::gradient_fill_type::path)); + format.fill(fill); + xlnt_assert(format.has_fill()); + xlnt_assert_equals(format.fill(), fill); + // set font + auto font = xlnt::font().color(xlnt::color::darkblue()); + format.font(font); + xlnt_assert(format.has_font()); + xlnt_assert_equals(format.font(), font); + // copy ctor + auto format_copy(format); + xlnt_assert_equals(format, format_copy); + } +}; +static conditional_format_test_suite x; \ No newline at end of file diff --git a/tests/styles/protection_test_suite.cpp b/tests/styles/protection_test_suite.cpp new file mode 100644 index 00000000..039d987c --- /dev/null +++ b/tests/styles/protection_test_suite.cpp @@ -0,0 +1,55 @@ +// Copyright (c) 2014-2018 Thomas Fussell +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE +// +// @license: http://www.opensource.org/licenses/mit-license.php +// @author: see AUTHORS file + +#include +#include +#include + +class protection_test_suite : public test_suite +{ +public: + protection_test_suite() + { + register_test(test_all); + } + + void test_all() + { + auto prot = xlnt::protection::unlocked_and_visible(); + xlnt_assert(!prot.hidden()); + xlnt_assert(!prot.locked()); + + prot = xlnt::protection::locked_and_visible(); + xlnt_assert(!prot.hidden()); + xlnt_assert(prot.locked()); + + prot = xlnt::protection::unlocked_and_hidden(); + xlnt_assert(prot.hidden()); + xlnt_assert(!prot.locked()); + + prot = xlnt::protection::locked_and_hidden(); + xlnt_assert(prot.hidden()); + xlnt_assert(prot.locked()); + } +}; +static protection_test_suite x; \ No newline at end of file diff --git a/tests/styles/style_test_suite.cpp b/tests/styles/style_test_suite.cpp new file mode 100644 index 00000000..3cd22a15 --- /dev/null +++ b/tests/styles/style_test_suite.cpp @@ -0,0 +1,66 @@ +// Copyright (c) 2014-2018 Thomas Fussell +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE +// +// @license: http://www.opensource.org/licenses/mit-license.php +// @author: see AUTHORS file + +#include +#include +#include + +class style_test_suite : public test_suite +{ +public: + style_test_suite() + { + register_test(test_all); + } + + void test_all() + { + xlnt::workbook wb; + auto ws = wb.active_sheet(); + auto test_style = wb.create_style("test_style"); + test_style.number_format(xlnt::number_format::date_ddmmyyyy()); + + auto copy_style(test_style); + xlnt_assert_equals(test_style, copy_style); + + // number format + xlnt_assert_equals(copy_style.name(), "test_style"); + xlnt_assert_equals(copy_style.number_format(), xlnt::number_format::date_ddmmyyyy()); + //xlnt_assert(!copy_style.number_format_applied()); // this doesn't seem to have sensible behaviour? + copy_style.number_format(xlnt::number_format::date_datetime(), true); // true applied param + xlnt_assert_equals(copy_style.number_format(), xlnt::number_format::date_datetime()); + xlnt_assert(copy_style.number_format_applied()); + copy_style.number_format(xlnt::number_format::date_dmminus(), false); // false applied param + xlnt_assert_equals(copy_style.number_format(), xlnt::number_format::date_dmminus()); + xlnt_assert(!copy_style.number_format_applied()); + + xlnt_assert(!copy_style.pivot_button()); + copy_style.pivot_button(true); + xlnt_assert(copy_style.pivot_button()); + + xlnt_assert(!copy_style.quote_prefix()); + copy_style.quote_prefix(true); + xlnt_assert(copy_style.quote_prefix()); + } +}; +static style_test_suite x; \ No newline at end of file diff --git a/tests/utils/path_test_suite.cpp b/tests/utils/path_test_suite.cpp index f8d4f24c..bfc6fdfa 100644 --- a/tests/utils/path_test_suite.cpp +++ b/tests/utils/path_test_suite.cpp @@ -26,6 +26,7 @@ #include #include #include +#include class path_test_suite : public test_suite { diff --git a/tests/utils/variant_tests.cpp b/tests/utils/variant_tests.cpp new file mode 100644 index 00000000..10de95ec --- /dev/null +++ b/tests/utils/variant_tests.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2014-2018 Thomas Fussell +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE +// +// @license: http://www.opensource.org/licenses/mit-license.php +// @author: see AUTHORS file + +#include +#include + +class variant_test_suite : public test_suite +{ +public: + variant_test_suite() + : test_suite() + { + register_test(test_null); + register_test(test_int32); + register_test(test_string); + } + + void test_null() + { + xlnt::variant var_null; + xlnt_assert_equals(var_null.value_type(), xlnt::variant::type::null); + xlnt_assert(var_null.is(xlnt::variant::type::null)); + } + + void test_int32() + { + xlnt::variant var_int(std::int32_t(10)); + xlnt_assert_equals(var_int.value_type(), xlnt::variant::type::i4); + xlnt_assert(var_int.is(xlnt::variant::type::i4)); + xlnt_assert_throws_nothing(var_int.get()); + xlnt_assert_equals(10, var_int.get()); + } + + void test_string() + { + xlnt::variant var_str1("test1"); + xlnt_assert_equals(var_str1.value_type(), xlnt::variant::type::lpstr); + xlnt_assert(var_str1.is(xlnt::variant::type::lpstr)); + xlnt_assert_throws_nothing(var_str1.get()); + xlnt_assert_equals("test1", var_str1.get()); + + xlnt::variant var_str2(std::string("test2")); + xlnt_assert_equals(var_str2.value_type(), xlnt::variant::type::lpstr); + xlnt_assert(var_str2.is(xlnt::variant::type::lpstr)); + xlnt_assert_throws_nothing(var_str2.get()); + xlnt_assert_equals("test2", var_str2.get()); + } +}; +static variant_test_suite x; \ No newline at end of file diff --git a/tests/worksheet/range_test_suite.cpp b/tests/worksheet/range_test_suite.cpp index 98012289..b8c14fb6 100644 --- a/tests/worksheet/range_test_suite.cpp +++ b/tests/worksheet/range_test_suite.cpp @@ -37,10 +37,40 @@ class range_test_suite : public test_suite public: range_test_suite() { + register_test(test_construction); register_test(test_batch_formatting); register_test(test_clear_cells); } + void test_construction() + { + xlnt::workbook wb; + auto ws = wb.active_sheet(); + + xlnt::range range_1(ws, xlnt::range_reference("A1:D10")); + xlnt_assert_equals(range_1.target_worksheet(), ws); + xlnt_assert_equals(1, range_1.front()[0].row()); // NOTE: querying row/column here desperately needs some shortcuts + xlnt_assert_equals(xlnt::column_t("D"), range_1.front().back().column()); + xlnt_assert_equals(10, range_1.back()[0].row()); + xlnt_assert_equals(xlnt::column_t("D"), range_1.back().back().column()); + // assert default parameters in ctor + xlnt::range range_2(ws, xlnt::range_reference("A1:D10"), xlnt::major_order::row, false); + xlnt_assert_equals(range_1, range_2); + // assert copy + xlnt::range range_3(range_2); + xlnt_assert_equals(range_1, range_3); + + // column order + xlnt::range range_4(ws, xlnt::range_reference("A1:D10"), xlnt::major_order::column); + xlnt_assert_equals(xlnt::column_t("A"), range_4.front()[0].column()); // NOTE: querying row/column here desperately needs some shortcuts + xlnt_assert_equals(10, range_4.front().back().row()); + xlnt_assert_equals(xlnt::column_t("D"), range_4.back()[0].column()); + xlnt_assert_equals(10, range_4.back().back().row()); + // assignment + range_3 = range_4; + xlnt_assert_equals(range_3, range_4); + } + void test_batch_formatting() { xlnt::workbook wb;