1 /*
2 * Copyright (C) 2006-2019 Christopho, Solarus - http://www.solarus-games.org
3 *
4 * Solarus is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Solarus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "solarus/entities/TilesetData.h"
18 #include "solarus/core/CurrentQuest.h"
19 #include "solarus/core/Debug.h"
20 #include "solarus/core/QuestDatabase.h"
21 #include "solarus/core/QuestFiles.h"
22 #include "tools/TestEnvironment.h"
23 #include <iostream>
24
25 using namespace Solarus;
26
27 namespace {
28
29 /**
30 * \brief Checks reading and writing a tileset data file.
31 */
check_tileset(TestEnvironment &,const std::string & tileset_id)32 void check_tileset(TestEnvironment& /* env */, const std::string& tileset_id) {
33
34 TilesetData tileset_data;
35 bool success = false;
36
37 // Import the tileset data file.
38 std::string file_name = "tilesets/" + tileset_id + ".dat";
39 std::string imported_tileset_buffer = QuestFiles::data_file_read(file_name);
40 success = tileset_data.import_from_buffer(imported_tileset_buffer, file_name);
41 Debug::check_assertion(success, "Tileset import failed");
42
43 // Export it.
44 std::string exported_tileset_buffer;
45 success = tileset_data.export_to_buffer(exported_tileset_buffer);
46 Debug::check_assertion(success, "Tileset export failed");
47
48 // Check that the file is identical after import/export.
49 if (exported_tileset_buffer != imported_tileset_buffer) {
50 std::cerr << "Tileset '" << tileset_id << "' differs from the original one after export." << std::endl
51 << "*** Original tileset file:" << std::endl << imported_tileset_buffer << std::endl
52 << "*** Exported tileset file:" << std::endl << exported_tileset_buffer << std::endl;
53 Debug::die("Tileset '" + tileset_id + "': exported file differs from the original one");
54 }
55 }
56
57 }
58
59 /**
60 * \brief Tests reading and writing tileset data files.
61 */
main(int argc,char ** argv)62 int main(int argc, char** argv) {
63
64 TestEnvironment env(argc, argv);
65
66 const std::map<std::string, std::string>& tileset_elements =
67 CurrentQuest::get_database().get_resource_elements(ResourceType::TILESET);
68 Debug::check_assertion(!tileset_elements.empty(), "No tilesets");
69 for (const auto& kvp : tileset_elements) {
70 const std::string& tileset_id = kvp.first;
71 check_tileset(env, tileset_id);
72 }
73
74 return 0;
75 }
76