Skip to content

Commit 7ef176c

Browse files
committed
Test GcovImporter thoroughly
1 parent ee3f22f commit 7ef176c

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

tests/GcovImporter.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (C) 2021 xaizek <xaizek@posteo.net>
2+
//
3+
// This file is part of uncov.
4+
//
5+
// uncov is free software: you can redistribute it and/or modify
6+
// it under the terms of version 3 of the GNU Affero General Public License as
7+
// published by the Free Software Foundation.
8+
//
9+
// uncov is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with uncov. If not, see <http://www.gnu.org/licenses/>.
16+
17+
#include "Catch/catch.hpp"
18+
19+
#include <boost/filesystem/operations.hpp>
20+
#include <boost/filesystem/path.hpp>
21+
#include <boost/scope_exit.hpp>
22+
23+
#include <fstream>
24+
#include <stdexcept>
25+
#include <string>
26+
27+
#include "utils/fs.hpp"
28+
#include "GcovImporter.hpp"
29+
30+
#include "TestUtils.hpp"
31+
32+
namespace fs = boost::filesystem;
33+
34+
TEST_CASE("Need support for at least one intermediate format", "[GcovImporter]")
35+
{
36+
GcovInfo gcovInfo(/*employBinning=*/false, /*jsonFormat=*/false,
37+
/*intermediateFormat=*/false);
38+
REQUIRE_THROWS_AS(GcovImporter("", "", {}, "", gcovInfo),
39+
const std::runtime_error &);
40+
}
41+
42+
TEST_CASE("Plain text format parsed and binning is performed", "[GcovImporter]")
43+
{
44+
TempDir tempDir("gcovimporter");
45+
std::string tempDirPath = tempDir;
46+
47+
fs::create_directory(tempDirPath + "/src");
48+
fs::create_directory(tempDirPath + "/tests");
49+
50+
std::ofstream{tempDirPath + "/src/file.gcno"} << "\n";
51+
std::ofstream{tempDirPath + "/tests/file.gcno"} << "\n";
52+
53+
auto runner = [](std::vector<std::string> &&cmd, const std::string &dir) {
54+
CHECK(cmd.size() == 5);
55+
56+
const fs::path inPath = cmd.back();
57+
const fs::path relPath = inPath.parent_path().filename().string()
58+
/ inPath.filename();
59+
const std::string outName = inPath.parent_path().filename().string()
60+
+ inPath.filename().string();
61+
const std::string outPath = dir + '/' + outName + ".gcov";
62+
63+
std::ofstream{outPath}
64+
<< "file:" << relPath.string() << '\n'
65+
<< "lcount:1,1\n"
66+
<< "lcount:2,0\n";
67+
};
68+
auto prevRunner = GcovImporter::setRunner(runner);
69+
BOOST_SCOPE_EXIT_ALL(prevRunner) {
70+
GcovImporter::setRunner(prevRunner);
71+
};
72+
73+
GcovInfo gcovInfo(/*employBinning=*/true, /*jsonFormat=*/false,
74+
/*intermediateFormat=*/true);
75+
std::vector<File> files =
76+
GcovImporter(tempDirPath, tempDirPath, {}, tempDirPath,
77+
gcovInfo).getFiles();
78+
79+
REQUIRE(files.size() == 2);
80+
CHECK(files[0].getCoveredCount() == 1);
81+
CHECK(files[0].getMissedCount() == 1);
82+
CHECK(files[1].getCoveredCount() == 1);
83+
CHECK(files[1].getMissedCount() == 1);
84+
}
85+
86+
TEST_CASE("JSON format is parsed", "[GcovImporter]")
87+
{
88+
TempDir tempDir("gcovimporter");
89+
std::string tempDirPath = tempDir;
90+
91+
std::ofstream{tempDirPath + "/file.gcno"} << "\n";
92+
93+
auto runner = [](std::vector<std::string> &&/*cmd*/,
94+
const std::string &dir) {
95+
makeGz(dir + "/out.gcov.json.gz", R"({
96+
"files": [
97+
{
98+
"file": "file.gcno",
99+
"lines": [
100+
{ "line_number": 1, "count": 1 },
101+
{ "line_number": 2, "count": 0 }
102+
]
103+
},
104+
{
105+
"file": "/usr/include/whatever.h",
106+
"lines": [ { "line_number": 1, "count": 1 } ]
107+
}
108+
]
109+
})");
110+
};
111+
auto prevRunner = GcovImporter::setRunner(runner);
112+
BOOST_SCOPE_EXIT_ALL(prevRunner) {
113+
GcovImporter::setRunner(prevRunner);
114+
};
115+
116+
GcovInfo gcovInfo(/*employBinning=*/false, /*jsonFormat=*/true,
117+
/*intermediateFormat=*/true);
118+
std::vector<File> files =
119+
GcovImporter(tempDirPath, tempDirPath, {}, tempDirPath,
120+
gcovInfo).getFiles();
121+
122+
REQUIRE(files.size() == 1);
123+
CHECK(files[0].getCoveredCount() == 1);
124+
CHECK(files[0].getMissedCount() == 1);
125+
}

0 commit comments

Comments
 (0)