tests/test_config_cache.cpp

Go to the documentation of this file.
00001 /* $Id: test_config_cache.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Pauli Nieminen <paniemin@cc.hut.fi>
00004    Part of thie Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #include <boost/test/unit_test.hpp>
00017 
00018 #define GETTEXT_DOMAIN "wesnoth-test"
00019 
00020 
00021 #include "config_cache.hpp"
00022 #include "config.hpp"
00023 #include "game_config.hpp"
00024 #include "language.hpp"
00025 #include "version.hpp"
00026 
00027 #include "tests/utils/game_config_manager.hpp"
00028 
00029 #include <boost/bind.hpp>
00030 
00031 
00032 static preproc_map setup_test_preproc_map()
00033 {
00034     preproc_map defines_map;
00035 
00036     if (game_config::small_gui)
00037         defines_map["SMALL_GUI"] = preproc_define();
00038 
00039 #if defined(__APPLE__)
00040     defines_map["APPLE"] = preproc_define();
00041 #endif
00042 
00043     defines_map["WESNOTH_VERSION"] = preproc_define(game_config::wesnoth_version.str());
00044 
00045     return defines_map;
00046 
00047 }
00048 
00049 
00050 /**
00051  * Used to make distinct singleton for testing it
00052  * because other tests will need original one to load data
00053  **/
00054 class test_config_cache : public game_config::config_cache {
00055     test_config_cache() : game_config::config_cache() {}
00056 
00057     public:
00058     static test_config_cache& instance() ;
00059 
00060     void set_force_invalid_cache(bool force)
00061     {
00062         game_config::config_cache::set_force_invalid_cache(force);
00063     }
00064 };
00065 
00066 test_config_cache & test_config_cache::instance() {
00067     static test_config_cache * cache_  = new test_config_cache;
00068     return *cache_;
00069 }
00070 
00071 /**
00072  * Used to redirect defines settings to test cache
00073  **/
00074 typedef game_config::scoped_preproc_define_internal<test_config_cache> test_scoped_define;
00075 
00076 struct config_cache_fixture {
00077     config_cache_fixture() : cache(test_config_cache::instance()), old_locale(get_language()), test_def("TEST")
00078     {
00079         test_utils::get_test_config_ref();
00080     }
00081     ~config_cache_fixture()
00082     {
00083         set_language(old_locale);
00084     }
00085     test_config_cache& cache;
00086     language_def old_locale;
00087     test_scoped_define test_def;
00088 };
00089 
00090 BOOST_AUTO_TEST_CASE( test_preproc_defines )
00091 {
00092     test_config_cache& cache = test_config_cache::instance();
00093     const preproc_map& test_defines = cache.get_preproc_map();
00094     preproc_map defines_map(setup_test_preproc_map());
00095 
00096     // check initial state
00097     BOOST_REQUIRE_EQUAL_COLLECTIONS(test_defines.begin(),test_defines.end(),
00098             defines_map.begin() ,defines_map.end());
00099 
00100     // scoped
00101     {
00102         test_scoped_define test("TEST");
00103         defines_map["TEST"] = preproc_define();
00104 
00105         BOOST_CHECK_EQUAL_COLLECTIONS(test_defines.begin(),test_defines.end(),
00106                 defines_map.begin() ,defines_map.end());
00107         defines_map.erase("TEST");
00108     }
00109     // Check scoped remove
00110 
00111     BOOST_CHECK_EQUAL_COLLECTIONS(test_defines.begin(),test_defines.end(),
00112             defines_map.begin() ,defines_map.end());
00113 
00114     // Manual add define
00115     cache.add_define("TEST");
00116     defines_map["TEST"] = preproc_define();
00117     BOOST_CHECK_EQUAL_COLLECTIONS(test_defines.begin(),test_defines.end(),
00118             defines_map.begin() ,defines_map.end());
00119 
00120     // Manual remove define
00121     cache.remove_define("TEST");
00122     defines_map.erase("TEST");
00123     BOOST_CHECK_EQUAL_COLLECTIONS(test_defines.begin(),test_defines.end(),
00124             defines_map.begin() ,defines_map.end());
00125 }
00126 
00127 BOOST_AUTO_TEST_CASE( test_config_cache_defaults )
00128 {
00129     test_config_cache& cache = test_config_cache::instance();
00130     preproc_map defines_map(setup_test_preproc_map());
00131 
00132     const preproc_map& test_defines = cache.get_preproc_map();
00133     BOOST_CHECK_EQUAL_COLLECTIONS(test_defines.begin(),test_defines.end(),
00134             defines_map.begin() ,defines_map.end());
00135 }
00136 
00137 
00138 BOOST_FIXTURE_TEST_SUITE( config_cache, config_cache_fixture )
00139 
00140     const std::string test_data_path("data/test/test/_main.cfg");
00141 
00142 static config setup_test_config()
00143 {
00144     config test_config;
00145     config* child = &test_config.add_child("textdomain");
00146     (*child)["name"] = GETTEXT_DOMAIN;
00147 
00148     child = &test_config.add_child("test_key");
00149     (*child)["define"] = "test";
00150     return test_config;
00151 }
00152 
00153 
00154 BOOST_AUTO_TEST_CASE( test_load_config )
00155 {
00156 
00157     config test_config = setup_test_config();
00158     config cached_config;
00159     cache.get_config(test_data_path, cached_config);
00160     BOOST_CHECK_EQUAL(test_config, cached_config);
00161 
00162     config &child = test_config.add_child("test_key2");
00163     child["define"] = t_string("testing translation reset.", GETTEXT_DOMAIN);
00164 
00165 
00166     test_scoped_define test_define_def("TEST_DEFINE");
00167     cached_config.clear();
00168     cache.get_config(test_data_path, cached_config);
00169     BOOST_CHECK_EQUAL(test_config, cached_config);
00170 
00171     BOOST_CHECK_EQUAL(test_config.child("test_key2")["define"].str(), cached_config.child("test_key2")["define"].str());
00172 }
00173 
00174 BOOST_AUTO_TEST_CASE( test_non_clean_config_loading )
00175 {
00176 
00177     config test_config = setup_test_config();
00178 
00179     // Test clean load first
00180     {
00181         config cfg;
00182         cache.get_config(test_data_path, cfg);
00183         BOOST_CHECK_EQUAL(test_config, cfg);
00184     }
00185 
00186     // test non-clean one then
00187     {
00188         config cfg;
00189         config &child = cfg.add_child("junk_data");
00190         child["some_junk"] = "hah";
00191         cache.get_config(test_data_path, cfg);
00192         BOOST_CHECK_EQUAL(test_config, cfg);
00193     }
00194 }
00195 
00196 BOOST_AUTO_TEST_CASE( test_macrosubstitution )
00197 {
00198     config test_config = setup_test_config();
00199 
00200     config &child = test_config.add_child("test_key3");
00201     child["define"] = "transaction";
00202     config &child2 = test_config.add_child("test_key4");
00203     child2["defined"] = "parameter";
00204 
00205     // test first that macro loading works
00206     test_scoped_define macro("TEST_MACRO");
00207 
00208     // Without cache
00209     config cached_config;
00210     cache.get_config(test_data_path, cached_config);
00211     BOOST_CHECK_EQUAL(test_config, cached_config);
00212 
00213     // With cache
00214     cached_config.clear();
00215     cache.get_config(test_data_path, cached_config);
00216     BOOST_CHECK_EQUAL(test_config, cached_config);
00217 
00218 
00219 }
00220 
00221 BOOST_AUTO_TEST_CASE( test_transaction )
00222 {
00223     config test_config = setup_test_config();
00224 
00225     config* child = &test_config.add_child("test_key3");
00226     (*child)["define"] = "transaction";
00227     child = &test_config.add_child("test_key4");
00228     (*child)["defined"] = "parameter";
00229 
00230     // test first that macro loading works
00231     test_scoped_define macro("TEST_MACRO");
00232 
00233     //Start transaction
00234 
00235     game_config::config_cache_transaction transaction;
00236 
00237     config cached_config;
00238     cache.get_config(test_data_path, cached_config);
00239     BOOST_CHECK_EQUAL(test_config, cached_config);
00240 
00241     transaction.lock();
00242     config umc_config;
00243     child = &umc_config.add_child("umc");
00244     (*child)["test"] = "umc load";
00245     child = &umc_config.add_child("test_key3");
00246     (*child)["define"] = "transaction";
00247     child = &umc_config.add_child("test_key4");
00248     (*child)["defined"] = "parameter";
00249     cached_config.clear();
00250     cache.get_config("data/test/test/umc.cfg", cached_config);
00251     BOOST_CHECK_EQUAL(umc_config, cached_config);
00252 }
00253 
00254 BOOST_AUTO_TEST_CASE( test_define_loading )
00255 {
00256     // try to load umc without valid cache
00257     config test_config = setup_test_config();
00258 
00259     config* child = &test_config.add_child("test_key3");
00260     (*child)["define"] = "transaction";
00261     child = &test_config.add_child("test_key4");
00262     (*child)["defined"] = "parameter";
00263 
00264     // test first that macro loading works
00265     test_scoped_define macro("TEST_MACRO");
00266 
00267     //Start transaction
00268 
00269     game_config::config_cache_transaction transaction;
00270 
00271     config cached_config;
00272     cache.get_config(test_data_path, cached_config);
00273     BOOST_CHECK_EQUAL(test_config, cached_config);
00274 
00275     transaction.lock();
00276 
00277     cache.set_force_invalid_cache(true);
00278     config umc_config;
00279     child = &umc_config.add_child("umc");
00280     (*child)["test"] = "umc load";
00281     child = &umc_config.add_child("test_key3");
00282     (*child)["define"] = "transaction";
00283     child = &umc_config.add_child("test_key4");
00284     (*child)["defined"] = "parameter";
00285     cached_config.clear();
00286     cache.get_config("data/test/test/umc.cfg", cached_config);
00287     BOOST_CHECK_EQUAL(umc_config, cached_config);
00288     cache.set_force_invalid_cache(false);
00289 }
00290 
00291 BOOST_AUTO_TEST_CASE( test_lead_spaces_loading )
00292 {
00293     config test_config;
00294     test_config.add_child("test_lead_space")["space"] = "empty char in middle";
00295     // Force reload of cache
00296     cache.set_force_invalid_cache(true);
00297     config cached_config;
00298     cache.get_config("data/test/test/leading_space.cfg", cached_config);
00299     BOOST_CHECK_EQUAL(test_config, cached_config);
00300     cache.set_force_invalid_cache(false);
00301     cached_config.clear();
00302     cache.get_config("data/test/test/leading_space.cfg", cached_config);
00303     BOOST_CHECK_EQUAL(test_config, cached_config);
00304 }
00305 
00306 #if 0
00307 // for profiling cache speed
00308 BOOST_AUTO_TEST_CASE( test_performance )
00309 {
00310     test_scoped_define mp("MULTIPLAYER");
00311     config cfg_ref;
00312 //  cache.set_force_invalid_cache(true);
00313     cache.get_config("data/", cfg_ref);
00314 //  cache.set_force_invalid_cache(false);
00315     for (int i=0; i < 3; ++i)
00316     {
00317         cache.get_config("data/");
00318     }
00319 }
00320 #endif
00321 
00322 /* vim: set ts=4 sw=4: */
00323 BOOST_AUTO_TEST_SUITE_END()
00324 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:12 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs