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