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