[app] Use system locale
[abstract.git] / xpunit / nsTestResult.cpp
blob837fe1108997623a88042699aec4863bcb837f51
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) 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.
22 #ifdef DEBUG
23 #define AA_HACK_DEBUG
24 #undef DEBUG
25 #endif
26 #include "xpcom-config.h"
28 #include <stdio.h>
29 #include "nsCOMPtr.h"
30 #include "prinrval.h"
31 #include "nsEmbedString.h"
32 #include "nsAutoLock.h"
34 /* Unfrozen interfaces */
35 #include "nsDeque.h"
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)
48 mCount = 0;
49 mFailureCount = 0;
50 mErrorCount = 0;
51 mStartTime = PR_IntervalNow();
52 mLock = PR_NewLock();
55 nsTestResult::~nsTestResult()
57 nsTestFailure *failure;
59 while ( mFailureList.GetSize() ) {
60 if ( (failure = (nsTestFailure *) mFailureList.PopFront()) ) {
61 delete failure;
64 if ( mLock )
65 PR_DestroyLock(mLock);
68 NS_IMPL_ISUPPORTS1(nsTestResult,
69 nsITestResult)
71 NS_IMETHODIMP
72 nsTestResult::MarkTestStart()
74 nsAutoLock lock(mLock);
75 mCount++;
76 fprintf(stderr,".");
77 if (! (mCount % 50))
78 fprintf(stderr,"\n");
79 return NS_OK;
82 NS_IMETHODIMP
83 nsTestResult::AddFailure(const char *aFile, PRUint32 aLine, const char *aText,
84 PRBool aError)
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 );
90 if ( ! aError ) {
91 fprintf(stderr,"F");
92 mFailureCount++;
93 } else if ( aLine ) {
94 fprintf(stderr,"E");
95 mErrorCount++;
98 return NS_OK;
101 NS_IMETHODIMP
102 nsTestResult::Done()
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 fprintf(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 fprintf(stderr,"%s:%u:%s %s\n", failure->getFile(), failure->getLine(),
135 failure->isError() ? "" : kFailureHeader, msg);
136 } else {
137 fprintf(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 fprintf(stderr,"Time: %u.%03u\n\n", sec, msec);
155 void
156 nsTestResult::printHeadline()
158 if (!mFailureCount && !mErrorCount) {
159 fprintf(stderr,"OK (%u tests)\n", mCount);
160 return;
162 fprintf(stderr,"Failures!!!\n Test run: %u Failures: %u Errors: %u\n",
163 mCount, mFailureCount, mErrorCount);