xref: /haiku/src/tools/cppunit/BTestCase.cpp (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
1 #include <TestCase.h>
2 #include <TestShell.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 
7 _EXPORT
8 BTestCase::BTestCase(string name)
9 	: CppUnit::TestCase(name)
10 	, fValidCWD(false)
11 	, fSubTestNum(0)
12 {
13 }
14 
15 _EXPORT
16 void
17 BTestCase::tearDown() {
18 	if (fSubTestNum != 0)
19 		NextSubTestBlock();
20 }
21 
22 _EXPORT
23 void
24 BTestCase::NextSubTest() {
25 	if (BTestShell::GlobalBeVerbose()) {
26 		printf("[%ld]", fSubTestNum++);
27 		fflush(stdout);
28 	}
29 }
30 
31 _EXPORT
32 void
33 BTestCase::NextSubTestBlock() {
34 	if (BTestShell::GlobalBeVerbose())
35 		printf("\n");
36 }
37 
38 _EXPORT
39 void
40 BTestCase::Outputf(const char *str, ...) {
41 	if (BTestShell::GlobalBeVerbose()) {
42 		va_list args;
43 		va_start(args, str);
44 		vprintf(str, args);
45 		va_end(args);
46 		fflush(stdout);
47 	}
48 }
49 
50 /*! To return to the last saved working directory, call RestoreCWD(). */
51 _EXPORT
52 void
53 BTestCase::SaveCWD() {
54 	fValidCWD = getcwd(fCurrentWorkingDir, B_PATH_NAME_LENGTH);
55 }
56 
57 /*	If SaveCWD() has not been called and an alternate
58 	directory is specified by alternate, the current working directory is
59 	changed to alternate. If alternate is null, the current working directory
60 	is not modified.
61 */
62 _EXPORT
63 void
64 BTestCase::RestoreCWD(const char *alternate) {
65 	if (fValidCWD)
66 		chdir(fCurrentWorkingDir);
67 	else if (alternate != NULL)
68 		chdir(alternate);
69 }
70 
71