2019-09-23 04:09:58 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
#include "cppunitlite/MainAdapter.h"
|
2019-09-23 04:09:58 +07:00
|
|
|
#include "cppunitlite/Test.h"
|
|
|
|
#include "cppunitlite/TestRegistry.h"
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
int MainAdapter::writeAllTestsInfoToFile(const char *fname) {
|
2019-09-23 04:09:58 +07:00
|
|
|
FILE *outFile = fopen(fname, "w");
|
|
|
|
if (!outFile) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outFile, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
|
|
|
fprintf(outFile, "<tests>\n");
|
|
|
|
|
|
|
|
Test *curTest = TestRegistry::getFirstTest();
|
|
|
|
while (curTest) {
|
|
|
|
fprintf(outFile, "<test ");
|
|
|
|
|
|
|
|
fprintf(outFile, " name=\"%s\" ", curTest->getName());
|
|
|
|
fprintf(outFile, " group=\"%s\" ", curTest->getGroup());
|
|
|
|
fprintf(outFile, " timeout=\"%d\" ", curTest->getTimeout());
|
|
|
|
|
|
|
|
fprintf(outFile, "/>\n");
|
|
|
|
curTest = curTest->getNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(outFile, "</tests>\n");
|
|
|
|
fclose(outFile);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
int MainAdapter::runTest(const char *groupName, const char *testName) {
|
2019-09-23 04:09:58 +07:00
|
|
|
Test *curTest = TestRegistry::getFirstTest();
|
|
|
|
while (curTest) {
|
|
|
|
if (!strcmp(groupName, curTest->getGroup()) && !strcmp(testName, curTest->getName())) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curTest = curTest->getNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!curTest) {
|
|
|
|
printf("Test group='%s' name='%s' not found\n", groupName, testName);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResult result;
|
|
|
|
curTest->run(result);
|
|
|
|
|
|
|
|
if (result.getFailureCount()) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-04-12 21:51:51 +07:00
|
|
|
else if (result.getWarningCount()) {
|
|
|
|
return 3;
|
|
|
|
}
|
2019-09-23 04:09:58 +07:00
|
|
|
else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
int MainAdapter::runGroup(const char *groupName) {
|
2019-09-23 04:09:58 +07:00
|
|
|
Test *curTest = TestRegistry::getFirstTest();
|
|
|
|
int ranTests = 0;
|
2021-04-12 21:51:51 +07:00
|
|
|
int warnTest = 0;
|
2019-09-23 04:09:58 +07:00
|
|
|
while (curTest) {
|
|
|
|
if (strcmp(groupName, curTest->getGroup())) {
|
|
|
|
curTest = curTest->getNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResult result;
|
|
|
|
curTest->run(result);
|
|
|
|
ranTests++;
|
|
|
|
|
|
|
|
if (result.getFailureCount()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
if (result.getWarningCount()) {
|
|
|
|
warnTest++;
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:09:58 +07:00
|
|
|
curTest = curTest->getNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ranTests == 0) {
|
|
|
|
printf("No tests with group='%s' found\n", groupName);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
if (warnTest > 0) {
|
|
|
|
printf("There were no test failures, but with warnings: %d; Tests executed: %d\n", warnTest, ranTests);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:09:58 +07:00
|
|
|
printf("There were no test failures; Tests executed: %d\n", ranTests);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
int MainAdapter::runAllTests() {
|
2019-09-23 04:09:58 +07:00
|
|
|
Test *curTest = TestRegistry::getFirstTest();
|
|
|
|
int ranTests = 0;
|
2021-04-12 21:51:51 +07:00
|
|
|
int warnTest = 0;
|
2019-09-23 04:09:58 +07:00
|
|
|
while (curTest) {
|
|
|
|
TestResult result;
|
|
|
|
curTest->run(result);
|
|
|
|
ranTests++;
|
|
|
|
|
|
|
|
if (result.getFailureCount()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
if (result.getWarningCount()) {
|
|
|
|
warnTest++;
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:09:58 +07:00
|
|
|
curTest = curTest->getNext();
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
if (warnTest > 0) {
|
|
|
|
printf("There were no test failures, but with warnings: %d; Tests executed: %d\n", warnTest, ranTests);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2019-09-23 04:09:58 +07:00
|
|
|
printf("There were no test failures; Tests executed: %d\n", ranTests);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:51:51 +07:00
|
|
|
int MainAdapter::testsEntryPoint(int argc, char *argv[]) {
|
2019-09-23 04:09:58 +07:00
|
|
|
if (argc < 2 || !strcmp(argv[1], "-all")) {
|
|
|
|
return runAllTests();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "-writeTestInfo")) {
|
|
|
|
if (argc != 3) {
|
|
|
|
printf("-writeTestInfo requires file name\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeAllTestsInfoToFile(argv[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "-runTest")) {
|
|
|
|
if (argc != 4) {
|
|
|
|
printf("-runTest requires group name and test name\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return runTest(argv[2], argv[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "-runGroup")) {
|
|
|
|
if (argc != 3) {
|
|
|
|
printf("-runGroup requires group name\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return runGroup(argv[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Bad argument specified\n");
|
|
|
|
return 1;
|
|
|
|
}
|