fixed annoying bug for test section manager
[feisty_meow.git] / nucleus / library / tests_configuration / test_section_manager.cpp
1 /*
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.
6 **
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                               *
13 */
14
15 //#define DEBUG_SECTION_MANAGER
16   // uncomment for debugging version.
17
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>
28
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;
39
40 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
41
42 //#ifdef DEBUG_SECTION_MANAGER
43 //  #include <stdio.h>
44 //#endif
45
46 class test_section_manager : public virtual unit_base, virtual public application_shell
47 {
48 public:
49   test_section_manager() {}
50   DEFINE_CLASS_NAME("test_section_manager");
51   virtual int execute();
52 };
53
54 //////////////
55
56 int test_section_manager::execute()
57 {
58   FUNCDEF("execute");
59
60   // prepare the storage area for ini config.
61   environment::set("ALLUSERSPROFILE", environment::get("TEMPORARIES_PILE"));
62
63   {
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....
68     string_array names;
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.
72     }
73
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!");
82     string_table temp_1;
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());
88 #endif
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!");
95     temp_1.reset();
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)!");
99
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!");
108     string_table temp_2;
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)!");
116
117 //more!
118   }    
119   {
120 //    astring TEST = "Second Test";
121   }    
122
123   return final_report();
124 }
125
126 //////////////
127
128 HOOPLE_MAIN(test_section_manager, );
129