Skip to content

Commit 29af4ba

Browse files
committed
DrgnParserTest: Split into reusable components
1 parent 6a8c6c7 commit 29af4ba

File tree

2 files changed

+74
-42
lines changed

2 files changed

+74
-42
lines changed

test/test_drgn_parser.cpp

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <gmock/gmock.h>
21
#include <gtest/gtest.h>
32

43
#include <regex>
@@ -7,63 +6,52 @@
76
// TODO needed?:
87
#include "oi/ContainerInfo.h"
98
#include "oi/OIParser.h"
10-
#include "oi/type_graph/DrgnParser.h"
119
#include "oi/type_graph/Printer.h"
1210
#include "oi/type_graph/TypeGraph.h"
1311
#include "oi/type_graph/Types.h"
12+
#include "test_drgn_parser.h"
1413

1514
using namespace type_graph;
1615
using ::testing::HasSubstr;
1716

1817
// TODO setup google logging for tests so it doesn't appear on terminal by
1918
// default
2019

21-
class DrgnParserTest : public ::testing::Test {
22-
protected:
23-
static void SetUpTestSuite() {
24-
symbols_ = new SymbolService{TARGET_EXE_PATH};
25-
}
26-
27-
static void TearDownTestSuite() {
28-
delete symbols_;
29-
}
30-
31-
std::string run(std::string_view function, bool chaseRawPointers);
32-
void test(std::string_view function,
33-
std::string_view expected,
34-
bool chaseRawPointers = true);
35-
void testContains(std::string_view function,
36-
std::string_view expected,
37-
bool chaseRawPointers = true);
38-
void testMultiCompiler(std::string_view function,
39-
std::string_view expectedClang,
40-
std::string_view expectedGcc,
41-
bool chaseRawPointers = true);
42-
void testMultiCompilerContains(std::string_view function,
43-
std::string_view expectedClang,
44-
std::string_view expectedGcc,
45-
bool chaseRawPointers = true);
46-
47-
static SymbolService* symbols_;
48-
};
49-
5020
SymbolService* DrgnParserTest::symbols_ = nullptr;
5121

52-
std::string DrgnParserTest::run(std::string_view function,
53-
bool chaseRawPointers) {
22+
namespace {
23+
const std::vector<ContainerInfo>& getContainerInfos() {
24+
static auto res = []() {
25+
// TODO more container types, with various template parameter options
26+
ContainerInfo std_vector{"std::vector", SEQ_TYPE, "vector"};
27+
std_vector.stubTemplateParams = {1};
28+
29+
std::vector<ContainerInfo> containers;
30+
containers.emplace_back(std::move(std_vector));
31+
return containers;
32+
}();
33+
return res;
34+
}
35+
} // namespace
36+
37+
DrgnParser DrgnParserTest::getDrgnParser(TypeGraph& typeGraph, bool chaseRawPointers) {
38+
DrgnParser drgnParser{typeGraph, getContainerInfos(), chaseRawPointers};
39+
return drgnParser;
40+
}
41+
42+
drgn_type* DrgnParserTest::getDrgnRoot(std::string_view function) {
5443
irequest req{"entry", std::string{function}, "arg0"};
55-
auto drgnRoot = symbols_->getRootType(req);
44+
auto* drgnRoot = symbols_->getRootType(req)->type.type;
45+
return drgnRoot;
46+
}
5647

48+
std::string DrgnParserTest::run(std::string_view function,
49+
bool chaseRawPointers) {
5750
TypeGraph typeGraph;
58-
// TODO more container types, with various template parameter options
59-
ContainerInfo std_vector{"std::vector", SEQ_TYPE, "vector"};
60-
std_vector.stubTemplateParams = {1};
61-
62-
std::vector<ContainerInfo> containers;
63-
containers.emplace_back(std::move(std_vector));
51+
auto drgnParser = getDrgnParser(typeGraph, chaseRawPointers);
52+
auto *drgnRoot = getDrgnRoot(function);
6453

65-
DrgnParser drgnParser(typeGraph, containers, chaseRawPointers);
66-
Type& type = drgnParser.parse(drgnRoot->type.type);
54+
Type& type = drgnParser.parse(drgnRoot);
6755

6856
std::stringstream out;
6957
Printer printer{out, typeGraph.size()};

test/test_drgn_parser.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include <gmock/gmock.h>
4+
5+
#include "oi/type_graph/DrgnParser.h"
6+
7+
namespace type_graph {
8+
class TypeGraph;
9+
}
10+
class SymbolService;
11+
struct drgn_type;
12+
13+
class DrgnParserTest : public ::testing::Test {
14+
protected:
15+
static void SetUpTestSuite() {
16+
symbols_ = new SymbolService{TARGET_EXE_PATH};
17+
}
18+
19+
static void TearDownTestSuite() {
20+
delete symbols_;
21+
}
22+
23+
static type_graph::DrgnParser getDrgnParser(type_graph::TypeGraph& typeGraph, bool chaseRawPointers);
24+
drgn_type* getDrgnRoot(std::string_view function);
25+
26+
std::string run(std::string_view function, bool chaseRawPointers);
27+
void test(std::string_view function,
28+
std::string_view expected,
29+
bool chaseRawPointers = true);
30+
void testContains(std::string_view function,
31+
std::string_view expected,
32+
bool chaseRawPointers = true);
33+
void testMultiCompiler(std::string_view function,
34+
std::string_view expectedClang,
35+
std::string_view expectedGcc,
36+
bool chaseRawPointers = true);
37+
void testMultiCompilerContains(std::string_view function,
38+
std::string_view expectedClang,
39+
std::string_view expectedGcc,
40+
bool chaseRawPointers = true);
41+
42+
private:
43+
static SymbolService* symbols_;
44+
};

0 commit comments

Comments
 (0)