1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "nel/misc/sstring.h"
28 CTestContext(const NLMISC::CSString
& title
, const NLMISC::CSString
& anticipatedResult
)
31 anticipatedResult
.splitLines(_Lines
);
36 // add self to top of the context stack
37 CTestContext
*& theTopContext
= _getTopContext();
38 _NextContext
= theTopContext
;
42 virtual ~CTestContext()
44 // setup a ref to the pointer to this context in the context stack
45 CTestContext
*& ptr
= _getTopContext();
50 ptr
= ptr
->_NextContext
;
53 // remove from the context stack...
56 // make sure we reached the end of th test vector correctly
57 if (!_Errors
&& _Index
!=_Lines
.size())
61 if (NLMISC::WarningLog
!=NULL
)
63 nlwarning("TEST FAILED: No more output but expecting: %s",_Lines
[_Index
].c_str());
67 // display success or failure message
68 if (NLMISC::InfoLog
!=NULL
)
70 nlinfo("TEST: %s: %s",_Title
.c_str(),(_Errors
?(NLMISC::toString("FAILED AT LINE %d",_ErrorLine
).c_str()):"SUCCESS"));
73 // calculate the length of the lines...
75 for (uint32 i
=0;i
<_Lines
.size();++i
)
77 lineLength
= std::max(lineLength
,(uint32
)_Lines
[i
].size()+3);
80 // display a header line
81 nlinfo(" %-*s %s",lineLength
,"EXPECTED","FOUND");
83 // display the lines that exist in both lines and result vectors
85 for (line
=0;line
<_Lines
.size() && line
<_Result
.size();++line
)
87 nlinfo("%4d %-*s %s",line
,lineLength
,_Lines
[line
].c_str(),_Result
[line
].c_str());
90 // display any remaining lines from the lines vector
91 for (;line
<_Lines
.size();++line
)
93 nlinfo("%4d %s",line
,_Lines
[line
].c_str());
96 // display any remaining lines from the result vector
97 for (;line
<_Result
.size();++line
)
99 nlinfo("%4d %*s %s",line
,lineLength
,"",_Result
[line
].c_str());
105 static void testOutput(const char* file
, uint32 line
, const NLMISC::CSString
& txt
)
107 // if we're not in a test context then just quit
108 if(_getTopContext()==NULL
)
111 NLMISC::DebugLog
->displayNL("%s: %d: Test output with no context: %s",file
,line
,txt
.c_str());
116 _getTopContext()->_testOutput(file
,line
,txt
);
119 static bool foundErrors()
121 if(_getTopContext()==NULL
)
123 return _getTopContext()->_Errors
;
127 virtual void _testOutput(const char* file
, uint32 line
, const NLMISC::CSString
& txt
)
129 // add the output to our result vector
130 _Result
.push_back(txt
);
132 // if there have already been errors then just display a warning
135 NLMISC::DebugLog
->displayNL("%s: %d: After error: %s",file
,line
,txt
.c_str());
139 // check that this test doesn't provoke an error
140 if (_Index
>=_Lines
.size())
143 NLMISC::WarningLog
->displayNL("%s: %d: TEST FAILED: Found extra output after end of test: %s",file
,line
,txt
.c_str());
148 // check that this test doesn't provoke an error
149 if (txt
!=_Lines
[_Index
])
152 NLMISC::WarningLog
->displayNL("%s: %d: TEST FAILED: \n- EXPECTED - %s\n- FOUND - %s",file
,line
,_Lines
[_Index
].c_str(),txt
.c_str());
157 // display the no-error message
158 nldebug("%s",txt
.c_str());
159 // increment the index
163 static CTestContext
*& _getTopContext()
165 static CTestContext
* topContext
= NULL
;
169 NLMISC::CSString _Title
;
170 NLMISC::CVectorSString _Lines
;
171 NLMISC::CVectorSString _Result
;
175 CTestContext
* _NextContext
;
178 class CTestContextMute
: public CTestContext
181 CTestContextMute(): CTestContext("","")
186 virtual void _testOutput(const char* file
, uint32 line
, const NLMISC::CSString
& txt
)
191 #define DOTEST(cmd,result) { CTestContext tc(NLMISC::toString("TEST("__FILE__":%d)",__LINE__),result); cmd; TEST_ASSERT(!tc.foundErrors()) }
192 #define MUTETEST(cmd) { CTestContextMute tcm; cmd; }
193 #define TEST(txt_and_args) CTestContext::testOutput(__FILE__,__LINE__,NLMISC::toString txt_and_args )
195 #else // ENABLE_TESTS
197 #define DOTEST(cmd,result) while(0) {}
198 #define MUTETEST(cmd) while(0) {}
199 #define TEST(txt) while(0) {}
201 #endif // ENABLE_TESTS
203 #endif // HEADER FILE