-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testable.hpp
51 lines (42 loc) · 1.28 KB
/
Testable.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Testable.hpp
//
// Created by rick gessner on 1/8/22.
//
#ifndef Testable_h
#define Testable_h
#include <iostream>
#include <string>
#include <optional>
#include <sstream>
namespace ECE141 {
class Testable {
protected:
size_t count;
public:
Testable() : count{0} {}
using OptString = std::optional<std::string>;
virtual OptString getTestName(size_t anIndex) const=0;
virtual bool operator()(const std::string &aName)=0;
size_t runTests() {
size_t theCount{0};
std::stringstream theOutput;
for(size_t i=0;i<count;i++) {
if(auto theName=getTestName(i)) {
bool theResult=(*this)(theName.value());
if(theResult) theCount++;
static const char* gResults[]={"FAIL","PASS"};
theOutput << i+1 << ". " << theName.value()
<< ": " << gResults[theResult] << "\n";
}
}
if(theCount!=count) {
std::cout << theCount << " of " << count;
}
else std::cout << "All";
std::cout << " tests passed.\n" << theOutput.str() << "\n";
return theCount;
}
};
}
#endif /* Testable_h */