Skip to content

Commit c3f170b

Browse files
committed
Don't use macro hacks, instead write test generators directly
1 parent a0611a6 commit c3f170b

File tree

3 files changed

+73
-39
lines changed

3 files changed

+73
-39
lines changed

src/test.cpp

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,61 @@ std::pair<PrimitiveInteger, PrimitiveInteger> Type::getRange() const {
4545
return { 0, 0 };
4646
}
4747

48-
void Test::print(std::ostream &ss) const {
49-
ss << "TEST(" << signature.name << "Test, " << name << ") {\n";
50-
ss << " ASSERT_EQ(";
51-
printFunctionCall(ss);
52-
ss << ", " << signature.returnType->printValue(resultMacroName()) << ");\n";
53-
ss << "}\n";
54-
}
48+
std::string quote(const std::string& s) {
49+
std::stringstream ss;
50+
51+
ss << '"';
52+
bool inEscape = false;
53+
54+
for (char c : s) {
55+
if (c == '\n') ss << "\\n";
56+
else if (c == '\t') ss << "\\t";
57+
else if (c == '"') ss << "\\\"";
58+
else if (c == '\0') {
59+
inEscape = !inEscape;
60+
if (inEscape) {
61+
ss << "\" << ";
62+
} else {
63+
ss << " << \"";
64+
}
65+
} else ss << c;
66+
}
67+
68+
ss << '"';
5569

56-
std::string Test::resultMacroName() const {
57-
return "FUZZ_TEST_" + name;
70+
return ss.str();
5871
}
5972

60-
void Test::printPreludeGenerator(std::ostream &ss) const {
61-
auto macro = resultMacroName();
62-
auto var = macro + "_result";
63-
ss << "auto " << var << " = ";
64-
printFunctionCall(ss);
65-
ss << ";\n";
73+
std::string Test::print() const {
74+
std::stringstream ss;
75+
auto call = printFunctionCall();
76+
ss << "TEST(" << signature.name << "Test, " << name << ") {\n";
77+
ss << " ASSERT_EQ(" << call;
78+
ss << ", " << signature.returnType->printValue('\0' + call + '\0') << ");\n";
79+
ss << "}\n";
80+
return ss.str();
81+
}
6682

67-
ss << "std::cout << \"#define " << macro << " \" << " << var << " << \"\\n\"\n";
83+
std::string Test::printGenerator() const {
84+
return "std::cout << " + quote(print()) + ";\n";
6885
}
6986

70-
void Test::printFunctionCall(std::ostream &ss) const {
71-
ss << signature.name << "(";
87+
std::string Test::printFunctionCall() const {
88+
std::string res;
89+
res += signature.name + "(";
7290

7391
bool first = true;
7492

7593
for (const auto &arg : arguments) {
7694
if (!first) {
77-
ss << ", ";
95+
res += ", ";
7896
}
79-
80-
ss << arg.print();
81-
97+
res += arg.print();
8298
first = false;
8399
}
84100

85-
ss << ")";
101+
res += ")";
102+
return res;
86103
}
87104

88105
std::string printPrimitiveType(PrimitiveType type) {
@@ -112,3 +129,21 @@ Type::ptr primitiveType(PrimitiveType type, PrimitiveInteger min, PrimitiveInteg
112129
Type::ptr pointerTo(Type::ptr type) {
113130
return std::make_shared<Type>(Type { PointerTo { std::move(type) } } );
114131
}
132+
133+
std::string TestSignature::print() const {
134+
std::string res = returnType->print() + " " + name;
135+
res += "(";
136+
137+
bool first = true;
138+
139+
for (const auto &type : parameterTypes) {
140+
if (!first) {
141+
res += ", ";
142+
}
143+
first = false;
144+
res += type->print();
145+
}
146+
147+
res += ");\n";
148+
return res;
149+
}

src/test.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ struct TestSignature {
5050
std::string name;
5151
std::vector<Type::ptr> parameterTypes;
5252
Type::ptr returnType;
53+
54+
[[nodiscard]] std::string print() const;
5355
};
5456

5557
struct Value {
@@ -75,10 +77,9 @@ struct Test
7577
// after test launch
7678
std::optional<Value> returnValue;
7779

78-
[[nodiscard]] std::string resultMacroName() const;
79-
void print(std::ostream &) const;
80-
void printPreludeGenerator(std::ostream &) const;
81-
void printFunctionCall(std::ostream &) const;
80+
[[nodiscard]] std::string print() const;
81+
[[nodiscard]] std::string printGenerator() const;
82+
[[nodiscard]] std::string printFunctionCall() const;
8283

8384
template <class Generator>
8485
static Test generate(Generator &gen, std::string name, TestSignature signature) {

src/test_main.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,24 @@ TestMain &TestMain::add(Test test) {
1515
}
1616

1717
void TestMain::usage() {
18-
std::cerr << "Usage: " << args[0] << " <-g|-t>";
18+
std::cerr << "Usage: " << args[0] << std::endl;
1919
std::exit(1);
2020
}
2121

2222
void TestMain::run() {
23-
if (args.size() != 2) {
23+
if (args.size() != 1) {
2424
usage();
2525
}
2626

27-
if (args[1] == "-g") {
28-
for (const auto& test : tests) {
29-
test.printPreludeGenerator(std::cout);
30-
}
31-
} else if (args[1] == "-t") {
32-
for (const auto& test : tests) {
33-
test.print(std::cout);
34-
}
35-
} else {
36-
usage();
27+
std::cout << "#include <iostream>\n";
28+
for (const auto& test : tests) {
29+
std::cout << test.signature.print();
30+
}
31+
std::cout << "int main() {\n";
32+
for (const auto& test : tests) {
33+
std::cout << test.printGenerator();
3734
}
35+
std::cout << "}\n";
3836
}
3937

4038
TestMain &TestMain::fuzz(const TestSignature& testSignature, int n) {

0 commit comments

Comments
 (0)