VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
testmgr.h
Go to the documentation of this file.
1 /*
2  * testmgr.h
3  *
4  * Copyright 2003 - 2016 Brenton Ross
5  *
6  * This class is the base class from which application specific
7  * test harnesses should be derived. Those derived classes must
8  * be made friends of the class that they are testing.
9  *
10  *
11  * This file is part of VICI.
12  *
13  * VICI is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * VICI is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with VICI. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
32 #ifndef CDI_TESTMGR_H
33 #define CDI_TESTMGR_H
34 
35 
36 #include "stringy.h"
37 #include "vx.h"
38 #include "log.h"
39 #include "test.h"
40 #include <map>
41 #include <list>
42 #include <set>
43 #include <fstream>
44 #include <mutex>
45 #include <condition_variable>
46 #include <chrono>
47 
48 // -----------------------------------------------------------------
49 
50 
51 namespace VICI
52 {
53 
55 
59 namespace cdi
60 {
61 
62 // -----------------------------------------------------------------
63 // The intent is that the concrete versions of the following classes
64 // contain the tests, their constructors set things up and their
65 // destructors do the tidying up after the testing.
66 // This means that the Tester needs to construct and destroy the
67 // concrete versions of these objects as it runs the tests.
68 // Hence it needs to have factory objects for each that can instantiate
69 // these concrete objects.
70 //
71 class AbstractTest;
72 class AbstractScenario;
73 class AbstractTestCase;
74 
75 // Bases for the factory classes
76 class TestFactory;
77 class ScenarioFactory;
78 class TestCaseFactory;
79 
80 class ScenarioResults;
81 
83 
89 class Tester
90 {
91 protected:
92  Tester();
93  std::string nameOfTest;
94  std::string logName;
95  bool failed;
96  void title();
97  void installDefaults();
98 
100  std::map< std::string, std::map< std::string, TestCaseFactory *> > tests;
101 
103  std::map< std::string, ScenarioFactory * > scenarios;
104 
106  std::map< std::string, ScenarioResults * > results;
107 
110 
113 
116 
118 
122  void testScenario( csr scenario, ScenarioFactory *scn );
123 public:
125 
128  void configure ( csr testName );
129 
131 
134  csr getTestName() { return nameOfTest; }
135 
137 
140  static Tester & instance();
141 
143  virtual ~Tester(){}
144 
146 
149  cfi::logstream & log();
150 
152 
157  void addTestCase( csr name, csr scenario, TestCaseFactory *tcf );
158 
160  void runTestCase( csr scenarioName, csr testCaseName, ScenarioResults *);
161 
163  void getTestCases( csr scenarioName, std::set< std::string > &testCases );
164 
166 
170  void addScenario( csr scenario, ScenarioFactory *sf);
171 
173 
176  void setTest( TestFactory *t) { theTest = t; }
177 
179 
182  AbstractTest * getTest() { return absTest; }
183 
185 
189 
191  virtual void runTests();
192 
194 
197  bool summary();
198 };
199 
200 // -----------------------------------------------------------------
201 
203 
206 class test_exception : public vx
207 {
208 public:
211 };
212 
214 
217 class scenario_exception : public vx
218 {
219 public:
222 };
223 
224 // -----------------------------------------------------------------
225 
227 
236 {
237 private:
238  std::string mId;
239 public:
241 
244  TestEvent( const std::string & s) : mId(s) {}
245 
247 
250  const std::string & id() { return mId; }
251 };
252 
254 
260 {
261 protected:
263  TestEventQueue();
264 
265 public:
266  std::mutex eventMx;
267  std::list< TestEvent * > events;
268  std::set< std::string > runningEvents;
269  std::mutex functionsMx;
270  std::condition_variable eventCV;
271 
272 public:
274 
277  static TestEventQueue & instance();
278 
280  ~TestEventQueue();
281 
283 
286  static void event( csr id );
287 
288 
290 
293  void enqueueEvent( TestEvent * );
294 };
295 
296 // -----------------------------------------------------------------
297 
299 
303 {
304 public:
306  virtual ~TestFactory(){}
307 
309 
312  virtual AbstractTest * make() = 0;
313 };
314 
316 
319 template < class T >
320 class TestFT : public TestFactory
321 {
322 public:
324 
327  AbstractTest * make() { return new T; }
328 };
329 
331 
336 {
337 public:
339  virtual ~AbstractTest(){}
340 };
341 
343 
346 template < class T >
347 class TestT : public AbstractTest
348 {
349 public:
351  static void install()
352  {
354  }
355 };
356 
358 
361 class DefaultTest : public TestT<DefaultTest>
362 {
363 public:
366 
369 };
370 
371 // -----------------------------------------------------------------
372 
374 
378 {
379 public:
382 
384  virtual ~ScenarioFactory(){}
385 
387  virtual AbstractScenario * make(csr name) = 0;
388 };
389 
391 
394 template < class T >
396 {
397 public:
399 
402  AbstractScenario * make(csr name) { return new T(name); }
403 };
404 
406 
410 {
411  int mNumTests;
413  bool failure;
414 
417 };
418 
420 
426 {
427 protected:
429  std::string scenarioName;
430 public:
432 
435  explicit AbstractScenario(csr name) : scenarioName(name) {}
436 
438  virtual ~AbstractScenario(){}
439 
442  virtual bool willRunTests() { return false; }
443 
446 };
447 
449 
452 template < class T >
454 {
455 public:
457 
460  explicit ScenarioT(csr name) : AbstractScenario(name) {}
461 
463 
466  static void install( csr nm )
467  {
469  }
470 };
471 
473 
476 class DefaultScenario : public ScenarioT<DefaultScenario>
477 {
478 public:
480  explicit DefaultScenario(csr name) : ScenarioT<DefaultScenario>(name) {}
481 
483  DefaultScenario() :ScenarioT<DefaultScenario>("Default-Scenario") {}
484 
487 };
488 
489 // -----------------------------------------------------------------
490 
492 
496 {
497 public:
499  virtual ~TestCaseFactory(){}
500 
502 
506  virtual AbstractTestCase * make(csr nm) = 0;
507 };
508 
510 
513 template < class T >
515 {
516 public:
518 
522  AbstractTestCase * make(csr nm) { return new T(nm); }
523 };
524 
526 
531 {
532 protected:
534  std::string name;
535 
537 
543  bool test( bool cond, csr testMsg, bool expected = false );
544 
546 
551  virtual bool runTest() = 0;
552 
554  ScenarioResults *scenario; // reference
555 public:
557 
562  explicit AbstractTestCase(csr nm) : name(nm), scenario(0) {}
563 
565 
568  virtual ~AbstractTestCase(){}
569 
571 
574  virtual bool operator()(ScenarioResults *);
575 
577  static const bool EXPECTED = true;
578 };
579 
581 
584 template < class T >
586 {
587 public:
589 
593 
595 
599  static void install( csr nm, csr scn )
600  {
601  Tester::instance().addTestCase(nm, scn, new TestCaseFT<T> );
602  }
603 };
604 
605 // -----------------------------------------------------------------
606 
608 
612 {
613 private:
615  virtual bool runTest();
616  void testThread();
617 protected:
619 
624  virtual void initTest() = 0;
625 
627 
634  virtual void handleEvent( TestEvent *ev ) = 0;
635 
637 
640  virtual void timedOut() = 0;
641 
643  // when the last expected event occurs
644  bool done;
645 
647  std::chrono::steady_clock::time_point startTime;
648 
650  std::chrono::steady_clock::duration timeOut;
651 public:
653 
657  ~AsyncTestCase();
658 };
659 
661 template < class T >
663 {
664 public:
666 
670 
672 
676  static void install( csr nm, csr scn )
677  {
678  Tester::instance().addTestCase(nm, scn, new TestCaseFT<T> );
679  }
680 };
681 
682 } // end namespace cdi
683 
684 } // end namespace VICI
685 
686 #endif /* TESTMGR_H_ */
scenario_exception()
Constructor.
Definition: testmgr.h:221
virtual bool willRunTests()
Definition: testmgr.h:442
An exception object with severity levels.
Definition: vx.h:79
cfi::logstream & log()
Get a reference to the logging stream used for the testing.
Definition: testmgr.cpp:80
AbstractTestCase(csr nm)
Constructor.
Definition: testmgr.h:562
static Tester & instance()
Get a reference to the Tester.
Definition: testmgr.cpp:63
static void install(csr nm, csr scn)
Install a factory for the test case.
Definition: testmgr.h:599
void addTestCase(csr name, csr scenario, TestCaseFactory *tcf)
Add a test case factory.
Definition: testmgr.cpp:88
const std::string & id()
Get the identity of the event.
Definition: testmgr.h:250
void setTest(TestFactory *t)
Set the main test object factory.
Definition: testmgr.h:176
virtual AbstractScenario * make(csr name)=0
Create an scenario object.
AsyncTestCase(csr name)
Constructor.
Definition: testmgr.cpp:356
virtual ~AbstractScenario()
Destructor.
Definition: testmgr.h:438
throw this to abandon a particular test case
Definition: testmgr.h:206
Responsible for queuing TestEvents.
Definition: testmgr.h:259
throw this to abandon an entire scenario
Definition: testmgr.h:217
static void event(csr id)
Create a TestEvent, queue it, and wait for it to be processed.
Definition: testmgr.cpp:453
AbstractTest * make()
Construct a Test object.
Definition: testmgr.h:327
AbstractScenario * make(csr name)
Create a scenario object.
Definition: testmgr.h:402
TestEventQueue()
Constructor.
Definition: testmgr.cpp:443
bool test(bool cond, csr testMsg, bool expected=false)
Perform or record a test.
Definition: testmgr.cpp:331
void(* AsyncTestEventFn)(const std::string &s)
Pointer to function used to enqueue a test event.
Definition: test.h:42
Provide a default version of the AbstractTest object.
Definition: testmgr.h:361
static const bool EXPECTED
Value for expected parameter of test()
Definition: testmgr.h:577
Responsible for creating a test object of the required type.
Definition: testmgr.h:320
AbstractTest * absTest
Pointer to the abstract test object. Only valid during the test run.
Definition: testmgr.h:112
std::string nameOfTest
Identifier for test in the configuration file.
Definition: testmgr.h:93
std::map< std::string, std::map< std::string, TestCaseFactory * > > tests
Factories for test cases indexed by name and scenario name.
Definition: testmgr.h:100
Interface between applications and the test harness.
Define a base type for test case factories.
Definition: testmgr.h:495
Useful string functions.
ScenarioResults * scenario
Results for the scenario are placed in here.
Definition: testmgr.h:554
virtual ~Tester()
Destructor.
Definition: testmgr.h:143
Responsible for creating a scenario of some type.
Definition: testmgr.h:395
~DefaultTest()
Destructor.
Definition: testmgr.h:368
DefaultScenario(csr name)
Constructor.
Definition: testmgr.h:480
std::string name
The name of the test case.
Definition: testmgr.h:534
virtual void handleEvent(TestEvent *ev)=0
Handle the events.
bool failed
Flag to hold overall testing result.
Definition: testmgr.h:95
AbstractTest * getTest()
Get the test object.
Definition: testmgr.h:182
~DefaultScenario()
Destructor.
Definition: testmgr.h:486
Tester()
Protected Constructor.
Definition: testmgr.cpp:71
virtual bool runTest()=0
Run the tests for the test case.
std::mutex functionsMx
Control access to list of running events.
Definition: testmgr.h:269
virtual void runTests(csr scenarioName, ScenarioResults *)
Runs the tests within the scenario.
Definition: testmgr.h:445
virtual ~TestFactory()
Destructor.
Definition: testmgr.h:306
Define a type for scenario factories.
Definition: testmgr.h:377
Responsible for installing a factory for the test case.
Definition: testmgr.h:585
Represent an event in the object under test.
Definition: testmgr.h:235
std::list< TestEvent * > events
The queue.
Definition: testmgr.h:267
AbstractScenario(csr name)
Constructor.
Definition: testmgr.h:435
bool done
This should be set by the handleEvent function.
Definition: testmgr.h:644
static TestEventQueue & instance()
Get reference to the singleton queue object.
Definition: testmgr.cpp:435
bool failure
true if scenario tests failed
Definition: testmgr.h:413
void runTestCase(csr scenarioName, csr testCaseName, ScenarioResults *)
Run a specific test case.
Definition: testmgr.cpp:123
static void install()
Install a factory for a type of AbstractTest object.
Definition: testmgr.h:351
int mNumTests
total number of tests
Definition: testmgr.h:411
void getTestCases(csr scenarioName, std::set< std::string > &testCases)
Get the test case names for a scenario.
Definition: testmgr.cpp:96
Responsible for storing the results of testing for a scenario.
Definition: testmgr.h:409
int mNumErrors
number of errors reported
Definition: testmgr.h:412
Responsible for managing resources needed for the entire test.
Definition: testmgr.h:335
void testScenario(csr scenario, ScenarioFactory *scn)
Do testing of a scenario.
Definition: testmgr.cpp:153
virtual void timedOut()=0
Handle time out.
void enqueueEvent(TestEvent *)
Place the event on the queue.
Definition: testmgr.cpp:471
Provide a default scenario object.
Definition: testmgr.h:476
std::chrono::steady_clock::time_point startTime
The time when the test is started.
Definition: testmgr.h:647
Responsible for creating an object that manages the resources for the entire test.
Definition: testmgr.h:302
std::map< std::string, ScenarioFactory * > scenarios
Factories for scenarios indexed by scenario name.
Definition: testmgr.h:103
ScenarioT(csr name)
Constructor.
Definition: testmgr.h:460
Define a type for scenarios.
Definition: testmgr.h:425
AbstractScenario * absScenario
Pointer to the abstract scenario object. Only valid during the test run.
Definition: testmgr.h:115
ScenarioFactory()
Constructor.
Definition: testmgr.h:381
Base class for test cases.
Definition: testmgr.h:530
std::set< std::string > runningEvents
The set of events being handled.
Definition: testmgr.h:268
std::string scenarioName
The name of the scenario, as used when installed.
Definition: testmgr.h:429
static void install(csr nm)
Install a scenario factor into the Tester.
Definition: testmgr.h:466
An exception object with stream semantics.
AsyncTestCaseT(csr nm)
Constructor.
Definition: testmgr.h:669
virtual AbstractTest * make()=0
Create an instance of the test object.
virtual AbstractTestCase * make(csr nm)=0
Create an instance of the test case object.
std::chrono::steady_clock::duration timeOut
The time to wait. The default is 10 seconds.
Definition: testmgr.h:650
bool summary()
Print a summary of the testing to the log stream.
Definition: testmgr.cpp:276
Responsible for handling asynchronous tests.
Definition: testmgr.h:611
static void install(csr nm, csr scn)
Install the factory for the test case.
Definition: testmgr.h:676
Responsible for managing the testing.
Definition: testmgr.h:89
void installDefaults()
Install default test and scenario objects.
Definition: testmgr.cpp:209
csr getTestName()
Get the name of the test.
Definition: testmgr.h:134
Responsible for creating a test case of some type.
Definition: testmgr.h:514
ScenarioResults()
Constructor.
Definition: testmgr.h:416
virtual void initTest()=0
Initiate the test case.
const std::string & csr
short cut for string constants
Definition: vici.h:80
DefaultScenario()
Constructor.
Definition: testmgr.h:483
test_exception()
Constructor.
Definition: testmgr.h:210
virtual bool operator()(ScenarioResults *)
Execute the tests.
Definition: testmgr.cpp:321
~TestEventQueue()
Destructor.
Definition: testmgr.cpp:448
Responsible for installing the factory for the test case type.
Definition: testmgr.h:662
TestCaseT(csr nm)
Constructor.
Definition: testmgr.h:592
void configure(csr testName)
specify the name of the test as in the configuration file
Definition: testmgr.cpp:46
std::map< std::string, ScenarioResults * > results
Results for scenario tests indexed by scenario name.
Definition: testmgr.h:106
virtual void runTests()
Run all the tests.
Definition: testmgr.cpp:230
std::condition_variable eventCV
Wait for event to be queued or processed.
Definition: testmgr.h:270
DefaultTest()
Default constructor that does nothing.
Definition: testmgr.h:365
AbstractTestCase * make(csr nm)
Create a test case object.
Definition: testmgr.h:522
TestFactory * theTest
Factor for the overall test.
Definition: testmgr.h:109
There is a problem but the program can continue.
Definition: vx.h:54
Declarations for the logging component of libcfi.
virtual ~AbstractTest()
Destructor.
Definition: testmgr.h:339
std::string logName
Identifier for the log.
Definition: testmgr.h:94
void title()
Print title for the test.
Definition: testmgr.cpp:263
virtual ~ScenarioFactory()
Destructor.
Definition: testmgr.h:384
Responsible for installing a factory to create scenarios of the required type.
Definition: testmgr.h:453
AbstractScenario * getScenario()
Get the current scenario object.
Definition: testmgr.h:188
std::mutex eventMx
Control access to the queue.
Definition: testmgr.h:266
TestEvent(const std::string &s)
Constructor.
Definition: testmgr.h:244
Responsible for installing a factory for making test objects.
Definition: testmgr.h:347
virtual ~AbstractTestCase()
Destructor.
Definition: testmgr.h:568
virtual ~TestCaseFactory()
Destructor.
Definition: testmgr.h:499
void addScenario(csr scenario, ScenarioFactory *sf)
Add a scenario factory.
Definition: testmgr.cpp:107
A stream class specialized for logging.
Definition: log.h:58