| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |