Path: blob/master/tests/core/io/test_config_file.cpp
45997 views
/**************************************************************************/1/* test_config_file.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_config_file)3334#include "core/io/config_file.h"3536#ifdef WINDOWS_ENABLED37#include "core/os/os.h"38#endif3940namespace TestConfigFile {4142TEST_CASE("[ConfigFile] Parsing well-formatted files") {43ConfigFile config_file;44// Formatting is intentionally hand-edited to see how human-friendly the parser is.45const Error error = config_file.parse(R"(46[player]4748name = "Unnamed Player"49tagline="Waiting50for51Godot"5253color =Color( 0, 0.5,1, 1) ; Inline comment54position= Vector2(553,56457)5859[graphics]6061antialiasing = true6263; Testing comments and case-sensitivity...64antiAliasing = false65)");6667CHECK_MESSAGE(error == OK, "The configuration file should parse successfully.");68CHECK_MESSAGE(69String(config_file.get_value("player", "name")) == "Unnamed Player",70"Reading `player/name` should return the expected value.");71CHECK_MESSAGE(72String(config_file.get_value("player", "tagline")) == "Waiting\nfor\nGodot",73"Reading `player/tagline` should return the expected value.");74CHECK_MESSAGE(75Color(config_file.get_value("player", "color")).is_equal_approx(Color(0, 0.5, 1)),76"Reading `player/color` should return the expected value.");77CHECK_MESSAGE(78Vector2(config_file.get_value("player", "position")).is_equal_approx(Vector2(3, 4)),79"Reading `player/position` should return the expected value.");80CHECK_MESSAGE(81bool(config_file.get_value("graphics", "antialiasing")),82"Reading `graphics/antialiasing` should return `true`.");83CHECK_MESSAGE(84bool(config_file.get_value("graphics", "antiAliasing")) == false,85"Reading `graphics/antiAliasing` should return `false`.");8687// An empty ConfigFile is valid.88const Error error_empty = config_file.parse("");89CHECK_MESSAGE(error_empty == OK,90"An empty configuration file should parse successfully.");91}9293TEST_CASE("[ConfigFile] Parsing malformatted file") {94ConfigFile config_file;95ERR_PRINT_OFF;96const Error error = config_file.parse(R"(97[player]9899name = "Unnamed Player"" ; Extraneous closing quote.100tagline = "Waiting\nfor\nGodot"101102color = Color(0, 0.5, 1) ; Missing 4th parameter.103position = Vector2(1043,,1054106) ; Extraneous comma.107108[graphics]109110antialiasing = true111antialiasing = false ; Duplicate key.112)");113ERR_PRINT_ON;114115CHECK_MESSAGE(error == ERR_PARSE_ERROR,116"The configuration file shouldn't parse successfully.");117}118119TEST_CASE("[ConfigFile] Saving file") {120ConfigFile config_file;121config_file.set_value("player", "name", "Unnamed Player");122config_file.set_value("player", "tagline", "Waiting\nfor\nGodot");123config_file.set_value("player", "color", Color(0, 0.5, 1));124config_file.set_value("player", "position", Vector2(3, 4));125config_file.set_value("graphics", "antialiasing", true);126config_file.set_value("graphics", "antiAliasing", false);127config_file.set_value("quoted", String::utf8("静音"), 42);128config_file.set_value("quoted", "a=b", 7);129130#ifdef WINDOWS_ENABLED131const String config_path = OS::get_singleton()->get_environment("TEMP").path_join("config.ini");132#else133const String config_path = "/tmp/config.ini";134#endif135136config_file.save(config_path);137138// Expected contents of the saved ConfigFile.139const String contents = String::utf8(R"([player]140141name="Unnamed Player"142tagline="Waiting143for144Godot"145color=Color(0, 0.5, 1, 1)146position=Vector2(3, 4)147148[graphics]149150antialiasing=true151antiAliasing=false152153[quoted]154155"静音"=42156"a=b"=7157)");158159Ref<FileAccess> file = FileAccess::open(config_path, FileAccess::READ);160CHECK_MESSAGE(file->get_as_utf8_string() == contents,161"The saved configuration file should match the expected format.");162}163164} // namespace TestConfigFile165166167