2 * Name : test_section_manager
3 * Author : Chris Koeritz
4 * Purpose: Tests that the section manager is writing sections properly and keeping its
5 table of contents correctly.
7 * Copyright (c) 2000-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
15 //#define DEBUG_SECTION_MANAGER
16 // uncomment for debugging version.
18 #include <application/hoople_main.h>
19 #include <basis/astring.h>
20 #include <basis/environment.h>
21 #include <basis/guards.h>
22 #include <configuration/ini_configurator.h>
23 #include <configuration/section_manager.h>
24 #include <structures/string_table.h>
25 #include <structures/string_array.h>
26 #include <structures/static_memory_gremlin.h>
27 #include <unit_test/unit_base.h>
29 using namespace application;
30 using namespace basis;
31 using namespace configuration;
32 using namespace filesystem;
33 using namespace loggers;
34 using namespace mathematics;
35 using namespace structures;
36 using namespace textual;
37 using namespace timely;
38 using namespace unit_test;
40 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
42 //#ifdef DEBUG_SECTION_MANAGER
46 class test_section_manager : public virtual unit_base, virtual public application_shell
49 test_section_manager() {}
50 DEFINE_CLASS_NAME("test_section_manager");
51 virtual int execute();
56 int test_section_manager::execute()
60 // prepare the storage area for ini config.
61 environment::set("ALLUSERSPROFILE", environment::get("TEMPORARIES_PILE"));
64 astring TEST = "First Test";
65 ini_configurator ini("t_section_manager_1.ini", ini_configurator::AUTO_STORE);
66 section_manager mangler(ini, "TOC", "BORK:");
67 // clean up the ini file for our testing....
69 if (mangler.get_section_names(names)) {
70 for (int i = 0; i < names.length(); i++) mangler.zap_section(names[i]);
71 ini.delete_section("TOC"); // remove table of contents.
74 // now add some entries...
75 string_table contents1;
76 contents1.add("oink", "bozoot");
77 contents1.add("gink", "rinkum");
78 contents1.add("sorty", "figulat");
79 contents1.add("crinkish", "wazir");
80 ASSERT_TRUE(mangler.add_section("burny", contents1),
81 TEST + ": couldn't add the first section!");
83 ASSERT_TRUE(mangler.find_section("burny", temp_1),
84 TEST + ": couldn't retrieve the first section!");
85 #ifdef DEBUG_SECTION_MANAGER
86 printf("first section has:\n%s\n", temp_1.text_form().s());
87 printf("we want:\n%s\n", contents1.text_form().s());
89 ASSERT_EQUAL(temp_1, contents1, TEST + ": first section's contents are incorrect!");
90 contents1.add("glurp", "locutusburger");
91 ASSERT_FALSE(mangler.add_section("burny", contents1),
92 TEST + ": incorrectly allowing re-add of first section!");
93 ASSERT_TRUE(mangler.replace_section("burny", contents1),
94 TEST + ": failing to replace first section!");
96 ASSERT_TRUE(mangler.find_section("burny", temp_1),
97 TEST + ": couldn't retrieve the first section (2)!");
98 ASSERT_EQUAL(temp_1, contents1, TEST + ": first section's contents are incorrect (2)!");
100 string_table contents2;
101 contents2.add("tsingha", "tsinglo");
102 contents2.add("chunk", "midgets");
103 contents2.add("burn", "barns in texas");
104 contents2.add("chump", "will not be elected");
105 contents2.add("geezerplant", "water weekly");
106 ASSERT_TRUE(mangler.add_section("itchy", contents2),
107 TEST + ": couldn't add the second section!");
109 ASSERT_TRUE(mangler.find_section("itchy", temp_2),
110 TEST + ": couldn't retrieve the second section!");
111 ASSERT_EQUAL(temp_2, contents2, TEST + ": second section's contents are incorrect!");
112 // test that first section is still there with second having been added.
113 ASSERT_TRUE(mangler.find_section("burny", temp_1),
114 TEST + ": couldn't retrieve the first section (3)!");
115 ASSERT_EQUAL(temp_1, contents1, TEST + ": first section's contents are incorrect (3)!");
120 // astring TEST = "Second Test";
123 return final_report();
128 HOOPLE_MAIN(test_section_manager, );