TestSuite.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-2023 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 { return directory; }
  40. const Rml::String& GetFilename() const { return files[index]; }
  41. Rml::String GetPath() const { return directory + '/' + files[index]; }
  42. const Rml::String& GetFilter() const { return filter; }
  43. void SetFilter(Rml::String new_filter)
  44. {
  45. filter = new_filter;
  46. UpdateFilteredTests();
  47. SetIndex(index, Direction::Any);
  48. }
  49. bool SetIndex(int new_index, Direction filter_direction = Direction::None)
  50. {
  51. new_index = GetIndexFiltered(new_index, filter_direction);
  52. if (new_index < 0 || new_index >= (int)files.size())
  53. return false;
  54. index = new_index;
  55. return true;
  56. }
  57. bool Next() { return SetIndex(index + 1, Direction::Forward); }
  58. bool Previous() { return SetIndex(index - 1, Direction::Backward); }
  59. int GetIndex() const { return index; }
  60. int GetFilterIndex() const
  61. {
  62. auto it = std::lower_bound(filtered_tests_indices.begin(), filtered_tests_indices.end(), index);
  63. if (it == filtered_tests_indices.end() || *it != index)
  64. return -1;
  65. return int(it - filtered_tests_indices.begin());
  66. }
  67. int GetNumTests() const { return (int)files.size(); }
  68. int GetNumFilteredTests() const { return filter.empty() ? GetNumTests() : (int)filtered_tests_indices.size(); }
  69. private:
  70. int GetIndexFiltered(int new_index, Direction filter_direction) const
  71. {
  72. if (filter_direction == Direction::None || filtered_tests_indices.empty())
  73. return new_index;
  74. auto it = std::lower_bound(filtered_tests_indices.begin(), filtered_tests_indices.end(), new_index);
  75. // Exact match
  76. if (it != filtered_tests_indices.end() && *it == new_index)
  77. return *it;
  78. if (filter_direction == Direction::Forward)
  79. {
  80. if (it != filtered_tests_indices.end())
  81. return *it;
  82. }
  83. else if (filter_direction == Direction::Backward)
  84. {
  85. if (it != filtered_tests_indices.begin())
  86. return *(it - 1);
  87. }
  88. else if (filter_direction == Direction::Any)
  89. {
  90. // Like forward but will go back if we cannot go forward.
  91. return it == filtered_tests_indices.end() ? *(it - 1) : *it;
  92. }
  93. return -1;
  94. }
  95. bool MatchesFilter(int id) const
  96. {
  97. RMLUI_ASSERT(id >= 0 && id < (int)files.size());
  98. return (files[id].find(filter) != Rml::String::npos);
  99. }
  100. void UpdateFilteredTests()
  101. {
  102. filtered_tests_indices.clear();
  103. for (int i = 0; i < (int)files.size(); i++)
  104. {
  105. if (MatchesFilter(i))
  106. filtered_tests_indices.push_back(i);
  107. }
  108. }
  109. Rml::String directory;
  110. Rml::StringList files;
  111. int index = 0;
  112. Rml::String filter;
  113. Rml::Vector<int> filtered_tests_indices;
  114. };
  115. using TestSuiteList = Rml::Vector<TestSuite>;
  116. #endif