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