2
0

test_fuzzy_search.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #pragma once
  31. #include "core/string/fuzzy_search.h"
  32. #include "tests/test_macros.h"
  33. namespace TestFuzzySearch {
  34. struct FuzzySearchTestCase {
  35. String query;
  36. String expected;
  37. };
  38. // Ideally each of these test queries should represent a different aspect, and potentially bottleneck, of the search process.
  39. const FuzzySearchTestCase test_cases[] = {
  40. // Short query, many matches, few adjacent characters
  41. { "///gd", "./menu/hud/hud.gd" },
  42. // Filename match with typo
  43. { "sm.png", "./entity/blood_sword/sam.png" },
  44. // Multipart filename word matches
  45. { "ham ", "./entity/game_trap/ha_missed_me.wav" },
  46. // Single word token matches
  47. { "push background", "./entity/background_zone1/background/push.png" },
  48. // Long token matches
  49. { "background_freighter background png", "./entity/background_freighter/background/background.png" },
  50. // Many matches, many short tokens
  51. { "menu menu characters wav", "./menu/menu/characters/smoker/0.wav" },
  52. // Maximize total matches
  53. { "entity gd", "./entity/entity_man.gd" }
  54. };
  55. Vector<String> load_test_data() {
  56. Ref<FileAccess> fp = FileAccess::open(TestUtils::get_data_path("fuzzy_search/project_dir_tree.txt"), FileAccess::READ);
  57. REQUIRE(fp.is_valid());
  58. return fp->get_as_utf8_string().split("\n");
  59. }
  60. TEST_CASE("[FuzzySearch] Test fuzzy search results") {
  61. FuzzySearch search;
  62. Vector<FuzzySearchResult> results;
  63. Vector<String> targets = load_test_data();
  64. for (FuzzySearchTestCase test_case : test_cases) {
  65. search.set_query(test_case.query);
  66. search.search_all(targets, results);
  67. CHECK_GT(results.size(), 0);
  68. CHECK_EQ(results[0].target, test_case.expected);
  69. }
  70. }
  71. } //namespace TestFuzzySearch