Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_config_file.cpp
45997 views
1
/**************************************************************************/
2
/* test_config_file.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_config_file)
34
35
#include "core/io/config_file.h"
36
37
#ifdef WINDOWS_ENABLED
38
#include "core/os/os.h"
39
#endif
40
41
namespace TestConfigFile {
42
43
TEST_CASE("[ConfigFile] Parsing well-formatted files") {
44
ConfigFile config_file;
45
// Formatting is intentionally hand-edited to see how human-friendly the parser is.
46
const Error error = config_file.parse(R"(
47
[player]
48
49
name = "Unnamed Player"
50
tagline="Waiting
51
for
52
Godot"
53
54
color =Color( 0, 0.5,1, 1) ; Inline comment
55
position= Vector2(
56
3,
57
4
58
)
59
60
[graphics]
61
62
antialiasing = true
63
64
; Testing comments and case-sensitivity...
65
antiAliasing = false
66
)");
67
68
CHECK_MESSAGE(error == OK, "The configuration file should parse successfully.");
69
CHECK_MESSAGE(
70
String(config_file.get_value("player", "name")) == "Unnamed Player",
71
"Reading `player/name` should return the expected value.");
72
CHECK_MESSAGE(
73
String(config_file.get_value("player", "tagline")) == "Waiting\nfor\nGodot",
74
"Reading `player/tagline` should return the expected value.");
75
CHECK_MESSAGE(
76
Color(config_file.get_value("player", "color")).is_equal_approx(Color(0, 0.5, 1)),
77
"Reading `player/color` should return the expected value.");
78
CHECK_MESSAGE(
79
Vector2(config_file.get_value("player", "position")).is_equal_approx(Vector2(3, 4)),
80
"Reading `player/position` should return the expected value.");
81
CHECK_MESSAGE(
82
bool(config_file.get_value("graphics", "antialiasing")),
83
"Reading `graphics/antialiasing` should return `true`.");
84
CHECK_MESSAGE(
85
bool(config_file.get_value("graphics", "antiAliasing")) == false,
86
"Reading `graphics/antiAliasing` should return `false`.");
87
88
// An empty ConfigFile is valid.
89
const Error error_empty = config_file.parse("");
90
CHECK_MESSAGE(error_empty == OK,
91
"An empty configuration file should parse successfully.");
92
}
93
94
TEST_CASE("[ConfigFile] Parsing malformatted file") {
95
ConfigFile config_file;
96
ERR_PRINT_OFF;
97
const Error error = config_file.parse(R"(
98
[player]
99
100
name = "Unnamed Player"" ; Extraneous closing quote.
101
tagline = "Waiting\nfor\nGodot"
102
103
color = Color(0, 0.5, 1) ; Missing 4th parameter.
104
position = Vector2(
105
3,,
106
4
107
) ; Extraneous comma.
108
109
[graphics]
110
111
antialiasing = true
112
antialiasing = false ; Duplicate key.
113
)");
114
ERR_PRINT_ON;
115
116
CHECK_MESSAGE(error == ERR_PARSE_ERROR,
117
"The configuration file shouldn't parse successfully.");
118
}
119
120
TEST_CASE("[ConfigFile] Saving file") {
121
ConfigFile config_file;
122
config_file.set_value("player", "name", "Unnamed Player");
123
config_file.set_value("player", "tagline", "Waiting\nfor\nGodot");
124
config_file.set_value("player", "color", Color(0, 0.5, 1));
125
config_file.set_value("player", "position", Vector2(3, 4));
126
config_file.set_value("graphics", "antialiasing", true);
127
config_file.set_value("graphics", "antiAliasing", false);
128
config_file.set_value("quoted", String::utf8("静音"), 42);
129
config_file.set_value("quoted", "a=b", 7);
130
131
#ifdef WINDOWS_ENABLED
132
const String config_path = OS::get_singleton()->get_environment("TEMP").path_join("config.ini");
133
#else
134
const String config_path = "/tmp/config.ini";
135
#endif
136
137
config_file.save(config_path);
138
139
// Expected contents of the saved ConfigFile.
140
const String contents = String::utf8(R"([player]
141
142
name="Unnamed Player"
143
tagline="Waiting
144
for
145
Godot"
146
color=Color(0, 0.5, 1, 1)
147
position=Vector2(3, 4)
148
149
[graphics]
150
151
antialiasing=true
152
antiAliasing=false
153
154
[quoted]
155
156
"静音"=42
157
"a=b"=7
158
)");
159
160
Ref<FileAccess> file = FileAccess::open(config_path, FileAccess::READ);
161
CHECK_MESSAGE(file->get_as_utf8_string() == contents,
162
"The saved configuration file should match the expected format.");
163
}
164
165
} // namespace TestConfigFile
166
167