rich_text_test_suite.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <ctime>
  24. #include <iostream>
  25. #include <sstream>
  26. #include <helpers/test_suite.hpp>
  27. #include <xlnt/cell/rich_text.hpp>
  28. class rich_text_test_suite : public test_suite
  29. {
  30. public:
  31. rich_text_test_suite()
  32. {
  33. register_test(test_operators);
  34. register_test(test_runs);
  35. register_test(test_phonetic_runs);
  36. register_test(test_phonetic_properties);
  37. }
  38. void test_operators()
  39. {
  40. xlnt::rich_text text1;
  41. xlnt::rich_text text2;
  42. xlnt_assert_equals(text1, text2);
  43. xlnt::rich_text_run run_default;
  44. text1.add_run(run_default);
  45. xlnt_assert_differs(text1, text2);
  46. text2.add_run(run_default);
  47. xlnt_assert_equals(text1, text2);
  48. xlnt::rich_text_run run_formatted;
  49. xlnt::font run_font;
  50. run_font.color(xlnt::color::green());
  51. run_font.name("Cambria");
  52. run_font.scheme("ascheme");
  53. run_font.size(40);
  54. run_font.family(17);
  55. run_formatted.second = run_font;
  56. xlnt::rich_text text_formatted;
  57. text_formatted.add_run(run_formatted);
  58. xlnt::rich_text_run run_color_differs = run_formatted;
  59. run_font = xlnt::font();
  60. run_font.color(xlnt::color::red());
  61. run_color_differs.second = run_font;
  62. xlnt::rich_text text_color_differs;
  63. text_color_differs.add_run(run_color_differs);
  64. xlnt_assert_differs(text_formatted, text_color_differs);
  65. xlnt::rich_text_run run_font_differs = run_formatted;
  66. run_font = xlnt::font();
  67. run_font.name("Calibri");
  68. run_font_differs.second = run_font;
  69. xlnt::rich_text text_font_differs;
  70. text_font_differs.add_run(run_font_differs);
  71. xlnt_assert_differs(text_formatted, text_font_differs);
  72. xlnt::rich_text_run run_scheme_differs = run_formatted;
  73. run_font = xlnt::font();
  74. run_font.scheme("bscheme");
  75. run_scheme_differs.second = run_font;
  76. xlnt::rich_text text_scheme_differs;
  77. text_scheme_differs.add_run(run_scheme_differs);
  78. xlnt_assert_differs(text_formatted, text_scheme_differs);
  79. xlnt::rich_text_run run_size_differs = run_formatted;
  80. run_font = xlnt::font();
  81. run_font.size(41);
  82. run_size_differs.second = run_font;
  83. xlnt::rich_text text_size_differs;
  84. text_size_differs.add_run(run_size_differs);
  85. xlnt_assert_differs(text_formatted, text_size_differs);
  86. xlnt::rich_text_run run_family_differs = run_formatted;
  87. run_font = xlnt::font();
  88. run_font.family(18);
  89. run_family_differs.second = run_font;
  90. xlnt::rich_text text_family_differs;
  91. text_family_differs.add_run(run_family_differs);
  92. xlnt_assert_differs(text_formatted, text_family_differs);
  93. }
  94. void test_runs()
  95. {
  96. xlnt::rich_text rt;
  97. xlnt_assert(rt.runs().empty());
  98. std::vector<xlnt::rich_text_run> 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"}};
  99. // just one
  100. rt.add_run(test_runs[0]);
  101. xlnt_assert_equals(1, rt.runs().size());
  102. xlnt_assert_equals(test_runs[0], rt.runs()[0]);
  103. // whole set
  104. rt.runs(test_runs);
  105. xlnt_assert_equals(test_runs, rt.runs());
  106. }
  107. void test_phonetic_runs()
  108. {
  109. xlnt::rich_text rt;
  110. rt.plain_text("取引", true);
  111. rt.add_phonetic_run({"トリヒキ", 0, 2});
  112. xlnt_assert_equals(rt.phonetic_runs().size(), 1);
  113. xlnt_assert_equals(rt.phonetic_runs()[0].text, "トリヒキ");
  114. xlnt_assert_equals(rt.phonetic_runs()[0].start, 0);
  115. xlnt_assert_equals(rt.phonetic_runs()[0].end, 2);
  116. }
  117. void test_phonetic_properties()
  118. {
  119. xlnt::rich_text rt;
  120. xlnt::phonetic_pr ph(1);
  121. ph.type(xlnt::phonetic_pr::type_from_string("fullwidthKatakana"));
  122. ph.alignment(xlnt::phonetic_pr::alignment_from_string("Center"));
  123. rt.phonetic_properties(ph);
  124. xlnt_assert_equals(rt.has_phonetic_properties(), true);
  125. xlnt_assert_equals(rt.phonetic_properties().has_type(), true);
  126. xlnt_assert_equals(rt.phonetic_properties().has_alignment(), true);
  127. }
  128. };
  129. static rich_text_test_suite x{};