|
1 /* |
|
2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Assert tool for checking test result and write log. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "bctestassert.h" |
|
20 #include "bctestlogger.h" |
|
21 |
|
22 _LIT( KFailedTest, "Failed test "); |
|
23 _LIT( KTestSucceed, "Test succeeded." ); |
|
24 _LIT( KTrueFail, "Return value is false." ); |
|
25 _LIT( KNotNullFail, "Verified pointer is NULL." ); |
|
26 _LIT( KIntFailFormat, "Return value is not expected. It is %d." ); |
|
27 _LIT( KCommentFormat, "%d. %s. " ); |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // Constructor |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 EXPORT_C CBCTestAssert::CBCTestAssert() |
|
36 { |
|
37 } |
|
38 |
|
39 // --------------------------------------------------------------------------- |
|
40 // Destructor |
|
41 // --------------------------------------------------------------------------- |
|
42 // |
|
43 EXPORT_C CBCTestAssert::~CBCTestAssert() |
|
44 { |
|
45 } |
|
46 |
|
47 // --------------------------------------------------------------------------- |
|
48 // CBCTestAssert::SetLogger |
|
49 // Setor for iLogger. |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 void CBCTestAssert::SetLogger( CBCTestLogger* aLogger ) |
|
53 { |
|
54 if ( aLogger ) |
|
55 { |
|
56 iLogger = aLogger; |
|
57 } |
|
58 } |
|
59 |
|
60 // --------------------------------------------------------------------------- |
|
61 // CBCTestAssert::AssertLogL |
|
62 // Write assert result to log file. |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 void CBCTestAssert::AssertLogL( TAssertType aType, TBool aSuccess, |
|
66 const TDesC& aComments, SAssertParam* aParam ) |
|
67 { |
|
68 if ( !iLogger || aType >= EAssertEnd || aType < EAssertInt ) |
|
69 { |
|
70 return; |
|
71 } |
|
72 |
|
73 iTestCount++; |
|
74 TDes& buf = iLogger->Buffer(); |
|
75 buf.Zero(); |
|
76 |
|
77 if ( !aSuccess ) |
|
78 { |
|
79 buf.Append( KFailedTest ); |
|
80 iFailedCount++; |
|
81 } |
|
82 buf.AppendFormat( |
|
83 TPtrC( KCommentFormat ), iTestCount, aComments.Ptr() ); |
|
84 if ( !aSuccess ) |
|
85 { |
|
86 switch ( aType ) |
|
87 { |
|
88 case EAssertInt: |
|
89 if ( aParam ) |
|
90 { |
|
91 buf.AppendFormat( |
|
92 TPtrC( KIntFailFormat ), aParam->intValue ); |
|
93 } |
|
94 break; |
|
95 case EAssertTrue: |
|
96 buf.Append( KTrueFail ); |
|
97 break; |
|
98 case EAssertNotNull: |
|
99 buf.Append( KNotNullFail ); |
|
100 break; |
|
101 default: |
|
102 break; |
|
103 } |
|
104 } |
|
105 else |
|
106 { |
|
107 buf.Append( KTestSucceed ); |
|
108 } |
|
109 iLogger->CreateTimeStamp( buf ); |
|
110 iLogger->WriteLogL( buf ); |
|
111 } |
|
112 |
|
113 // --------------------------------------------------------------------------- |
|
114 // CBCTestAssert::AssertIntL |
|
115 // --------------------------------------------------------------------------- |
|
116 // |
|
117 EXPORT_C void CBCTestAssert::AssertIntL( |
|
118 TInt aExpect, TInt aActual, const TDesC& aComments ) |
|
119 { |
|
120 SAssertParam param; |
|
121 param.intValue = aActual; |
|
122 AssertLogL( EAssertInt, ( aExpect == aActual ), aComments, ¶m ); |
|
123 } |
|
124 |
|
125 // --------------------------------------------------------------------------- |
|
126 // CBCTestAssert::AssertTrueL |
|
127 // --------------------------------------------------------------------------- |
|
128 // |
|
129 EXPORT_C void CBCTestAssert::AssertTrueL( |
|
130 TBool aActual, const TDesC& aComments ) |
|
131 { |
|
132 AssertLogL( EAssertTrue, aActual, aComments ); |
|
133 } |
|
134 |
|
135 // --------------------------------------------------------------------------- |
|
136 // CBCTestAssert::AssertNotNullL |
|
137 // --------------------------------------------------------------------------- |
|
138 // |
|
139 EXPORT_C void CBCTestAssert::AssertNotNullL( |
|
140 TAny* aActual, const TDesC& aComments ) |
|
141 { |
|
142 AssertLogL( EAssertNotNull, ( aActual != NULL ), aComments ); |
|
143 } |
|
144 |
|
145 // --------------------------------------------------------------------------- |
|
146 // CBCTestAssert::WriteLogL |
|
147 // Write a sentence of information to log file. |
|
148 // --------------------------------------------------------------------------- |
|
149 // |
|
150 EXPORT_C void CBCTestAssert::WriteLogL( const TDesC& aLogText ) |
|
151 { |
|
152 TDes& buf = iLogger->Buffer(); |
|
153 buf.Zero(); |
|
154 buf.Append( aLogText ); |
|
155 buf.Append( KLogLine ); |
|
156 iLogger->WriteLogL( buf ); |
|
157 } |
|
158 |
|
159 // --------------------------------------------------------------------------- |
|
160 // CBCTestAssert::GetTestSummary |
|
161 // --------------------------------------------------------------------------- |
|
162 // |
|
163 void CBCTestAssert::GetTestSummary( TInt& aTestCount, |
|
164 TInt& aFailedCount ) const |
|
165 { |
|
166 aTestCount = iTestCount; |
|
167 aFailedCount = iFailedCount; |
|
168 } |
|
169 |
|
170 // --------------------------------------------------------------------------- |
|
171 // CBCTestAssert::ClearTestSummary |
|
172 // --------------------------------------------------------------------------- |
|
173 // |
|
174 void CBCTestAssert::ClearTestSummary() |
|
175 { |
|
176 iTestCount = 0; |
|
177 iFailedCount = 0; |
|
178 } |