TestSuite.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_TESTS_VISUALTESTS_TESTSUITE_H
  29. #define RMLUI_TESTS_VISUALTESTS_TESTSUITE_H
  30. #include <RmlUi/Core/Types.h>
  31. #include <algorithm>
  32. class TestSuite {
  33. public:
  34. enum class Direction { None, Forward, Backward, Any };
  35. TestSuite(Rml::String directory, Rml::StringList files) : directory(std::move(directory)), files(std::move(files))
  36. {
  37. RMLUI_ASSERTMSG(!this->files.empty(), "At least one file in the test suite is required.");
  38. }
  39. const Rml::String& GetDirectory() const
  40. {
  41. return directory;
  42. }
  43. const Rml::String& GetFilename() const
  44. {
  45. return files[index];
  46. }
  47. Rml::String GetPath() const
  48. {
  49. return directory + '/' + files[index];
  50. }
  51. const Rml::String& GetFilter() const
  52. {
  53. return filter;
  54. }
  55. void SetFilter(Rml::String new_filter)
  56. {
  57. filter = new_filter;
  58. UpdateFilteredTests();
  59. SetIndex(index, Direction::Any);
  60. }
  61. bool SetIndex(int new_index, Direction filter_direction = Direction::None)
  62. {
  63. new_index = GetIndexFiltered(new_index, filter_direction);
  64. if (new_index < 0 || new_index >= (int)files.size())
  65. return false;
  66. index = new_index;
  67. return true;
  68. }
  69. bool Next()
  70. {
  71. return SetIndex(index + 1, Direction::Forward);
  72. }
  73. bool Previous()
  74. {
  75. return SetIndex(index - 1, Direction::Backward);
  76. }
  77. int GetIndex() const
  78. {
  79. return index;
  80. }
  81. int GetFilterIndex() const
  82. {
  83. auto it = std::lower_bound(filtered_tests_indices.begin(), filtered_tests_indices.end(), index);
  84. if (it == filtered_tests_indices.end() || *it != index)
  85. return -1;
  86. return int(it - filtered_tests_indices.begin());
  87. }
  88. int GetNumTests() const
  89. {
  90. return (int)files.size();
  91. }
  92. int GetNumFilteredTests() const
  93. {
  94. return filter.empty() ? GetNumTests() : (int)filtered_tests_indices.size();
  95. }
  96. private:
  97. int GetIndexFiltered(int new_index, Direction filter_direction) const
  98. {
  99. if (filter_direction == Direction::None || filtered_tests_indices.empty())
  100. return new_index;
  101. auto it = std::lower_bound(filtered_tests_indices.begin(), filtered_tests_indices.end(), new_index);
  102. // Exact match
  103. if (it != filtered_tests_indices.end() && *it == new_index)
  104. return *it;
  105. if (filter_direction == Direction::Forward)
  106. {
  107. if (it != filtered_tests_indices.end())
  108. return *it;
  109. }
  110. else if (filter_direction == Direction::Backward)
  111. {
  112. if (it != filtered_tests_indices.begin())
  113. return *(it - 1);
  114. }
  115. else if (filter_direction == Direction::Any)
  116. {
  117. // Like forward but will go back if we cannot go forward.
  118. return it == filtered_tests_indices.end() ? *(it - 1) : *it;
  119. }
  120. return -1;
  121. }
  122. bool MatchesFilter(int id) const
  123. {
  124. RMLUI_ASSERT(id >= 0 && id < (int)files.size());
  125. return (files[id].find(filter) != Rml::String::npos);
  126. }
  127. void UpdateFilteredTests()
  128. {
  129. filtered_tests_indices.clear();
  130. for (int i = 0; i < (int)files.size(); i++)
  131. {
  132. if (MatchesFilter(i))
  133. filtered_tests_indices.push_back(i);
  134. }
  135. }
  136. Rml::String directory;
  137. Rml::StringList files;
  138. int index = 0;
  139. Rml::String filter;
  140. Rml::Vector<int> filtered_tests_indices;
  141. };
  142. using TestSuiteList = Rml::Vector<TestSuite>;
  143. #endif