1 /*****************************************************************************
2 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
3 * @file
4 * @author Jim E. Brooks http://www.palomino3d.org
5 * @brief Test of ConfTxt class.
6 *//*
7 * LEGAL: COPYRIGHT (C) 2008 JIM E. BROOKS
8 * THIS SOURCE CODE IS RELEASED UNDER THE TERMS
9 * OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
10 *****************************************************************************/
11
12 #define TESTING 1
13 #include "test.hh"
14 #include "base/module.hh"
15 #include "base/conf_txt.hh"
16 #include "base/file.hh"
17 #include <fstream>
18 using namespace base;
19
20 INTERN const string FNAME = "/tmp/base_conf_txt_test.txt";
21
22 class ConfTxtTest : public ConfTxt
23 {
24 public:
ConfTxtTest(void)25 ConfTxtTest( void )
26 : mLineCount(0),
27 mWindowWidth(),
28 mWindowHeight()
29 {
30 }
31
32 private:
ProcessField(string & field,std::ifstream & config)33 virtual void ProcessField( string& field, std::ifstream& config )
34 {
35 if ( field == "WindowWidth" )
36 {
37 config >> mWindowWidth;
38 }
39 else if ( field == "WindowHeight" )
40 {
41 config >> mWindowHeight;
42 }
43 else
44 {
45 Fail();
46 }
47 }
48
49 public:
50 int mLineCount;
51 string mWindowWidth; // "800"
52 string mWindowHeight; // "600"
53 };
54
55 /*****************************************************************************
56 * Test.
57 *****************************************************************************/
58 bool
Test1(void)59 Test1( void )
60 {
61 // ------------------------------------------------------------------------
62 // First series of tests are for when no configuration file exists yet.
63 // ------------------------------------------------------------------------
64
65 RemoveFile( FNAME ); // ensure configuration file doesn't exist
66
67 ConfTxtTest conf;
68 bool exception = false;
69
70 // Test that the error of empty filename causes throwing an exception.
71 exception = false;
72 try
73 {
74 conf.Read( "" );
75 }
76 catch (...)
77 {
78 // ok
79 exception = true;
80 }
81 if ( not exception )
82 {
83 CDEBUG << "FAIL: empty filename \n";
84 return Fail();
85 }
86
87 return true;
88 }
89
90 /*****************************************************************************
91 * Test reading/parsing lines of textual configuration.
92 *****************************************************************************/
93 bool
Test2(void)94 Test2( void )
95 {
96 RemoveFile( FNAME ); // ensure configuration file doesn't exist
97
98 // Create a data file.
99 std::fstream fs;
100 // fs.open( FNAME.c_str(), std::ios::in | std::ios::out | std::ios::binary ); // wrong
101 fs.open( FNAME.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc );
102 fs << "WindowWidth 800" << std::endl
103 << "WindowHeight 600" << std::endl;
104 if ( (not fs.good()) or (not fs.is_open()) )
105 {
106 return Fail();
107 }
108
109 // Read data file.
110 ConfTxtTest conf;
111 try
112 {
113 conf.Read( FNAME );
114 }
115 catch (...)
116 {
117 return Fail();
118 }
119
120 if ( conf.mWindowWidth == "800"
121 and conf.mWindowHeight == "600" )
122 {
123 return true;
124 }
125 else
126 {
127 return Fail();
128 }
129 }
130
131 /*****************************************************************************
132 * Called if all other tests passed else no cleanup.
133 *****************************************************************************/
134 bool
Cleanup(void)135 Cleanup( void )
136 {
137 RemoveFile( FNAME );
138 return true;
139 }
140
141 /*****************************************************************************
142 * main.
143 *****************************************************************************/
144 int
main(int argc,char ** argv)145 main( int argc, char** argv )
146 {
147 RUN_TEST( "ConfTxt", TEST_ERROR_MODE_CATCH, // this test expects an exception
148 Test1()
149 and Test2()
150 and Cleanup() )
151 }
152