|
5 | 5 | #include "utils/CollectionUtils.h"
|
6 | 6 | #include "Paths.h"
|
7 | 7 |
|
8 |
| -void printer::CoverageAndResultsStatisticsPrinter::write(const utbot::ProjectContext &projectContext, |
9 |
| - const Coverage::TestResultMap &testsResultMap, |
10 |
| - const Coverage::CoverageMap &coverageMap) { |
11 |
| - for (auto const &[testPath, testsResult]: testsResultMap) { |
12 |
| - fs::path sourcePath = Paths::testPathToSourcePath(projectContext, testPath); |
13 |
| - Coverage::FileCoverage fileCoverage = CollectionUtils::getOrDefault(coverageMap, |
14 |
| - sourcePath, |
15 |
| - Coverage::FileCoverage()); |
16 |
| - insert({sourcePath, FileCoverageAndResultsStatistics(testsResult, fileCoverage)}); |
17 |
| - } |
18 |
| - std::vector<std::string> metricNames = { |
19 |
| - "filename", "executionTime (ms)", |
20 |
| - "totalTestsNumber", "passedTestsNumber", "failedTestsNumber", "deathTestsNumber", "interruptedTestsNumber", |
21 |
| - "totalLinesNumber", "coveredLinesNumber", "lineCoverageRatio (%)" |
22 |
| - }; |
23 |
| - std::string header = StringUtils::joinWith(metricNames, ","); |
24 |
| - std::stringstream ss; |
25 |
| - ss << header << '\n'; |
26 |
| - for (auto const &[sourcePath, statistics]: *this) { |
27 |
| - ss << fs::relative(sourcePath, projectContext.projectPath) << ","; |
28 |
| - ss << statistics.totalExecutionTimeMs << ','; |
29 |
| - ss << statistics.totalTestsNum << ","; |
30 |
| - ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_PASSED, 0u) |
| 8 | +namespace printer { |
| 9 | + std::ostream &operator<<(std::ostream &os, const FileCoverageAndResultsStatistics &statistics) { |
| 10 | + // total tests execution file |
| 11 | + os << statistics.totalExecutionTimeMs << ','; |
| 12 | + // total number of tests |
| 13 | + os << statistics.totalTestsNum << ","; |
| 14 | + // number of passed tests |
| 15 | + os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_PASSED, 0u) |
31 | 16 | << ',';
|
32 |
| - ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_FAILED, 0u) |
| 17 | + // number of failed tests |
| 18 | + os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_FAILED, 0u) |
33 | 19 | << ',';
|
34 |
| - ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_DEATH, 0u) << ','; |
35 |
| - ss << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_INTERRUPTED, 0u) |
| 20 | + // number of death tests |
| 21 | + os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_DEATH, 0u) << ','; |
| 22 | + // number of interrupted tests |
| 23 | + os << CollectionUtils::getOrDefault(statistics.testsWithStatusNum, testsgen::TestStatus::TEST_INTERRUPTED, 0u) |
36 | 24 | << ',';
|
37 | 25 |
|
38 |
| - uint32_t coveredLinesNum = |
39 |
| - statistics.coverage.fullCoverageLines.size() + statistics.coverage.partialCoverageLines.size(); |
40 |
| - uint32_t totalLinesNum = coveredLinesNum + statistics.coverage.noCoverageLines.size(); |
| 26 | + uint32_t coveredLinesNum = statistics.fullCoverageLinesNum + statistics.partialCoverageLinesNum; |
| 27 | + uint32_t totalLinesNum = coveredLinesNum + statistics.noCoverageLinesNum; |
41 | 28 | double lineCoverageRatio = 0;
|
42 | 29 | if (totalLinesNum != 0) {
|
43 | 30 | lineCoverageRatio = 100.0 * coveredLinesNum / totalLinesNum;
|
44 | 31 | }
|
45 |
| - ss << totalLinesNum << ',' << coveredLinesNum << ','; |
46 |
| - ss << std::fixed << lineCoverageRatio << '\n'; |
| 32 | + // total number of lines and number of covered lines |
| 33 | + os << totalLinesNum << ',' << coveredLinesNum << ','; |
| 34 | + // line coverage ratio |
| 35 | + os << std::fixed << lineCoverageRatio; |
| 36 | + return os; |
| 37 | + } |
| 38 | + |
| 39 | + void CoverageAndResultsStatisticsPrinter::write(const utbot::ProjectContext &projectContext, |
| 40 | + const Coverage::TestResultMap &testsResultMap, |
| 41 | + const Coverage::CoverageMap &coverageMap) { |
| 42 | + for (auto const &[testPath, testsResult]: testsResultMap) { |
| 43 | + fs::path sourcePath = Paths::testPathToSourcePath(projectContext, testPath); |
| 44 | + Coverage::FileCoverage fileCoverage = CollectionUtils::getOrDefault(coverageMap, |
| 45 | + sourcePath, |
| 46 | + Coverage::FileCoverage()); |
| 47 | + insert({sourcePath, FileCoverageAndResultsStatistics(testsResult, fileCoverage)}); |
| 48 | + } |
| 49 | + std::vector<std::string> metricNames = { |
| 50 | + "filename", "executionTime (ms)", |
| 51 | + "totalTestsNumber", "passedTestsNumber", "failedTestsNumber", "deathTestsNumber", |
| 52 | + "interruptedTestsNumber", |
| 53 | + "totalLinesNumber", "coveredLinesNumber", "lineCoverageRatio (%)" |
| 54 | + }; |
| 55 | + std::string header = StringUtils::joinWith(metricNames, ","); |
| 56 | + std::stringstream ss; |
| 57 | + ss << header << '\n'; |
| 58 | + |
| 59 | + FileCoverageAndResultsStatistics total; |
| 60 | + for (auto const &[sourcePath, statistics]: *this) { |
| 61 | + total += statistics; |
| 62 | + ss << fs::relative(sourcePath, projectContext.projectPath).string() << ","; |
| 63 | + ss << statistics << '\n'; |
| 64 | + } |
| 65 | + ss << "Total,"; |
| 66 | + ss << total << '\n'; |
| 67 | + fs::path resultsFilePath = resultsDirectory / "coverage-and-results-stats.csv"; |
| 68 | + FileSystemUtils::writeToFile(resultsFilePath, ss.str()); |
| 69 | + LOG_S(INFO) << StringUtils::stringFormat("See statistics info here: %s", resultsFilePath); |
| 70 | + } |
| 71 | + |
| 72 | + FileCoverageAndResultsStatistics::FileCoverageAndResultsStatistics( |
| 73 | + const Coverage::FileTestsResult &testsResult, |
| 74 | + const Coverage::FileCoverage &fileCoverage) { |
| 75 | + totalTestsNum = 0; |
| 76 | + totalExecutionTimeMs = 0; |
| 77 | + for (const auto &[_, testResult]: testsResult) { |
| 78 | + totalTestsNum++; |
| 79 | + testsWithStatusNum[testResult.status()]++; |
| 80 | + totalExecutionTimeMs += testResult.executiontimems(); |
| 81 | + } |
| 82 | + fullCoverageLinesNum = fileCoverage.fullCoverageLines.size(); |
| 83 | + partialCoverageLinesNum = fileCoverage.partialCoverageLines.size(); |
| 84 | + noCoverageLinesNum = fileCoverage.noCoverageLines.size(); |
| 85 | + } |
| 86 | + |
| 87 | + FileCoverageAndResultsStatistics & |
| 88 | + FileCoverageAndResultsStatistics::operator+=(const FileCoverageAndResultsStatistics &other) { |
| 89 | + totalExecutionTimeMs += other.totalExecutionTimeMs; |
| 90 | + totalTestsNum += other.totalTestsNum; |
| 91 | + for (auto &[status, testsNum]: testsWithStatusNum) { |
| 92 | + testsNum += CollectionUtils::getOrDefault(other.testsWithStatusNum, status, 0u); |
| 93 | + } |
| 94 | + fullCoverageLinesNum += other.fullCoverageLinesNum; |
| 95 | + partialCoverageLinesNum += other.partialCoverageLinesNum; |
| 96 | + noCoverageLinesNum += other.noCoverageLinesNum; |
| 97 | + return *this; |
47 | 98 | }
|
48 |
| - fs::path resultsFilePath = resultsDirectory / (TimeUtils::getDate() + "-stats.csv"); |
49 |
| - FileSystemUtils::writeToFile(resultsFilePath, ss.str()); |
50 |
| - LOG_S(INFO) << StringUtils::stringFormat("See statistics info here: %s", resultsFilePath); |
51 |
| -} |
52 | 99 |
|
53 |
| -printer::FileCoverageAndResultsStatistics::FileCoverageAndResultsStatistics( |
54 |
| - const Coverage::FileTestsResult &testsResult, |
55 |
| - Coverage::FileCoverage fileCoverage) { |
56 |
| - coverage = std::move(fileCoverage); |
57 |
| - totalTestsNum = 0; |
58 |
| - totalExecutionTimeMs = 0; |
59 |
| - for (const auto &[_, testResult]: testsResult) { |
60 |
| - totalTestsNum++; |
61 |
| - testsWithStatusNum[testResult.status()]++; |
62 |
| - totalExecutionTimeMs += testResult.executiontimems(); |
| 100 | + FileCoverageAndResultsStatistics |
| 101 | + FileCoverageAndResultsStatistics::operator+(FileCoverageAndResultsStatistics other) const { |
| 102 | + other += *this; |
| 103 | + return other; |
63 | 104 | }
|
64 | 105 | }
|
0 commit comments