TestGroup.h
Go to the documentation of this file.
1 /****
2  * TestGroup.h - Extensible test framework for Sming
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the SmingTest Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "TestBase.h"
25 #include <Platform/Timers.h>
27 #include <csetjmp>
28 
29 using namespace Profiling;
30 
34 class TestGroup : public TestBase
35 {
36 public:
37  TestGroup(const String& name) : name(name)
38  {
39  }
40 
41  void commenceTest();
42 
48  virtual void execute() = 0;
49 
53  void startItem(const String& tag, const String& description = nullptr);
54 
58  void fail(const char* func) override;
59 
60  const String& getName()
61  {
62  return name;
63  }
64 
66  {
67  return groupTimer.elapsedTime();
68  }
69 
70  enum class State {
71  running,
72  pending,
73  complete,
74  failed,
75  };
76 
77  State getState() const
78  {
79  return state;
80  }
81 
86 
87 protected:
92  void pending()
93  {
94  state = State::pending;
95  }
96 
100  void complete();
101 
102 private:
103  String name;
104  State state{State::running};
105  OneShotFastUs groupTimer;
106  jmp_buf exception;
107 };
108 
109 #define startTest(s) startItem(_F(s))
110 
124 // https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros
125 #define TEST_CASE_1_ARG(name) startItem(_F(name));
126 #define TEST_CASE_2_ARGS(name, desc) startItem(_F(name), _F(desc));
127 
128 #define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
129 #define TEST_CASE_ARG_CHOOSER(...) GET_3RD_ARG(__VA_ARGS__, TEST_CASE_2_ARGS, TEST_CASE_1_ARG, )
130 
131 #define TEST_CASE(...) TEST_CASE_ARG_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
Template class to implement a polled timer.
Definition: PolledTimer.h:68
The String class.
Definition: WString.h:137
Base class supporting verification for test assertions.
Definition: TestBase.h:31
Class to simplify generation of begin/end messages for a test group.
Definition: TestGroup.h:35
TestGroup(const String &name)
Definition: TestGroup.h:37
State
Definition: TestGroup.h:70
virtual void execute()=0
Implement this method to define the test.
const String & getName()
Definition: TestGroup.h:60
void complete()
Call to complete pending (asynchronous) test.
void pending()
Call to mark test as pending so it will be executed asynchronously Call complete() when test is finis...
Definition: TestGroup.h:92
void commenceTest()
void startItem(const String &tag, const String &description=nullptr)
Note the start of a test item within a group.
void initialiseAndExecute()
Called by test runner.
State getState() const
Definition: TestGroup.h:77
void fail(const char *func) override
Called when test fails to identify location.
NanoTime::Time< uint32_t > elapsedTime() const
Definition: TestGroup.h:65
@ pending
Definition: Libraries/IOControl/include/IO/Error.h:72
Definition: CpuUsage.h:5