c8358237a86dc2b575dec790558bacc0cad3ebfa
[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/guards.h>
21 #include <configuration/ini_configurator.h>
22 #include <configuration/section_manager.h>
23 #include <structures/string_table.h>
24 #include <structures/string_array.h>
25 #include <structures/static_memory_gremlin.h>
26 #include <unit_test/unit_base.h>
27
28 using namespace application;
29 using namespace basis;
30 using namespace configuration;
31 using namespace filesystem;
32 using namespace loggers;
33 using namespace mathematics;
34 using namespace structures;
35 using namespace textual;
36 using namespace timely;
37 using namespace unit_test;
38
39 #define LOG(to_print) EMERGENCY_LOG(program_wide_logger().get(), astring(to_print))
40
41 //#ifdef DEBUG_SECTION_MANAGER
42 //  #include <stdio.h>
43 //#endif
44
45 class test_section_manager : public virtual unit_base, virtual public application_shell
46 {
47 public:
48   test_section_manager() {}
49   DEFINE_CLASS_NAME("test_section_manager");
50   virtual int execute();
51 };
52
53 //////////////
54
55 int test_section_manager::execute()
56 {
57   FUNCDEF("execute");
58   {
59     astring TEST = "First Test";
60     ini_configurator ini("t_section_manager_1.ini", ini_configurator::AUTO_STORE);
61     section_manager mangler(ini, "TOC", "BORK:");
62     // clean up the ini file for our testing....
63     string_array names;
64     if (mangler.get_section_names(names)) {
65       for (int i = 0; i < names.length(); i++) mangler.zap_section(names[i]);
66       ini.delete_section("TOC");  // remove table of contents.
67     }
68
69     // now add some entries...
70     string_table contents1;
71     contents1.add("oink", "bozoot");
72     contents1.add("gink", "rinkum");
73     contents1.add("sorty", "figulat");
74     contents1.add("crinkish", "wazir");
75     ASSERT_TRUE(mangler.add_section("burny", contents1),
76         TEST + ": couldn't add the first section!");
77     string_table temp_1;
78     ASSERT_TRUE(mangler.find_section("burny", temp_1),
79         TEST + ": couldn't retrieve the first section!");
80 #ifdef DEBUG_SECTION_MANAGER
81     printf("first section has:\n%s\n", temp_1.text_form().s());
82     printf("we want:\n%s\n", contents1.text_form().s());
83 #endif
84     ASSERT_EQUAL(temp_1, contents1, TEST + ": first section's contents are incorrect!");
85     contents1.add("glurp", "locutusburger");
86     ASSERT_FALSE(mangler.add_section("burny", contents1),
87         TEST + ": incorrectly allowing re-add of first section!");
88     ASSERT_TRUE(mangler.replace_section("burny", contents1),
89         TEST + ": failing to replace first section!");
90     temp_1.reset();
91     ASSERT_TRUE(mangler.find_section("burny", temp_1),
92         TEST + ": couldn't retrieve the first section (2)!");
93     ASSERT_EQUAL(temp_1, contents1, TEST + ": first section's contents are incorrect (2)!");
94
95     string_table contents2;
96     contents2.add("tsingha", "tsinglo");
97     contents2.add("chunk", "midgets");
98     contents2.add("burn", "barns in texas");
99     contents2.add("chump", "will not be elected");
100     contents2.add("geezerplant", "water weekly");
101     ASSERT_TRUE(mangler.add_section("itchy", contents2),
102         TEST + ": couldn't add the second section!");
103     string_table temp_2;
104     ASSERT_TRUE(mangler.find_section("itchy", temp_2),
105         TEST + ": couldn't retrieve the second section!");
106     ASSERT_EQUAL(temp_2, contents2, TEST + ": second section's contents are incorrect!");
107     // test that first section is still there with second having been added.
108     ASSERT_TRUE(mangler.find_section("burny", temp_1),
109         TEST + ": couldn't retrieve the first section (3)!");
110     ASSERT_EQUAL(temp_1, contents1, TEST + ": first section's contents are incorrect (3)!");
111
112 //more!
113   }    
114   {
115 //    astring TEST = "Second Test";
116   }    
117
118   return final_report();
119 }
120
121 //////////////
122
123 HOOPLE_MAIN(test_section_manager, );
124