Skip to content

Commit

Permalink
Add unit test for covered lines
Browse files Browse the repository at this point in the history
  • Loading branch information
domohuhn committed Mar 24, 2024
1 parent 5c9bbe2 commit 8bdb1dc
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/reports/create_test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// License: BSD-3-Clause
// See LICENSE for the full text of the license

import 'package:mutation_test/src/configuration/coverage.dart';
import 'package:mutation_test/src/core/mutated_line.dart';
import 'package:mutation_test/src/reports/report_data.dart';
import 'package:mutation_test/src/core/mutation.dart';
Expand Down Expand Up @@ -31,6 +32,15 @@ ReportData createTestData() {
return reporter;
}

ProjectLineCoverage createCodeCoverage() {
FileCoverage f = FileCoverage('path.dart');
f.coverage[1] = 10;
f.coverage[2] = 0;
ProjectLineCoverage coverage = ProjectLineCoverage();
coverage.coveredFiles['path.dart'] = f;
return coverage;
}

ReportData createTestDataWithNotCovered() {
var reporter = createTestData();
reporter.addTestReport(
Expand Down
114 changes: 114 additions & 0 deletions test/reports/html_reporter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ void main() {
expect(result.substring(0, end1), htmlSourceFileReport.substring(0, end1));
expect(result.substring(start2), htmlSourceFileReport.substring(start2));
});

test('Create html source report with coverage', () {
final reporter = createTestData();
final coverage = createCodeCoverage();
var result = createSourceHtmlFile(
reporter, reporter.testedFiles.values.first, 'test.html', coverage);
// exclude report creation time
final end1 = htmlSourceFileReport.indexOf('Date:') + 49;
final start2 = end1 + 26;
expect(result.substring(0, end1),
htmlSourceFileReportWithCoverage.substring(0, end1));
expect(result.substring(start2),
htmlSourceFileReportWithCoverage.substring(start2));
});
}

final emptyTopLevel = '<!DOCTYPE html>\n'
Expand Down Expand Up @@ -210,3 +224,103 @@ final htmlSourceFileReport = '<!DOCTYPE html>\n'
'</body>\n'
'</html>\n'
'';

final htmlSourceFileReportWithCoverage = '<!DOCTYPE html>\n'
'<html lang="en">\n'
'<head>\n'
'<meta name="viewport" content="width=device-width, initial-scale=1">\n'
'<style>\n${getCSSFileContents()}'
'</style>\n'
'</head>\n'
'<body>\n'
'<table width ="100%" cellspacing="0" border="0">\n'
' <tr><td class="title">Mutation test report</td></tr>\n'
' <tr><td><hr class="ruler"/></td></tr>\n'
'\n'
' <tr>\n'
' <td width="100%">\n'
' <table width="100%" cellpadding="1" border="0">\n'
' <tbody><tr>\n'
' <td class="ItemLabel" width="10%">Current display:</td>\n'
' <td class="ItemText" width="35%">path.dart - <a href="./test.html">back to top</a></td>\n'
' <td width="10%"></td>\n'
' <td class="MiddleHeader" width="15%">Detected</td>\n'
' <td class="MiddleHeader" width="15%">Total</td>\n'
' <td class="MiddleHeader" width="15%">Percentage</td>\n'
' </tr>\n'
'\n'
' <tr>\n'
' <td class="ItemLabel" width="10%">Date:</td>\n'
' <td class="ItemText" width="35%">2022-10-30 16:37:03.526343</td>\n'
' <td class="ItemLabel" width="10%">Mutations:</td>\n'
' <td class="ItemReport" width="15%">1</td>\n'
' <td class="ItemReport" width="15%">3</td>\n'
' <td class="ItemReportLow" width="15%">33.3 %</td>\n'
' </tr>\n'
' \n'
' <tr>\n'
' <td class="ItemLabel" width="10%">Builtin rules:</td>\n'
' <td class="ItemText" width="35%">true</td>\n'
' <td class="ItemLabel" width="10%">Blocked:</td>\n'
' <td class="ItemReport" width="15%">1</td>\n'
' <td class="ItemReport" width="15%">3</td>\n'
' <td class="ItemReportMedium" width="15%">33.3 %</td>\n'
' </tr>\n'
' </tbody>\n'
' </table>\n'
' </td>\n'
' </tr>\n'
' \n'
'\n'
' \n'
' <tr><td><hr class="ruler"/></td></tr>\n'
'</table>\n'
'\n'
'<pre class="fileHeader"> Source code</pre>\n'
'<pre class="fileContents">\n'
'<a name="1"><button class="collapsible problem"><pre class="fileContents"><span class="lineNumber"><span class="covered"> 1 </span></span>var x = 0;</pre></button>\n'
'<div class="content">\n'
'<b>Undetected mutations:</b>\n'
'<table class="mutationTable" width="100%">\n'
'<tr><td class="mutationLabel" width="5%">1 :</td><td class="mutationText" width="87%"><span class="addedLine">+ <span class="changedTokens">var x</span> = a;</span></td><td class="match">[0-9]+</td></tr></table>\n'
'<b>Detected mutations:</b>\n'
'<table class="mutationTable" width="100%">\n'
'<tr><td class="mutationLabel" width="5%">1 :</td><td class="mutationText" width="87%"><span class="addedLine">+ <span class="changedTokens">var x </span>= -0;</span></td><td class="match">[0-9]+</td></tr></table>\n'
'<b>Mutations that caused a time out:</b>\n'
'<table class="mutationTable" width="100%">\n'
'<tr><td class="mutationLabel" width="5%">1 :</td><td class="mutationText" width="87%"><span class="addedLine">+ <span class="changedTokens">var x</span> = c;</span></td><td class="match">[0-9]+<span class="tooltip">Id: testId</span></td></tr></table>\n'
'\n'
'</div></a><a name="2"><span class="lineNumber"><span class="instrumented"> 2 </span></span></a>\n'
'<a name="3"><span class="lineNumber"> 3 </span>// mooo</a>\n'
'<a name="4"><span class="lineNumber"> 4 </span></a>\n'
'</pre>\n'
'<script>\n'
'var coll = document.getElementsByClassName("collapsible");\n'
'var i;\n'
'\n'
'for (i = 0; i < coll.length; i++) {\n'
' coll[i].addEventListener("click", function() {\n'
' this.classList.toggle("active");\n'
' var content = this.nextElementSibling;\n'
' if (content.style.maxHeight){\n'
' content.style.maxHeight = null;\n'
' } else {\n'
' content.style.maxHeight = content.scrollHeight + "px";\n'
' }\n'
'\tfor (k = 0; k < coll.length; k++) {\n'
' var content2 = coll[k].nextElementSibling;\n'
' if (content2.style.maxHeight && content.parentElement == content2){\n'
' content2.style.maxHeight = content2.scrollHeight + content.scrollHeight + "px";\n'
' }\n'
' \n'
' }\n'
' \n'
' });\n'
'}\n'
'</script><table width ="100%" cellspacing="0" border="0">\n'
' <tr><td><hr class="ruler"/></td></tr>\n'
' <tr><td class="footer">Generated by ${mutationTestVersion()}</td></tr>\n'
'</table>\n'
'</body>\n'
'</html>\n'
'';

0 comments on commit 8bdb1dc

Please sign in to comment.