Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestConfig.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 
00020 #include "Basics.H"
00021 #include "OS.H"
00022 #include "VestaConfig.H"
00023 
00024 using std::cout;
00025 
00026 const char nl = '\n';
00027   
00028 void report_location()
00029 {
00030   try {
00031     Text loc(VestaConfig::get_location());
00032 
00033     cout << "Configuration file found at " << loc.cchars() << nl;
00034   } catch (VestaConfig::failure f) { cout << f.msg <<  nl; }
00035 }
00036 
00037 void set_location(const Text &loc)
00038 {
00039   cout << nl;
00040   cout << "Setting configuration file to " << loc << "...";
00041   try {
00042     VestaConfig::set_location(loc);
00043     cout << "ok\n";
00044   } catch (VestaConfig::failure f) { cout << f.msg <<  nl; }
00045 }
00046 
00047 void print_expected(const char *result)
00048 {
00049   cout << nl;
00050   if (result == NULL) cout << "  Expected lookup error\n";
00051   else cout << "  Expected: " << result << nl;
00052 }
00053 
00054 void do_lookup(const char *s, const char *n, const char *result)
00055 {
00056   Text v;
00057   cout << "Lookup [" << s << "]" << n << "...";
00058   try {
00059     if (VestaConfig::get(s, n, v))
00060       if (result != NULL && v == result) cout << "ok." << nl;
00061       else {
00062         print_expected(result);
00063         cout << "  Actual:   " << v << nl;
00064       }
00065     else
00066       if (result == NULL) cout << "ok." << nl;
00067       else {
00068         print_expected(result);
00069         cout << "  Field not found" << nl;
00070       }
00071   } catch (VestaConfig::failure f) {
00072     cout << "  Got error: " << f.msg << nl;
00073   }
00074 }
00075 
00076 void do_lookup_int(const char *s, const char *n, const int result)
00077 {
00078   int v;
00079   cout << "Lookup_int [" << s << "]" << n << "...";
00080   try {
00081     v = VestaConfig::get_int(s, n);
00082     if (v == result) cout << "ok." << nl;
00083     else cout << "  Expected:  " << result << "  Actual:   " << v << nl;
00084   } catch (VestaConfig::failure f) {
00085     cout << "  Got error: " << f.msg << nl;
00086   }
00087 }
00088 
00089 
00090 void test_bad(const char *name, const char *problem)
00091 {
00092   cout << nl;
00093   set_location(name);
00094   cout << "Expected problem:  " << problem << nl;
00095   try {
00096     Text dummy;
00097     (void)VestaConfig::get("Doesn't", "matter", dummy);
00098     cout << "Failed to get an error report!" << nl;
00099   } catch (VestaConfig::failure f) {
00100     cout << "Reported problem:  " << f.msg << nl;
00101   }
00102 }
00103 
00104 int main()
00105 {
00106   // Test 1:  report default location.
00107 
00108   cout << "Default configuration file: ";
00109   report_location();
00110 
00111   // Test 2: set location to a good file; try some lookups.
00112 
00113   set_location("tests/good_file.cfg");
00114 
00115   do_lookup("Section 1", "Field 1", "Value 1");
00116   do_lookup("Section 2", "Odd_field+name", "even   <,,> odder <,,> value :-)");
00117   do_lookup("Section 3", "xyzzy", "crowther");
00118   do_lookup("Section 4", "No such field", NULL);
00119   do_lookup("Section 5", "Long_field", "This field is so long that it occupies more than one hundred characters, which is the internal buffer size and so requires some extra code to ensure that the whole line is processed.");
00120   do_lookup("Section 5", "The very end", "19");
00121   do_lookup_int("Section 5", "The very end", 19);
00122   do_lookup("Included Section", "FieldInIncludedSection", "Hello");
00123   do_lookup("Section 6", "Field", "Right");
00124   do_lookup("Section 7", "Field", "Right");
00125   do_lookup("Section 7", "Field2", "New");
00126 
00127   // Test 3: set location to various files with syntax errors.
00128 
00129   test_bad("tests/bad_file_1.cfg", "malformed [section] line");
00130 
00131   test_bad("tests/bad_file_2.cfg", "missing equal sign");
00132 
00133   test_bad("tests/bad_file_3.cfg", "missing name before equal sign");
00134 
00135   test_bad("tests/bad_file_4.cfg", "inclusion depth too large");
00136   
00137   test_bad("tests/bad_file_5.cfg", "no section name");
00138 
00139   test_bad("tests/no_such_file", "configuration file doesn't exist");
00140   
00141   return 0;
00142 }

Generated on Mon May 8 00:48:37 2006 for Vesta by  doxygen 1.4.2