Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/config/test_project_settings.h
10278 views
1
/**************************************************************************/
2
/* test_project_settings.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/config/project_settings.h"
34
#include "core/io/dir_access.h"
35
#include "core/variant/variant.h"
36
#include "tests/test_macros.h"
37
38
class TestProjectSettingsInternalsAccessor {
39
public:
40
static String &resource_path() {
41
return ProjectSettings::get_singleton()->resource_path;
42
}
43
};
44
45
namespace TestProjectSettings {
46
47
TEST_CASE("[ProjectSettings] Get existing setting") {
48
CHECK(ProjectSettings::get_singleton()->has_setting("application/run/main_scene"));
49
50
Variant variant = ProjectSettings::get_singleton()->get_setting("application/run/main_scene");
51
CHECK_EQ(variant.get_type(), Variant::STRING);
52
53
String name = variant;
54
CHECK_EQ(name, String());
55
}
56
57
TEST_CASE("[ProjectSettings] Default value is ignored if setting exists") {
58
CHECK(ProjectSettings::get_singleton()->has_setting("application/run/main_scene"));
59
60
Variant variant = ProjectSettings::get_singleton()->get_setting("application/run/main_scene", "SomeDefaultValue");
61
CHECK_EQ(variant.get_type(), Variant::STRING);
62
63
String name = variant;
64
CHECK_EQ(name, String());
65
}
66
67
TEST_CASE("[ProjectSettings] Non existing setting is null") {
68
CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting"));
69
70
Variant variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting");
71
CHECK_EQ(variant.get_type(), Variant::NIL);
72
}
73
74
TEST_CASE("[ProjectSettings] Non existing setting should return default value") {
75
CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting"));
76
77
Variant variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting");
78
CHECK_EQ(variant.get_type(), Variant::NIL);
79
80
variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting", "my_nice_default_value");
81
CHECK_EQ(variant.get_type(), Variant::STRING);
82
83
String name = variant;
84
CHECK_EQ(name, "my_nice_default_value");
85
86
CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting"));
87
}
88
89
TEST_CASE("[ProjectSettings] Set value should be returned when retrieved") {
90
CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("my_custom_setting"));
91
92
Variant variant = ProjectSettings::get_singleton()->get_setting("my_custom_setting");
93
CHECK_EQ(variant.get_type(), Variant::NIL);
94
95
ProjectSettings::get_singleton()->set_setting("my_custom_setting", true);
96
CHECK(ProjectSettings::get_singleton()->has_setting("my_custom_setting"));
97
98
variant = ProjectSettings::get_singleton()->get_setting("my_custom_setting");
99
CHECK_EQ(variant.get_type(), Variant::BOOL);
100
101
bool value = variant;
102
CHECK_EQ(true, value);
103
104
CHECK(ProjectSettings::get_singleton()->has_setting("my_custom_setting"));
105
}
106
107
TEST_CASE("[ProjectSettings] localize_path") {
108
String old_resource_path = TestProjectSettingsInternalsAccessor::resource_path();
109
TestProjectSettingsInternalsAccessor::resource_path() = DirAccess::create(DirAccess::ACCESS_FILESYSTEM)->get_current_dir();
110
String root_path = ProjectSettings::get_singleton()->get_resource_path();
111
#ifdef WINDOWS_ENABLED
112
String root_path_win = ProjectSettings::get_singleton()->get_resource_path().replace_char('/', '\\');
113
#endif
114
115
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("filename"), "res://filename");
116
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path/filename"), "res://path/filename");
117
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path/something/../filename"), "res://path/filename");
118
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path/./filename"), "res://path/filename");
119
#ifdef WINDOWS_ENABLED
120
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path\\filename"), "res://path/filename");
121
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path\\something\\..\\filename"), "res://path/filename");
122
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path\\.\\filename"), "res://path/filename");
123
#endif
124
125
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("../filename"), "../filename");
126
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("../path/filename"), "../path/filename");
127
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("..\\path\\filename"), "../path/filename");
128
129
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/filename"), "/testroot/filename");
130
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/path/filename"), "/testroot/path/filename");
131
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/path/something/../filename"), "/testroot/path/filename");
132
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/path/./filename"), "/testroot/path/filename");
133
#ifdef WINDOWS_ENABLED
134
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/filename"), "C:/testroot/filename");
135
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/path/filename"), "C:/testroot/path/filename");
136
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/path/something/../filename"), "C:/testroot/path/filename");
137
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/path/./filename"), "C:/testroot/path/filename");
138
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\filename"), "C:/testroot/filename");
139
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\path\\filename"), "C:/testroot/path/filename");
140
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\path\\something\\..\\filename"), "C:/testroot/path/filename");
141
CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\path\\.\\filename"), "C:/testroot/path/filename");
142
#endif
143
144
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/filename"), "res://filename");
145
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/path/filename"), "res://path/filename");
146
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/path/something/../filename"), "res://path/filename");
147
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/path/./filename"), "res://path/filename");
148
#ifdef WINDOWS_ENABLED
149
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\filename"), "res://filename");
150
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\path\\filename"), "res://path/filename");
151
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\path\\something\\..\\filename"), "res://path/filename");
152
CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\path\\.\\filename"), "res://path/filename");
153
#endif
154
155
TestProjectSettingsInternalsAccessor::resource_path() = old_resource_path;
156
}
157
158
} // namespace TestProjectSettings
159
160