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