transport: pass correct channel
[abstract.git] / xpunit / nsTestResult.cpp
blob66bb60a56c1d2fe7b8c1f450ef06c4d0edd8274c
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: */
3 /*
4 * Copyright (C) 2010 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.
22 #include "xpcom-config.h"
24 #include <stdio.h>
25 #include "nsCOMPtr.h"
27 /* Unfrozen interfaces */
28 #include "nsDeque.h"
29 #include "nsIConsoleService.h"
30 #include "nsIConsoleMessage.h"
31 #include "nsServiceManagerUtils.h"
33 /* Project includes */
34 #include "nsTestResult.h"
35 #include "nsTestFailure.h"
37 /******************** nsTestResult ************************************/
38 nsTestResult::nsTestResult()
39 : mFailureList(0)
40 , mLock("nsTestResult Lock")
42 mCount = 0;
43 mFailureCount = 0;
44 mErrorCount = 0;
45 mStartTime = PR_IntervalNow();
46 mSilent = PR_FALSE;
49 nsTestResult::~nsTestResult()
51 nsTestFailure *failure;
53 while ( mFailureList.GetSize() ) {
54 if ( (failure = (nsTestFailure *) mFailureList.PopFront()) ) {
55 delete failure;
60 NS_IMPL_ISUPPORTS1(nsTestResult,
61 nsITestResult)
63 NS_IMETHODIMP
64 nsTestResult::MarkTestStart()
66 mozilla::MutexAutoLock lock(mLock);
67 mCount++;
68 if (mSilent)
69 return NS_OK;
70 printf_stderr(".");
71 if (! (mCount % 50))
72 printf_stderr("\n");
73 return NS_OK;
76 NS_IMETHODIMP
77 nsTestResult::AddFailure(const char *aFile, PRUint32 aLine, const char *aText,
78 PRBool aError)
80 mozilla::MutexAutoLock lock(mLock);
81 nsTestFailure *failure = new nsTestFailure(aFile, aLine, aText, aError);
82 NS_ENSURE_TRUE(failure, NS_ERROR_OUT_OF_MEMORY);
83 mFailureList.Push( (void *) failure );
84 if (mSilent)
85 return NS_OK;
87 if ( ! aError ) {
88 printf_stderr("F");
89 mFailureCount++;
90 } else if ( aLine ) {
91 printf_stderr("E");
92 mErrorCount++;
95 return NS_OK;
98 NS_IMETHODIMP
99 nsTestResult::Done()
101 if (mSilent)
102 return NS_OK;
104 printFailures();
105 printTime();
106 printHeadline();
108 return NS_OK;
111 void
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"
120 ,"cxxunit: %s\n"
121 ,"cxxunit: error: failed to open main window\n"
122 ,"cxxunit: error: component \"%s\" failed to complete async test\n"
125 printf_stderr("\n");
126 while ( mFailureList.GetSize() ) {
127 if ( ! (failure = (nsTestFailure *) mFailureList.PopFront()) )
128 continue;
129 if ( failure->getFile()[0] ){
130 const char *msg = kFailureMessage;
131 if ( failure->getText()[0] ) {
132 msg = failure->getText();
134 printf_stderr("%s:%u:%s %s\n", failure->getFile(), failure->getLine(),
135 failure->isError() ? "" : kFailureHeader, msg);
136 } else {
137 printf_stderr( kErrorMessages[ failure->getLine() ],
138 failure->getText() );
140 delete failure;
144 void
145 nsTestResult::printTime()
147 PRInt32 runTime, msec, sec;
149 runTime = PR_IntervalToMilliseconds(PR_IntervalNow() - mStartTime);
150 sec = runTime / 1000;
151 msec = runTime % 1000;
152 printf_stderr("Time: %u.%03u\n\n", sec, msec);
155 void
156 nsTestResult::printHeadline()
158 if (!mFailureCount && !mErrorCount) {
159 printf_stderr("OK (%u tests)\n", mCount);
160 return;
162 printf_stderr("Failures!!!\n Test run: %u Failures: %u Errors: %u\n",
163 mCount, mFailureCount, mErrorCount);