Skip to content

Commit 1a5d223

Browse files
committed
Convert tests and the test harness to gtest.
1 parent 0284e38 commit 1a5d223

23 files changed

+1145
-1857
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "third_party/googletest"]
2+
path = third_party/googletest
3+
url = https://github.com/google/googletest

AMBuildScript

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class KEConfig(object):
154154
binary = compiler.Program(name)
155155
return binary
156156

157+
def StaticLibrary(self, context, name):
158+
compiler = context.compiler.clone()
159+
binary = compiler.StaticLibrary(name)
160+
return binary
161+
157162
KE = KEConfig()
158163
KE.configure()
159164

tests/AMBuild.tests

+12
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,29 @@
1818
#
1919
import os
2020

21+
binary = KE.StaticLibrary(builder, 'libgtest')
22+
binary.compiler.includes += [
23+
os.path.join(builder.sourcePath, 'third_party', 'googletest', 'googletest', 'include'),
24+
os.path.join(builder.sourcePath, 'third_party', 'googletest', 'googletest'),
25+
]
26+
binary.sources += [
27+
os.path.join('..', 'third_party', 'googletest', 'googletest', 'src', 'gtest-all.cc'),
28+
]
29+
libgtest = builder.Add(binary)
30+
2131
binary = KE.Program(builder, 'testrunner')
2232
compiler = binary.compiler
2333
compiler.includes += [
2434
os.path.join(builder.sourcePath, 'amtl'),
2535
os.path.join(builder.sourcePath),
36+
os.path.join(builder.sourcePath, 'third_party', 'googletest', 'googletest', 'include'),
2637
]
2738

2839
if compiler.like('msvc'):
2940
compiler.linkflags.append('/SUBSYSTEM:CONSOLE')
3041
if builder.target_platform == 'linux':
3142
compiler.linkflags.append('-ldl')
43+
compiler.linkflags.append(libgtest.binary)
3244

3345
binary.sources += [
3446
'main.cpp',

tests/main.cpp

+3-17
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,15 @@
3131
#include <stdio.h>
3232
#include <stdlib.h>
3333
#include <string.h>
34+
#include <gtest/gtest.h>
3435
#include "runner.h"
3536

3637
using namespace ke;
3738

38-
Test* Test::head = nullptr;
39-
4039
int main(int argc, char** argv)
4140
{
42-
Test* test = Test::first();
43-
while (test) {
44-
if (argc >= 2 && strcmp(argv[1], test->name()) != 0) {
45-
test = test->next();
46-
continue;
47-
}
48-
fprintf(stdout, "Testing %s... \n", test->name());
49-
if (!test->Run()) {
50-
fprintf(stdout, "TEST:%s FAIL\n", test->name());
51-
return 1;
52-
}
53-
fprintf(stdout, "TEST:%s OK\n", test->name());
54-
test = test->next();
55-
}
56-
return 0;
41+
::testing::InitGoogleTest(&argc, argv);
42+
return RUN_ALL_TESTS();
5743
}
5844

5945
#if defined(__GNUC__)

tests/runner.h

+1-62
Original file line numberDiff line numberDiff line change
@@ -37,67 +37,6 @@
3737

3838
namespace ke {
3939

40-
class Test
41-
{
42-
public:
43-
Test(const char* name)
44-
: name_(name),
45-
next_(head)
46-
{
47-
head = this;
48-
}
49-
50-
virtual bool Run() = 0;
51-
52-
const char* name() const {
53-
return name_;
54-
}
55-
Test* next() const {
56-
return next_;
57-
}
58-
59-
static inline Test* first() {
60-
return head;
61-
}
62-
63-
private:
64-
static Test* head;
65-
66-
private:
67-
const char* name_;
68-
Test* next_;
69-
};
70-
71-
static inline bool
72-
check(bool condition, const char* fmt, ...)
73-
{
74-
FILE* fp = condition ? stdout : stderr;
75-
if (condition)
76-
fprintf(fp, " -- Ok: ");
77-
else
78-
fprintf(fp, " -- Failure: ");
79-
va_list ap;
80-
va_start(ap, fmt);
81-
vfprintf(fp, fmt, ap);
82-
va_end(ap);
83-
fprintf(fp, "\n");
84-
return condition;
85-
}
86-
87-
static inline bool
88-
check_silent(bool condition, const char* fmt, ...)
89-
{
90-
if (condition)
91-
return true;
92-
fprintf(stderr, " -- Failure: ");
93-
va_list ap;
94-
va_start(ap, fmt);
95-
vfprintf(stderr, fmt, ap);
96-
va_end(ap);
97-
fprintf(stderr, "\n");
98-
return false;
99-
}
100-
10140
class FallibleMalloc
10241
{
10342
public:
@@ -149,7 +88,7 @@ class FallibleMalloc
14988
size_t overflows_;
15089
};
15190

152-
}
91+
} // namespace ke
15392

15493
#endif // _include_amtl_runner_h_
15594

0 commit comments

Comments
 (0)