Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/string/test_fuzzy_search.cpp
45997 views
1
/**************************************************************************/
2
/* test_fuzzy_search.cpp */
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
31
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_fuzzy_search)
34
35
#include "core/io/file_access.h"
36
#include "core/string/fuzzy_search.h"
37
#include "tests/test_utils.h"
38
39
namespace TestFuzzySearch {
40
41
struct FuzzySearchTestCase {
42
String query;
43
String expected;
44
};
45
46
// Ideally each of these test queries should represent a different aspect, and potentially bottleneck, of the search process.
47
const FuzzySearchTestCase test_cases[] = {
48
// Short query, many matches, few adjacent characters
49
{ "///gd", "./menu/hud/hud.gd" },
50
// Filename match with typo
51
{ "sm.png", "./entity/blood_sword/sam.png" },
52
// Multipart filename word matches
53
{ "ham ", "./entity/game_trap/ha_missed_me.wav" },
54
// Single word token matches
55
{ "push background", "./entity/background_zone1/background/push.png" },
56
// Long token matches
57
{ "background_freighter background png", "./entity/background_freighter/background/background.png" },
58
// Many matches, many short tokens
59
{ "menu menu characters wav", "./menu/menu/characters/smoker/0.wav" },
60
// Maximize total matches
61
{ "entity gd", "./entity/entity_man.gd" }
62
};
63
64
Vector<String> load_test_data() {
65
Ref<FileAccess> fp = FileAccess::open(TestUtils::get_data_path("fuzzy_search/project_dir_tree.txt"), FileAccess::READ);
66
REQUIRE(fp.is_valid());
67
return fp->get_as_utf8_string().split("\n");
68
}
69
70
TEST_CASE("[FuzzySearch] Test fuzzy search results") {
71
FuzzySearch search;
72
Vector<FuzzySearchResult> results;
73
Vector<String> targets = load_test_data();
74
75
for (FuzzySearchTestCase test_case : test_cases) {
76
search.set_query(test_case.query);
77
search.search_all(targets, results);
78
CHECK_GT(results.size(), 0);
79
CHECK_EQ(results[0].target, test_case.expected);
80
}
81
}
82
83
} // namespace TestFuzzySearch
84
85