epoc32/include/e32test.h
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
parent 0 061f57f2323e
child 4 837f303aceeb
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\include\e32test.h
    15 // 
    16 //
    17 
    18 
    19 
    20 /**
    21  @file e32test.h
    22  @publishedAll
    23  @released
    24 */
    25 
    26 #ifndef __E32TEST_H__
    27 #define __E32TEST_H__
    28 #include <e32std.h>
    29 #include <e32base.h>
    30 #include <e32cons.h>
    31 #include <e32ver.h>
    32 #include <e32kpan.h>
    33 #include <e32debug.h>
    34 
    35 
    36 /**
    37 Test console.
    38 
    39 The class creates a console window to which test results can be logged
    40 through the various overloads of the operator().
    41 */
    42 class RTest
    43 	{
    44 public:
    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);
    52 	IMPORT_C void End();
    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);
    69 
    70 	IMPORT_C static TInt CloseHandleAndWaitForDestruction(RHandleBase& aH);	/**< @internalTechnology */
    71 
    72 protected:
    73 	void CheckConsoleCreated();
    74 	void DisplayLevel();
    75 	inline void Push();
    76 	inline void Pop();
    77 private:
    78 	enum {EMaxStack=0x100,EMaxBuffer=0x100};
    79 private:
    80 	TInt	iTest;
    81 	TInt	iCheck;
    82 	TInt	iLevel;
    83 	TBool	iLogging;
    84 	CConsoleBase *iConsole;
    85 	TBuf<0x40> iTitle;
    86 	TInt iStack[EMaxStack];
    87 	TText iBuf[EMaxBuffer];
    88 	};
    89 
    90 
    91 
    92 
    93 /**
    94 Gets the console.
    95 
    96 @return A pointer to the console object.
    97 */
    98 inline CConsoleBase* RTest::Console() const
    99 	{ return(iConsole); }
   100 
   101 	
   102 	
   103 	
   104 /**
   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.
   107 
   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.
   111 
   112 @return A pointer to aBuf1, if the value of aSel is the size of a TText8 type,
   113         otherwise a pointer to aBuf2.
   114 */	
   115 inline const TAny *RTest::String(TInt aSel,const TText8 *aBuf1,const TText16 *aBuf2)
   116 	{ return(aSel == sizeof(TText8) ? (TAny *)aBuf1 : (TAny *)aBuf2); }
   117 
   118 
   119 
   120 /**
   121 @internalComponent
   122 */
   123 inline void RTest::Push()
   124 	{ iStack[iLevel++] = iTest; iTest = 0; }
   125 
   126 
   127 
   128 /**
   129 @internalComponent
   130 */
   131 inline void RTest::Pop()
   132 	{ iTest = iStack[--iLevel]; }
   133 
   134 
   135 
   136 
   137 /**
   138 Sets the console.
   139 
   140 @param aConsole A pointer to the console object to be used.
   141 */	
   142 inline void RTest::SetConsole(CConsoleBase* aConsole)
   143     { iConsole = aConsole; }
   144 
   145 
   146 
   147 
   148 /**
   149 Tests whether the logging flag is set.
   150 
   151 If the logging flag is set, console output is also written to
   152 the debug output as represented by a RDebug object.
   153 
   154 @return True, if the logging flag is set, false otherwise.
   155 */	
   156 inline TBool RTest::Logged() const
   157 	{ return(iLogging); }
   158 
   159 
   160 
   161 
   162 /**
   163 Sets the logging flag.
   164 
   165 If the logging flag is set, console output is also written to
   166 the debug output as represented by a RDebug object.
   167 
   168 @param aToLog ETrue, if the logging flag is to be set, EFalse, otherwise.
   169 */	
   170 inline void RTest::SetLogged(TBool aToLog)
   171 	{ iLogging = aToLog; }
   172 
   173 
   174 
   175 
   176 // test equivalent of _L
   177 /**
   178 @internalComponent
   179 */
   180 #define _TL(a) (S*)RTest::String(sizeof(S),(TText8*)a,(TText16*)L ## a) 
   181 
   182 // the next two, slightly confusing, macros are necessary in order
   183 // to enable proper string merging with certain compilers.
   184 
   185 /**
   186 @internalComponent
   187 */
   188 #define __test(x,l,f) test(x,l,_S(f))
   189 
   190 /**
   191 @internalComponent
   192 */
   193 #define test(x) __test(x,__LINE__,__FILE__)
   194 
   195 
   196 #ifdef __E32TEST_EXTENSION__
   197 
   198 /**
   199 @internalComponent
   200 */
   201 #define __S(f) _S(f)
   202 
   203 /**
   204 @internalComponent
   205 
   206 Panics and displays an approprate error message if x is less then zero (Indicating an error code).
   207 */
   208 #define test_NotNegative(x) { TInt _r = (x); if (_r < 0) test.HandleError(_r, __LINE__,__S(__FILE__)); }
   209 
   210 /**
   211 @internalComponent
   212 
   213 Panics and displays an approprate error message if x is not equal to KErrNone.
   214 */
   215 #define test_KErrNone(x) { TInt _r = (x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); }
   216 
   217 /**
   218 @internalComponent
   219 
   220 Panics and displays an approprate error message if the trapped statement/block x leaves.
   221 */
   222 #define test_TRAP(x) { TRAPD(_r, x); if (_r !=KErrNone) test.HandleError(_r, __LINE__,__S(__FILE__)); }
   223 
   224 /**
   225 @internalComponent
   226 
   227 Panics and displays an approprate error message if x is not equal to NULL.
   228 */
   229 #define test_NotNull(x) { TAny* _a = (TAny*)(x); if (_a == NULL) test.HandleNull(__LINE__,__S(__FILE__)); }
   230 /**
   231 @internalComponent
   232 
   233 Panics and displays an approprate error message if e (expected) is not equal to a (actual).
   234 */
   235 #define test_Equal(e, a) { TInt _e = TInt(e); TInt _a = TInt(a); if (_e != _a) test.HandleNotEqual(_e, _a, __LINE__,__S(__FILE__)); }
   236 
   237 /**
   238 @internalComponent
   239 
   240 Panics and displays an approprate error message if the comparison specifed with b, between a and c is EFalse.
   241 */
   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__)); }
   243 	
   244 /**
   245 @internalComponent
   246 
   247 If the expressione e is false, the the statment s is exectude before a Panic is raised.
   248 */
   249 #define test_Assert(e,s) if(!(e)) {s; test.operator()(EFalse, __LINE__,__S(__FILE__)); }
   250 	
   251 	
   252 
   253 #endif
   254 
   255 
   256 /**
   257 Prints a failure message, including an error code at the console and raises a panic.
   258 
   259 
   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.
   263                  
   264 @panic USER 84 Always.
   265 */
   266 inline void RTest::HandleError(TInt aError, TInt aLine, const TText* aFileName)
   267 	{
   268 	RDebug::Printf("RTEST: Error %d at line %d", aError,aLine);
   269 	Printf(_L("RTEST: Error %d\n"), aError);
   270 	operator()(EFalse, aLine, aFileName);
   271 	}
   272 /**
   273 Prints a failure message indicating null was encountered, at the console and raises a panic.
   274 
   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.
   277                  
   278 @panic USER 84 Always.
   279 */
   280 
   281 inline void RTest::HandleNull(TInt aLine, const TText* aFileName)
   282 	{
   283 	RDebug::Printf("RTEST: Null value at line %d", aLine);
   284 	Printf(_L("RTEST: Null value\n"));
   285 	operator()(EFalse, aLine, aFileName);
   286 	}
   287 
   288 
   289 /**
   290 Prints a failure message indicating that two value (also printed) where not equal, at the console and raises a panic.
   291 
   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.
   296                  
   297 @panic USER 84 Always.
   298 */
   299 
   300 inline void RTest::HandleNotEqual(TInt aExpected, TInt aActual, TInt aLine, const TText* aFileName)
   301 	{
   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);
   305 	}
   306 
   307 
   308 /**
   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.
   311 
   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.
   317                  
   318 @panic USER 84 Always.
   319 */
   320 inline void RTest::HandleFailedCompare(TInt aLeft, const TText* aComp, TInt aRight, TInt aLine, const TText* aFileName)
   321 	{
   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);
   325 	}
   326 
   327 
   328 /**
   329 @internalTechnology
   330 */
   331 _LIT(KLitCloseAndWait,"Close&Wait");
   332 
   333 /**
   334 @internalTechnology
   335 */
   336 #define CLOSE_AND_WAIT(h)	((void)(RTest::CloseHandleAndWaitForDestruction(h) && (User::Panic(KLitCloseAndWait,__LINE__),1)))
   337 
   338 #endif