-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbase.cpp
62 lines (50 loc) · 1.45 KB
/
testbase.cpp
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
52
53
54
55
56
57
58
59
60
61
62
#include "testbase.h"
#include <iomanip>
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#define CONSOLE_COLOR_GREEN FOREGROUND_GREEN
#define CONSOLE_COLOR_RED FOREGROUND_RED
#elif __linux
#define CONSOLE_COLOR_GREEN 0
#define CONSOLE_COLOR_RED 1
#endif
unit::TestBase::TestBase(const std::string &name)
: name(name)
, passed(false)
, maxNameLength(0) {
}
void unit::TestBase::align(int maxNameLength) {
this->maxNameLength = maxNameLength;
}
std::string unit::TestBase::getName() {
return name;
}
void unit::TestBase::report() {
std::cout << name << std::setw(maxNameLength - (int)name.length() + 2) << " [";
setConsoleColor(passed ? CONSOLE_COLOR_GREEN : CONSOLE_COLOR_RED);
std::cout << (passed ? "OK" : "FAIL");
resetConsoleColor();
std::cout << "]" << std::endl;
}
void unit::TestBase::pass() {
passed = true;
}
void unit::TestBase::setConsoleColor(unsigned long color) {
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO info;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(handle, &info);
attributes = info.wAttributes;
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
#elif __linux
std::cout << "\033[0;" << (color == CONSOLE_COLOR_GREEN ? "32" : "31") << "m";
#endif
}
void unit::TestBase::resetConsoleColor() {
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attributes);
#elif __linux
std::cout << "\033[0m";
#endif
}