xref: /haiku/headers/tools/cppunit/TestShell.h (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 #ifndef _beos_test_shell_h_
2 #define _beos_test_shell_h_
3 
4 #include <LockerSyncObject.h>
5 #include <cppunit/Exception.h>
6 #include <cppunit/Test.h>
7 #include <cppunit/TestListener.h>
8 #include <cppunit/TestResult.h>
9 #include <cppunit/TestResultCollector.h>
10 #include <TestSuite.h>
11 #include <map>
12 #include <set>
13 #include <string>
14 
15 class BDirectory;
16 
17 // Defines SuiteFunction to be a pointer to a function that
18 // takes no arguments and returns a pointer to a CppUnit::Test
19 typedef CppUnit::Test* (*SuiteFunction)(void);
20 
21 // This is just absurd to have to type...
22 typedef CppUnit::SynchronizedObject::SynchronizationObject SyncObject;
23 
24 //! BeOS savvy command line interface for the CppUnit testing framework.
25 /*! This class provides a fully functional command-line testing interface
26 	built on top of the CppUnit testing library. You add named test suites
27 	via AddSuite(), and then call Run(), which does all the dirty work. The
28 	user can get a list of each test installed via AddSuite(), and optionally
29 	can opt to run only a specified set of them.
30 */
31 class BTestShell {
32 public:
33 	BTestShell(const std::string &description = "", SyncObject *syncObject = 0);
34 	virtual ~BTestShell();
35 
36 	// This function is used to add the tests for a given kit (as contained
37 	// in a BTestSuite object) to the list of available tests. The shell assumes
38 	// ownership of the BTestSuite object. Each test in the kit is added to
39 	// the list of tests via a call to AddTest(std::string
40 	status_t AddSuite(BTestSuite *kit);
41 
42 	// This function is used to add test suites to the list of available
43 	// tests. A SuiteFunction is just a function that takes no parameters
44 	// and returns a pointer to a CppUnit::Test object. Return NULL at
45 	// your own risk :-). The name given is the name that will be presented
46 	// when the program is run with "--list" as an argument. Usually the
47 	// given suite would be a test suite for an entire class, but that's
48 	// not a requirement.
49 	void AddTest(const std::string &name, CppUnit::Test* test);
50 
51 	// This function loads all the test addons it finds in the given
52 	// directory, returning the number of tests actually loaded.
53 	int32 LoadSuitesFrom(BDirectory *libDir);
54 
55 	// This is the function you call after you've added all your test
56 	// suites with calls to AddSuite(). It runs the test, or displays
57 	// help, or lists installed tests, or whatever, depending on the
58 	// command-line arguments passed in.
59 	int Run(int argc, char *argv[]);
60 
61 	// Verbosity Level enumeration and accessor function
62 	enum VerbosityLevel { v0, v1, v2, v3, v4 };
63 	VerbosityLevel Verbosity() const;
64 
65 	// Returns true if verbosity is high enough that individual tests are
66 	// allowed to make noise.
67 	bool BeVerbose() const { return Verbosity() >= v2; };
68 
69 	static bool GlobalBeVerbose() { return (fGlobalShell ? fGlobalShell->BeVerbose() : true); };
70 
71 	// Returns a pointer to a global BTestShell object. This function is
72 	// something of a hack, used to give BTestCase and its subclasses
73 	// access to verbosity information. Don't rely on it if you don't
74 	// have to (and always make sure the pointer it returns isn't NULL
75 	// before you try to use it :-).
76 	static BTestShell* GlobalShell() { return fGlobalShell; };
77 
78 	// Sets the global BTestShell pointer. The BTestShell class does
79 	// not assume ownership of the object.
80 	static void SetGlobalShell(BTestShell *shell) { fGlobalShell = shell; };
81 
82 	const char* TestDir() const;
83 	static const char* GlobalTestDir() { return (fGlobalShell ? fGlobalShell->TestDir() : NULL); };
84 
85 
86 protected:
87 	typedef std::map<std::string, CppUnit::Test*> TestMap;
88 	typedef std::map<std::string, BTestSuite*> SuiteMap;
89 
90 	VerbosityLevel fVerbosityLevel;
91 	std::set<std::string> fTestsToRun;
92 	std::set<std::string> fSuitesToRun;
93 	TestMap fTests;
94 	SuiteMap fSuites;
95 	std::set<std::string> fLibDirs;
96 	CppUnit::TestResult fTestResults;
97 	CppUnit::TestResultCollector fResultsCollector;
98 	std::string fDescription;
99 	static BTestShell* fGlobalShell;
100 	static const char indent[];
101 	bool fListTestsAndExit;
102 	BPath *fTestDir;
103 
104 	//! Prints a brief description of the program.
105 	virtual void PrintDescription(int argc, char *argv[]);
106 
107 	//! Prints out command line argument instructions
108 	void PrintHelp();
109 
110 	/*! \brief Prints out the list of valid command line arguments.
111 		Called by PrintHelp().
112 	*/
113 	virtual void PrintValidArguments();
114 
115 	//! Prints out a list of all the currently available tests
116 	void PrintInstalledTests();
117 
118 	/*! \brief Handles command line arguments; returns true if everything goes
119 		okay, false if not (or if the program just needs to terminate without
120 		running any tests). Modifies settings in "settings" as necessary.
121 	*/
122 	bool ProcessArguments(int argc, char *argv[]);
123 
124 	//! Processes a single argument, given by the \c arg parameter.
125 	virtual bool ProcessArgument(std::string arg, int argc, char *argv[]);
126 
127 	//! Makes any necessary pre-test preparations
128 	void InitOutput();
129 
130 	/*! \brief Prints out the test results in the proper format per
131 		the specified verbosity level.
132 	*/
133 	void PrintResults();
134 
135 	/*! \brief Searches all the paths in \c fLibDirs, loading any dynamically
136 		loadable suites it finds.
137 	*/
138 	virtual void LoadDynamicSuites();
139 
140 	//! Sets the current test directory.
141 	void UpdateTestDir(char *argv[]);
142 
143 
144 private:
145   //! Prevents the use of the copy constructor.
146   BTestShell( const BTestShell &copy );
147 
148   //! Prevents the use of the copy operator.
149   void operator =( const BTestShell &copy );
150 
151 };	// class BTestShell
152 
153 #endif // _beos_test_shell_h_
154