fill_test_suite.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2014-2021 Thomas Fussell
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE
  20. //
  21. // @license: http://www.opensource.org/licenses/mit-license.php
  22. // @author: see AUTHORS file
  23. #include <iostream>
  24. #include <helpers/test_suite.hpp>
  25. #include <xlnt/cell/cell.hpp>
  26. #include <xlnt/styles/fill.hpp>
  27. #include <xlnt/utils/date.hpp>
  28. #include <xlnt/worksheet/worksheet.hpp>
  29. class fill_test_suite : public test_suite
  30. {
  31. public:
  32. fill_test_suite()
  33. {
  34. register_test(test_properties);
  35. register_test(test_comparison);
  36. register_test(test_two_fills);
  37. }
  38. void test_properties()
  39. {
  40. xlnt::fill fill;
  41. xlnt_assert_equals(fill.type(), xlnt::fill_type::pattern);
  42. fill = xlnt::fill(xlnt::gradient_fill());
  43. xlnt_assert_equals(fill.type(), xlnt::fill_type::gradient);
  44. xlnt_assert_equals(fill.gradient_fill().type(), xlnt::gradient_fill_type::linear);
  45. xlnt_assert_equals(fill.gradient_fill().left(), 0);
  46. xlnt_assert_equals(fill.gradient_fill().right(), 0);
  47. xlnt_assert_equals(fill.gradient_fill().top(), 0);
  48. xlnt_assert_equals(fill.gradient_fill().bottom(), 0);
  49. xlnt_assert_throws(fill.pattern_fill(), xlnt::invalid_attribute);
  50. xlnt_assert_equals(fill.gradient_fill().degree(), 0);
  51. fill = fill.gradient_fill().degree(1);
  52. xlnt_assert_equals(fill.gradient_fill().degree(), 1);
  53. fill = xlnt::pattern_fill().type(xlnt::pattern_fill_type::solid);
  54. fill = fill.pattern_fill().foreground(xlnt::color::black());
  55. xlnt_assert(fill.pattern_fill().foreground().is_set());
  56. xlnt_assert_equals(fill.pattern_fill().foreground().get().rgb().hex_string(),
  57. xlnt::color::black().rgb().hex_string());
  58. fill = fill.pattern_fill().background(xlnt::color::green());
  59. xlnt_assert(fill.pattern_fill().background().is_set());
  60. xlnt_assert_equals(fill.pattern_fill().background().get().rgb().hex_string(),
  61. xlnt::color::green().rgb().hex_string());
  62. const auto &const_fill = fill;
  63. xlnt_assert(const_fill.pattern_fill().foreground().is_set());
  64. xlnt_assert(const_fill.pattern_fill().background().is_set());
  65. }
  66. void test_comparison()
  67. {
  68. xlnt::fill pattern_fill = xlnt::pattern_fill().type(xlnt::pattern_fill_type::solid);
  69. xlnt::fill gradient_fill_linear = xlnt::gradient_fill().type(xlnt::gradient_fill_type::linear);
  70. xlnt::fill gradient_fill_path = xlnt::gradient_fill().type(xlnt::gradient_fill_type::path);
  71. xlnt_assert_differs(pattern_fill, gradient_fill_linear);
  72. xlnt_assert_differs(gradient_fill_linear, gradient_fill_path);
  73. xlnt_assert_differs(gradient_fill_path, pattern_fill);
  74. }
  75. void test_two_fills()
  76. {
  77. xlnt::workbook wb;
  78. auto ws1 = wb.active_sheet();
  79. auto cell1 = ws1.cell("A10");
  80. auto cell2 = ws1.cell("A11");
  81. cell1.fill(xlnt::fill::solid(xlnt::color::yellow()));
  82. cell1.value("Fill Yellow");
  83. cell2.fill(xlnt::fill::solid(xlnt::color::green()));
  84. cell2.value(xlnt::date(2010, 7, 13));
  85. xlnt_assert_equals(cell1.fill(), xlnt::fill::solid(xlnt::color::yellow()));
  86. xlnt_assert_equals(cell2.fill(), xlnt::fill::solid(xlnt::color::green()));
  87. }
  88. };
  89. static fill_test_suite x;