test_fuzzy_search.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**************************************************************************/
  2. /* test_fuzzy_search.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_FUZZY_SEARCH_H
  31. #define TEST_FUZZY_SEARCH_H
  32. #include "core/string/fuzzy_search.h"
  33. #include "tests/test_macros.h"
  34. namespace TestFuzzySearch {
  35. struct FuzzySearchTestCase {
  36. String query;
  37. String expected;
  38. };
  39. // Ideally each of these test queries should represent a different aspect, and potentially bottleneck, of the search process.
  40. const FuzzySearchTestCase test_cases[] = {
  41. // Short query, many matches, few adjacent characters
  42. { "///gd", "./menu/hud/hud.gd" },
  43. // Filename match with typo
  44. { "sm.png", "./entity/blood_sword/sam.png" },
  45. // Multipart filename word matches
  46. { "ham ", "./entity/game_trap/ha_missed_me.wav" },
  47. // Single word token matches
  48. { "push background", "./entity/background_zone1/background/push.png" },
  49. // Long token matches
  50. { "background_freighter background png", "./entity/background_freighter/background/background.png" },
  51. // Many matches, many short tokens
  52. { "menu menu characters wav", "./menu/menu/characters/smoker/0.wav" },
  53. // Maximize total matches
  54. { "entity gd", "./entity/entity_man.gd" }
  55. };
  56. Vector<String> load_test_data() {
  57. Ref<FileAccess> fp = FileAccess::open(TestUtils::get_data_path("fuzzy_search/project_dir_tree.txt"), FileAccess::READ);
  58. REQUIRE(fp.is_valid());
  59. return fp->get_as_utf8_string().split("\n");
  60. }
  61. TEST_CASE("[FuzzySearch] Test fuzzy search results") {
  62. FuzzySearch search;
  63. Vector<FuzzySearchResult> results;
  64. Vector<String> targets = load_test_data();
  65. for (FuzzySearchTestCase test_case : test_cases) {
  66. search.set_query(test_case.query);
  67. search.search_all(targets, results);
  68. CHECK_GT(results.size(), 0);
  69. CHECK_EQ(results[0].target, test_case.expected);
  70. }
  71. }
  72. } //namespace TestFuzzySearch
  73. #endif // TEST_FUZZY_SEARCH_H