1 // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\include\e32test.h
39 The class creates a console window to which test results can be logged
40 through the various overloads of the operator().
45 IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway,const TText* anOtherThrowaway);
46 IMPORT_C RTest(const TDesC &aTitle,TInt aThrowaway);
47 IMPORT_C RTest(const TDesC &aTitle);
48 IMPORT_C void Close();
49 IMPORT_C void Title();
50 IMPORT_C void Start(const TDesC &aHeading);
51 IMPORT_C void Next(const TDesC &aHeading);
53 IMPORT_C void operator()(TInt aResult,TInt aLineNum,const TText* aFileName);
54 IMPORT_C void operator()(TInt aResult,TInt aLineNum);
55 IMPORT_C void operator()(TInt aResult);
56 IMPORT_C void Panic(TInt anError,TRefByValue<const TDesC> aFmt,...);
57 IMPORT_C void Panic(TRefByValue<const TDesC> aFmt,...);
58 IMPORT_C void Printf(TRefByValue<const TDesC> aFmt,...);
59 IMPORT_C TKeyCode Getch();
60 inline static const TAny* String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2);
61 inline CConsoleBase* Console() const;
62 inline void SetConsole(CConsoleBase* aConsole);
63 inline TBool Logged() const;
64 inline void SetLogged(TBool aToLog);
65 inline void HandleError(TInt aError, TInt aLine, const TText* aFileName);
66 inline void HandleNull(TInt aLine, const TText* aFileName);
67 inline void HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName);
68 inline void HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName);
70 IMPORT_C static TInt CloseHandleAndWaitForDestruction(RHandleBase& aH); /**< @internalTechnology */
73 void CheckConsoleCreated();
78 enum {EMaxStack=0x100,EMaxBuffer=0x100};
84 CConsoleBase *iConsole;
86 TInt iStack[EMaxStack];
87 TText iBuf[EMaxBuffer];
96 @return A pointer to the console object.
98 inline CConsoleBase* RTest::Console() const
105 Utility function that returns a pointer to the specified TText8* argument
106 or the TText16* argument depending on the value of the aSel argument.
108 @param aSel An integer containing the size of a TText8 type or TText16 type.
109 @param aBuf1 A pointer to 8-bit text.
110 @param aBuf2 A pointer to 16-bit text.
112 @return A pointer to aBuf1, if the value of aSel is the size of a TText8 type,
113 otherwise a pointer to aBuf2.
115 inline const TAny *RTest::String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2)
116 { return(aSel == sizeof(TText8) ? (TAny *)aBuf1 : (TAny *)aBuf2); }
123 inline void RTest::Push()
124 { iStack[iLevel++] = iTest; iTest = 0; }
131 inline void RTest::Pop()
132 { iTest = iStack[--iLevel]; }
140 @param aConsole A pointer to the console object to be used.
142 inline void RTest::SetConsole(CConsoleBase* aConsole)
143 { iConsole = aConsole; }
149 Tests whether the logging flag is set.
151 If the logging flag is set, console output is also written to
152 the debug output as represented by a RDebug object.
154 @return True, if the logging flag is set, false otherwise.
156 inline TBool RTest::Logged() const
157 { return(iLogging); }
163 Sets the logging flag.
165 If the logging flag is set, console output is also written to
166 the debug output as represented by a RDebug object.
168 @param aToLog ETrue, if the logging flag is to be set, EFalse, otherwise.
170 inline void RTest::SetLogged(TBool aToLog)
171 { iLogging = aToLog; }
176 // test equivalent of _L
180 #define _TL(a) (S*)RTest::String(sizeof(S),(TText8*)a,(TText16*)L ## a)
182 // the next two, slightly confusing, macros are necessary in order
183 // to enable proper string merging with certain compilers.
188 #define __test(x,l,f) test(x,l,_S(f))
193 #define test(x) __test(x,__LINE__,__FILE__)
196 #ifdef __E32TEST_EXTENSION__
206 Panics and displays an approprate error message if x is less then zero (Indicating an error code).
208 #define test_NotNegative(x) { TInt _r = (x); if (_r < 0) test.HandleError(_r, __LINE__,__S(__FILE__)); }
213 Panics and displays an approprate error message if x is not equal to KErrNone.
215 #define test_KErrNone(x) { TInt _r = (x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); }
220 Panics and displays an approprate error message if the trapped statement/block x leaves.
222 #define test_TRAP(x) { TRAPD(_r, x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); }
227 Panics and displays an approprate error message if x is not equal to NULL.
229 #define test_NotNull(x) { TAny* _a = (TAny*)(x); if (_a == NULL) test.HandleNull(__LINE__,__S(__FILE__)); }
233 Panics and displays an approprate error message if e (expected) is not equal to a (actual).
235 #define test_Equal(e, a) { TInt _e = TInt(e); TInt _a = TInt(a); if (_e != _a) test.HandleNotEqual(_e, _a, __LINE__,__S(__FILE__)); }
240 Panics and displays an approprate error message if the comparison specifed with b, between a and c is EFalse.
242 #define test_Compare(a,b,c) {TInt _a = TInt(a); TInt _c = TInt(c); if (!(_a b _c)) test.HandleFailedCompare(_a, __S(#b), _c, __LINE__,__S(__FILE__)); }
247 If the expressione e is false, the the statment s is exectude before a Panic is raised.
249 #define test_Assert(e,s) if(!(e)) {s; test.operator()(EFalse, __LINE__,__S(__FILE__)); }
257 Prints a failure message, including an error code at the console and raises a panic.
260 @param aError The error code to be printed in the failure massage.
261 @param aLineNum A line number that is printed in the failure message.
262 @param aFileName A file name that is printed in the failure message.
264 @panic USER 84 Always.
266 inline void RTest::HandleError(TInt aError, TInt aLine, const TText* aFileName)
268 RDebug::Printf("RTEST: Error %d at line %d", aError,aLine);
269 Printf(_L("RTEST: Error %d\n"), aError);
270 operator()(EFalse, aLine, aFileName);
273 Prints a failure message indicating null was encountered, at the console and raises a panic.
275 @param aLineNum A line number that is printed in the failure message.
276 @param aFileName A file name that is printed in the failure message.
278 @panic USER 84 Always.
281 inline void RTest::HandleNull(TInt aLine, const TText* aFileName)
283 RDebug::Printf("RTEST: Null value at line %d", aLine);
284 Printf(_L("RTEST: Null value\n"));
285 operator()(EFalse, aLine, aFileName);
290 Prints a failure message indicating that two value (also printed) where not equal, at the console and raises a panic.
292 @param aExpected The value that is to be printed as expected.
293 @param aActual The value that is to be printed as being actually received.
294 @param aLineNum A line number that is printed in the failure message.
295 @param aFileName A file name that is printed in the failure message.
297 @panic USER 84 Always.
300 inline void RTest::HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName)
302 RDebug::Printf("RTEST: Expected 0x%x (%d) but got 0x%x (%d) at line %d", aExpected,aExpected,aActual,aActual,aLine);
303 Printf(_L("RTEST: Expected 0x%x (%d) but got 0x%x (%d)\n"), aExpected,aExpected,aActual,aActual);
304 operator()(EFalse, aLine, aFileName);
309 Prints a failure message indicating that a comparison between two values (also printed) resulted in EFalse,
310 at the console and raises a panic.
312 @param aLeft The left value of the comparison.
313 @param aComp A string representing the comparison operator.
314 @param aRight The right value of the comparison.
315 @param aLineNum A line number that is printed in the failure message.
316 @param aFileName A file name that is printed in the failure message.
318 @panic USER 84 Always.
320 inline void RTest::HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName)
322 RDebug::Printf("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse at line %d", aLeft,aLeft,aComp,aRight,aRight,aLine);
323 Printf(_L("RTEST: (0x%x (%d) %s 0x%x (%d)) == EFalse\n"), aLeft,aLeft,aComp, aRight,aRight);
324 operator()(EFalse, aLine, aFileName);
331 _LIT(KLitCloseAndWait,"Close&Wait");
336 #define CLOSE_AND_WAIT(h) ((void)(RTest::CloseHandleAndWaitForDestruction(h) && (User::Panic(KLitCloseAndWait,__LINE__),1)))