Skip to content

Commit 663fbca

Browse files
committed
gtest: basic CMake example of using the gtest annotation
1 parent f5c8257 commit 663fbca

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

gtest_annotations/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ The header file includes `undoex-test-annotations.h`, which is included in the
3030
`undoex` path of a UDB release. It also requires the library to be included in
3131
the build - it is distributed for both static and dynamic linking.
3232

33+
## Examples
34+
35+
The [example](examples/) is a very basic test of the listener, built using `cmake`.
36+
If the `undoex` header and library files are not in system paths, you can specify
37+
the path to them using `CMAKE_PREFIX_PATH`. Build the example using:
38+
```sh
39+
export CMAKE_PREFIX_PATH=/path/to/undo/release/undoex
40+
cmake -S . -B build
41+
cmake --build build
42+
```
43+
44+
The resulting binary in `build/listener_test` runs three tests, two that should
45+
pass and one that should fail. If a recording is made (using LiveRecorder or UDB)
46+
annotations will be inserted. These can be viewed using the `info annotations`
47+
command in UDB. You can also travel directly to the start or endof a test using
48+
`ugo annotation`, for example `ugo annotation Fails/run2 u-test-start` will go
49+
to the start of the failing test in this example.
50+
3351
## Limitations
3452

3553
Note that the annotations are inserted at the point in time in the plugin, so

gtest_annotations/examples/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/*
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(basic_undo_gtest_annotation)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
include(FetchContent)
8+
FetchContent_Declare(
9+
googletest
10+
URL https://github.com/google/googletest/archive/23f642ab2317c632d93326c65efd44671c1d9985.zip
11+
)
12+
13+
FetchContent_MakeAvailable(googletest)
14+
15+
enable_testing()
16+
17+
add_executable(
18+
listener_test
19+
)
20+
21+
find_library(
22+
UNDOEX_LIBRARY
23+
NAMES libundoex_pic_x64.a
24+
)
25+
26+
find_path(
27+
UNDOEX_INCLUDE
28+
NAMES undoex-test-annotations.h
29+
)
30+
31+
target_include_directories(
32+
listener_test
33+
PRIVATE
34+
"../"
35+
"${UNDOEX_INCLUDE}"
36+
)
37+
38+
target_sources(
39+
listener_test
40+
PRIVATE listener_test.cc
41+
)
42+
43+
target_link_libraries(
44+
listener_test
45+
GTest::gtest
46+
"${UNDOEX_LIBRARY}"
47+
)
48+
49+
include(GoogleTest)
50+
gtest_discover_tests(listener_test)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "undo_gtest_annotation.h"
4+
5+
TEST(BasicTest, Equality)
6+
{
7+
EXPECT_EQ(1, 1);
8+
EXPECT_TRUE(1 == 1);
9+
}
10+
11+
TEST(BasicTest, Addition)
12+
{
13+
EXPECT_TRUE(1 + 1 == 2);
14+
}
15+
16+
TEST(BasicTest, Fails)
17+
{
18+
EXPECT_TRUE(1 == 2);
19+
}
20+
21+
GTEST_API_ int
22+
main(int argc, char **argv)
23+
{
24+
testing::InitGoogleTest(&argc, argv);
25+
26+
testing::UnitTest::GetInstance()->listeners().Append(new undo_annotation::UndoAnnotationListener);
27+
return RUN_ALL_TESTS();
28+
}

0 commit comments

Comments
 (0)