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