1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
4 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include "xpcom-config.h"
31 #include "nsEmbedString.h"
32 #include "nsAutoLock.h"
34 /* Unfrozen interfaces */
36 #include "nsIConsoleService.h"
37 #include "nsIConsoleMessage.h"
38 #include "nsServiceManagerUtils.h"
40 /* Project includes */
41 #include "nsITestResult.h"
42 #include "nsTestResult.h"
43 #include "nsTestFailure.h"
45 /******************** nsTestResult ************************************/
46 nsTestResult::nsTestResult() :mFailureList(0)
51 mStartTime
= PR_IntervalNow();
55 nsTestResult::~nsTestResult()
57 nsTestFailure
*failure
;
59 while ( mFailureList
.GetSize() ) {
60 if ( (failure
= (nsTestFailure
*) mFailureList
.PopFront()) ) {
65 PR_DestroyLock(mLock
);
68 NS_IMPL_ISUPPORTS1(nsTestResult
,
72 nsTestResult::MarkTestStart()
74 nsAutoLock
lock(mLock
);
83 nsTestResult::AddFailure(const char *aFile
, PRUint32 aLine
, const char *aText
,
86 nsAutoLock
lock(mLock
);
87 nsTestFailure
*failure
= new nsTestFailure(aFile
, aLine
, aText
, aError
);
88 NS_ENSURE_TRUE(failure
, NS_ERROR_OUT_OF_MEMORY
);
89 mFailureList
.Push( (void *) failure
);
112 nsTestResult::printFailures()
114 nsTestFailure
*failure
;
115 const static char *kFailureMessage
= "test failed";
116 const static char *kFailureHeader
= " failure:";
117 const static char *kErrorMessages
[] = {
118 "cxxunit: message: %s\n"
119 ,"cxxunit: error: component \"%s\" failed to load\n"
121 ,"cxxunit: error: failed to open main window\n"
122 ,"cxxunit: error: component \"%s\" failed to complete async test\n"
125 fprintf(stderr
,"\n");
126 while ( mFailureList
.GetSize() ) {
127 if ( ! (failure
= (nsTestFailure
*) mFailureList
.PopFront()) )
129 if ( failure
->getFile()[0] ){
130 const char *msg
= kFailureMessage
;
131 if ( failure
->getText()[0] ) {
132 msg
= failure
->getText();
134 fprintf(stderr
,"%s:%u:%s %s\n", failure
->getFile(), failure
->getLine(),
135 failure
->isError() ? "" : kFailureHeader
, msg
);
137 fprintf(stderr
, kErrorMessages
[ failure
->getLine() ],
138 failure
->getText() );
145 nsTestResult::printTime()
147 PRInt32 runTime
, msec
, sec
;
149 runTime
= PR_IntervalToMilliseconds(PR_IntervalNow() - mStartTime
);
150 sec
= runTime
/ 1000;
151 msec
= runTime
% 1000;
152 fprintf(stderr
,"Time: %u.%03u\n\n", sec
, msec
);
156 nsTestResult::printHeadline()
158 if (!mFailureCount
&& !mErrorCount
) {
159 fprintf(stderr
,"OK (%u tests)\n", mCount
);
162 fprintf(stderr
,"Failures!!!\n Test run: %u Failures: %u Errors: %u\n",
163 mCount
, mFailureCount
, mErrorCount
);