named_range_test_suite.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/workbook/named_range.hpp>
  26. class named_range_test_suite : public test_suite
  27. {
  28. public:
  29. void test_split()
  30. {
  31. /*
  32. using string_pair = std::pair<std::string, std::string>;
  33. using string_pair_vector = std::vector<string_pair>;
  34. using expected_pair = std::pair<std::string, string_pair_vector>;
  35. std::vector<expected_pair> expected_pairs =
  36. {
  37. { "'My Sheet'!$D$8", {{ "My Sheet", "$D$8" }} },
  38. { "Sheet1!$A$1", {{ "Sheet1", "$A$1" }} },
  39. { "[1]Sheet1!$A$1", {{ "[1]Sheet1", "$A$1" }} },
  40. { "[1]!B2range", {{ "[1]", "" }} },
  41. { "Sheet1!$C$5:$C$7,Sheet1!$C$9:$C$11,Sheet1!$E$5:$E$7,Sheet1!$E$9:$E$11,Sheet1!$D$8",
  42. {
  43. { "Sheet1", "$C$5:$C$7" },
  44. { "Sheet1", "$C$9:$C$11" },
  45. { "Sheet1", "$E$5:$E$7" },
  46. { "Sheet1", "$E$9:$E$11" },
  47. { "Sheet1", "$D$8" }
  48. }
  49. }
  50. };
  51. for(auto pair : expected_pairs)
  52. {
  53. xlnt_assert_equals(xlnt::split_named_range(pair.first), pair.second);
  54. }
  55. */
  56. }
  57. void test_split_no_quotes()
  58. {
  59. /*xlnt_assert_equals([("HYPOTHESES", "$B$3:$L$3"), ], split_named_range("HYPOTHESES!$B$3:$L$3"))*/
  60. }
  61. void test_bad_range_name()
  62. {
  63. /*xlnt_assert_raises(NamedRangeException, split_named_range, "HYPOTHESES$B$3")*/
  64. }
  65. void test_range_name_worksheet_special_chars()
  66. {
  67. /*class DummyWs
  68. {
  69. title = "My Sheeet with a , and \""
  70. void __str__()
  71. {
  72. return title
  73. }
  74. ws = DummyWs()
  75. };
  76. class DummyWB
  77. {
  78. void get_sheet_by_name(self, name)
  79. {
  80. if name == ws.title :
  81. return ws
  82. }
  83. };
  84. handle = open(os.path.join(DATADIR, "reader", "workbook_namedrange.xml"))
  85. try
  86. {
  87. content = handle.read()
  88. named_ranges = read_named_ranges(content, DummyWB())
  89. xlnt_assert_equals(1, len(named_ranges))
  90. ok_(isinstance(named_ranges[0], NamedRange))
  91. xlnt_assert_equals([(ws, "$U$16:$U$24"), (ws, "$V$28:$V$36")], named_ranges[0].destinations)
  92. }
  93. finally
  94. {
  95. handle.close()
  96. }*/
  97. }
  98. void test_read_named_ranges()
  99. {
  100. /*class DummyWs
  101. {
  102. title = "My Sheeet"
  103. void __str__()
  104. {
  105. return title;
  106. }
  107. };
  108. class DummyWB
  109. {
  110. void get_sheet_by_name(self, name)
  111. {
  112. return DummyWs()
  113. }
  114. };
  115. handle = open(os.path.join(DATADIR, "reader", "workbook.xml"))
  116. try :
  117. content = handle.read()
  118. named_ranges = read_named_ranges(content, DummyWB())
  119. xlnt_assert_equals(["My Sheeet!$D$8"], [str(range) for range in named_ranges])
  120. finally :
  121. handle.close()*/
  122. }
  123. void test_oddly_shaped_named_ranges()
  124. {
  125. /* ranges_counts = ((4, "TEST_RANGE"),
  126. (3, "TRAP_1"),
  127. (13, "TRAP_2"))*/
  128. }
  129. //void check_ranges(ws, count, range_name)
  130. //{
  131. // /*xlnt_assert_equals(count, len(ws.range(range_name)))
  132. // wb = load_workbook(os.path.join(DATADIR, "genuine", "merge_range.xlsx"),
  133. // use_iterators = False)
  134. // ws = wb.worksheets[0]
  135. // for count, range_name in ranges_counts
  136. // {
  137. // yield check_ranges, ws, count, range_name
  138. // }*/
  139. //}
  140. void test_merged_cells_named_range()
  141. {
  142. /*wb = load_workbook(os.path.join(DATADIR, "genuine", "merge_range.xlsx"),
  143. use_iterators = False)
  144. ws = wb.worksheets[0]
  145. cell = ws.range("TRAP_3")
  146. xlnt_assert_equals("B15", cell.get_coordinate())
  147. xlnt_assert_equals(10, cell.value)*/
  148. }
  149. void setUp()
  150. {
  151. /*wb = load_workbook(os.path.join(DATADIR, "genuine", "NameWithValueBug.xlsx"))
  152. ws = wb.get_sheet_by_name("Sheet1")
  153. make_tmpdir()*/
  154. }
  155. void tearDown()
  156. {
  157. /* clean_tmpdir()*/
  158. }
  159. void test_has_ranges()
  160. {
  161. /*ranges = wb.get_named_ranges()
  162. xlnt_assert_equals(["MyRef", "MySheetRef", "MySheetRef", "MySheetValue", "MySheetValue", "MyValue"], [range.name for range in ranges])*/
  163. }
  164. void test_workbook_has_normal_range()
  165. {
  166. /*normal_range = wb.get_named_range("MyRef")
  167. xlnt_assert_equals("MyRef", normal_range.name)*/
  168. }
  169. void test_workbook_has_value_range()
  170. {
  171. /*value_range = wb.get_named_range("MyValue")
  172. xlnt_assert_equals("MyValue", value_range.name)
  173. xlnt_assert_equals("9.99", value_range.value)*/
  174. }
  175. void test_worksheet_range()
  176. {
  177. /*range = ws.range("MyRef")*/
  178. }
  179. void test_worksheet_range_error_on_value_range()
  180. {
  181. /*xlnt_assert_raises(NamedRangeException, ws.range, "MyValue")*/
  182. }
  183. //void range_as_string(self, range, include_value = False)
  184. //{
  185. // /*void scope_as_string(range)
  186. // {
  187. // if range.scope
  188. // {
  189. // return range.scope.title
  190. // }
  191. // else
  192. // {
  193. // return "Workbook"
  194. // }
  195. // }
  196. // retval = "%s: %s" % (range.name, scope_as_string(range))
  197. // if include_value :
  198. // if isinstance(range, NamedRange) :
  199. // retval += "=[range]"
  200. // else :
  201. // retval += "=" + range.value
  202. // return retval*/
  203. //}
  204. void test_handles_scope()
  205. {
  206. /*ranges = wb.get_named_ranges()
  207. xlnt_assert_equals(["MyRef: Workbook", "MySheetRef: Sheet1", "MySheetRef: Sheet2", "MySheetValue: Sheet1", "MySheetValue: Sheet2", "MyValue: Workbook"],
  208. [range_as_string(range) for range in ranges])*/
  209. }
  210. void test_can_be_saved()
  211. {
  212. /*FNAME = os.path.join(TMPDIR, "foo.xlsx")
  213. wb.save(FNAME)
  214. wbcopy = load_workbook(FNAME)
  215. xlnt_assert_equals(["MyRef: Workbook=[range]", "MySheetRef: Sheet1=[range]", "MySheetRef: Sheet2=[range]", "MySheetValue: Sheet1=3.33", "MySheetValue: Sheet2=14.4", "MyValue: Workbook=9.99"],
  216. [range_as_string(range, include_value = True) for range in wbcopy.get_named_ranges()])*/
  217. }
  218. };