range_test_suite.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/font.hpp>
  27. #include <xlnt/workbook/workbook.hpp>
  28. #include <xlnt/worksheet/header_footer.hpp>
  29. #include <xlnt/worksheet/range.hpp>
  30. #include <xlnt/worksheet/worksheet.hpp>
  31. class range_test_suite : public test_suite
  32. {
  33. public:
  34. range_test_suite()
  35. {
  36. register_test(test_construction);
  37. register_test(test_batch_formatting);
  38. register_test(test_clear_cells);
  39. }
  40. void test_construction()
  41. {
  42. xlnt::workbook wb;
  43. auto ws = wb.active_sheet();
  44. xlnt::range range_1(ws, xlnt::range_reference("A1:D10"));
  45. xlnt_assert_equals(range_1.target_worksheet(), ws);
  46. xlnt_assert_equals(1, range_1.front()[0].row()); // NOTE: querying row/column here desperately needs some shortcuts
  47. xlnt_assert_equals(xlnt::column_t("D"), range_1.front().back().column());
  48. xlnt_assert_equals(10, range_1.back()[0].row());
  49. xlnt_assert_equals(xlnt::column_t("D"), range_1.back().back().column());
  50. // assert default parameters in ctor
  51. xlnt::range range_2(ws, xlnt::range_reference("A1:D10"), xlnt::major_order::row, false);
  52. xlnt_assert_equals(range_1, range_2);
  53. // assert copy
  54. xlnt::range range_3(range_2);
  55. xlnt_assert_equals(range_1, range_3);
  56. // column order
  57. xlnt::range range_4(ws, xlnt::range_reference("A1:D10"), xlnt::major_order::column);
  58. xlnt_assert_equals(xlnt::column_t("A"), range_4.front()[0].column()); // NOTE: querying row/column here desperately needs some shortcuts
  59. xlnt_assert_equals(10, range_4.front().back().row());
  60. xlnt_assert_equals(xlnt::column_t("D"), range_4.back()[0].column());
  61. xlnt_assert_equals(10, range_4.back().back().row());
  62. // assignment
  63. range_3 = range_4;
  64. xlnt_assert_equals(range_3, range_4);
  65. }
  66. void test_batch_formatting()
  67. {
  68. xlnt::workbook wb;
  69. auto ws = wb.active_sheet();
  70. for (auto row = 1; row <= 10; ++row)
  71. {
  72. for (auto column = 1; column <= 10; ++column)
  73. {
  74. auto ref = xlnt::cell_reference(column, row);
  75. ws[ref].value(ref.to_string());
  76. }
  77. }
  78. ws.range("A1:A10").font(xlnt::font().name("Arial"));
  79. ws.range("A1:J1").font(xlnt::font().bold(true));
  80. xlnt_assert_equals(ws.cell("A1").font().name(), "Calibri");
  81. xlnt_assert(ws.cell("A1").font().bold());
  82. xlnt_assert_equals(ws.cell("A2").font().name(), "Arial");
  83. xlnt_assert(!ws.cell("A2").font().bold());
  84. xlnt_assert_equals(ws.cell("B1").font().name(), "Calibri");
  85. xlnt_assert(ws.cell("B1").font().bold());
  86. xlnt_assert(!ws.cell("B2").has_format());
  87. }
  88. void test_clear_cells()
  89. {
  90. xlnt::workbook wb;
  91. auto ws = wb.active_sheet();
  92. ws.cell("A1").value("A1");
  93. ws.cell("A3").value("A3");
  94. ws.cell("C1").value("C1");
  95. ws.cell("B2").value("B2");
  96. ws.cell("C3").value("C3");
  97. xlnt_assert_equals(ws.calculate_dimension(), xlnt::range_reference(1, 1, 3, 3));
  98. auto range = ws.range("B1:C3");
  99. range.clear_cells();
  100. xlnt_assert_equals(ws.calculate_dimension(), xlnt::range_reference(1, 1, 1, 3));
  101. }
  102. };
  103. static range_test_suite x;