Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit c3815dc

Browse files
[[ cpptest ]] Add TAP formatted test logs
A TAP test log can now be specified using a `--tap=<filename>` argument on the test runner.
1 parent 99440b6 commit c3815dc

File tree

6 files changed

+301
-4
lines changed

6 files changed

+301
-4
lines changed

config/cpptest.gypi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
'../util/run-tests.pl',
113113
'<(exec_wrapper)',
114114
'<(PRODUCT_DIR)/test-<(module_name)<(test_suffix)',
115+
'test-<(module_name).log',
115116
],
116117

117118
},

libcpptest/libcpptest.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
[
3232
'googletest/googletest/src/gtest-all.cc',
3333
'src/gtest_main.cpp',
34+
'src/MCTapListener.cpp',
3435
],
3536

3637
'defines':

libcpptest/src/MCTapListener.cpp

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* Copyright (C) 2003-2015 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
#include "MCTapListener.h"
18+
19+
#include <iostream>
20+
21+
using ::testing::UnitTest;
22+
using ::testing::TestCase;
23+
using ::testing::TestInfo;
24+
using ::testing::TestResult;
25+
using ::testing::TestPartResult;
26+
27+
28+
MCTapListener::MCTapListener(const char* log_file)
29+
: m_log(log_file, std::ofstream::out),
30+
m_test_part_result(TestPartResult::kSuccess, "", 0, "")
31+
{
32+
if (!m_log.good()) {
33+
std::cerr
34+
<< "Failed to open TAP log file ("
35+
<< log_file
36+
<< ")"
37+
<< std::endl;
38+
}
39+
}
40+
41+
42+
MCTapListener::~MCTapListener()
43+
{
44+
}
45+
46+
47+
// Fired before any test activity starts.
48+
void MCTapListener::OnTestProgramStart(const UnitTest& unit_test)
49+
{
50+
}
51+
52+
53+
// Fired before each iteration of tests starts. There may be more
54+
// than one iteration if GTEST_FLAG(repeat) is set. iteration is the
55+
// iteration index, starting from 0.
56+
void MCTapListener::OnTestIterationStart(
57+
const UnitTest& unit_test,
58+
int iteration
59+
)
60+
{
61+
int total_count = unit_test.total_test_count();
62+
if (m_log.good() && total_count > 0) {
63+
m_log << "1.." << total_count << std::endl;
64+
}
65+
}
66+
67+
68+
// Fired before environment set-up for each iteration of tests starts.
69+
void MCTapListener::OnEnvironmentsSetUpStart(const UnitTest& unit_test)
70+
{
71+
}
72+
73+
74+
// Fired after environment set-up for each iteration of tests ends.
75+
void MCTapListener::OnEnvironmentsSetUpEnd(const UnitTest& unit_test)
76+
{
77+
78+
}
79+
80+
81+
// Fired before the test case starts.
82+
void MCTapListener::OnTestCaseStart(const TestCase& test_case)
83+
{
84+
if (m_log.good()) {
85+
m_log << "\n### " << test_case.name() << "\n" << std::endl;
86+
}
87+
}
88+
89+
90+
// Fired before the test starts.
91+
void MCTapListener::OnTestStart(const TestInfo& test_info)
92+
{
93+
94+
}
95+
96+
97+
// Fired after a failed assertion or a SUCCEED() invocation.
98+
void MCTapListener::OnTestPartResult(const TestPartResult& test_part_result)
99+
{
100+
m_test_part_result = test_part_result;
101+
}
102+
103+
104+
// Fired after the test ends.
105+
void MCTapListener::OnTestEnd(const TestInfo& test_info)
106+
{
107+
if (m_log.good()) {
108+
109+
const TestResult* test_result = test_info.result();
110+
111+
if (test_result->Passed()) {
112+
m_log << "ok - ";
113+
} else {
114+
m_log << "not ok - ";
115+
}
116+
117+
m_log << test_info.test_case_name() << "." << test_info.name() << "\n";
118+
119+
if (test_result->Passed()) {
120+
m_log
121+
<< "# elapsed time "
122+
<< test_result->elapsed_time()
123+
<< "ms";
124+
125+
} else {
126+
m_log
127+
<< "# "
128+
<< m_test_part_result.file_name()
129+
<< ":"
130+
<< m_test_part_result.line_number()
131+
<< "\n# ";
132+
133+
const std::string& message = m_test_part_result.message();
134+
for (std::string::const_iterator it = message.begin();
135+
it != message.end();
136+
++it) {
137+
if (*it == '\n') {
138+
m_log << "\n# ";
139+
} else {
140+
m_log << *it;
141+
}
142+
}
143+
}
144+
145+
m_log << "\n" << std::endl;
146+
}
147+
}
148+
149+
150+
// Fired after the test case ends.
151+
void MCTapListener::OnTestCaseEnd(const TestCase& test_case)
152+
{
153+
if (m_log.good()) {
154+
m_log
155+
<< "### total elapsed time "
156+
<< test_case.elapsed_time()
157+
<< "ms\n"
158+
<< std::endl;
159+
}
160+
}
161+
162+
163+
// Fired before environment tear-down for each iteration of tests
164+
// starts.
165+
void MCTapListener::OnEnvironmentsTearDownStart(const UnitTest& unit_test)
166+
{
167+
}
168+
169+
170+
// Fired after environment tear-down for each iteration of tests ends.
171+
void MCTapListener::OnEnvironmentsTearDownEnd(const UnitTest& unit_test)
172+
{
173+
}
174+
175+
176+
// Fired after each iteration of tests finishes.
177+
void MCTapListener::OnTestIterationEnd(
178+
const UnitTest& unit_test,
179+
int iteration
180+
)
181+
{
182+
}
183+
184+
185+
// Fired after all test activities have ended.
186+
void MCTapListener::OnTestProgramEnd(const UnitTest& unit_test)
187+
{
188+
}

libcpptest/src/MCTapListener.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* Copyright (C) 2003-2015 LiveCode Ltd.
2+
3+
This file is part of LiveCode.
4+
5+
LiveCode is free software; you can redistribute it and/or modify it under
6+
the terms of the GNU General Public License v3 as published by the Free
7+
Software Foundation.
8+
9+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
10+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
16+
17+
#ifndef __MC_TAP_LISTENER__
18+
#define __MC_TAP_LISTENER__
19+
20+
#include <fstream>
21+
#include "gtest/gtest.h"
22+
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
class MCTapListener : public ::testing::TestEventListener {
26+
public:
27+
MCTapListener(const char* log_file);
28+
virtual ~MCTapListener();
29+
30+
// Fired before any test activity starts.
31+
virtual void OnTestProgramStart(const ::testing::UnitTest& unit_test);
32+
33+
// Fired before each iteration of tests starts. There may be more
34+
// than one iteration if GTEST_FLAG(repeat) is set. iteration is the
35+
// iteration index, starting from 0.
36+
virtual void OnTestIterationStart(
37+
const ::testing::UnitTest& unit_test,
38+
int iteration
39+
);
40+
41+
// Fired before environment set-up for each iteration of tests starts.
42+
virtual void OnEnvironmentsSetUpStart(const ::testing::UnitTest& unit_test);
43+
44+
// Fired after environment set-up for each iteration of tests ends.
45+
virtual void OnEnvironmentsSetUpEnd(const ::testing::UnitTest& unit_test);
46+
47+
// Fired before the test case starts.
48+
virtual void OnTestCaseStart(const ::testing::TestCase& test_case);
49+
50+
// Fired before the test starts.
51+
virtual void OnTestStart(const ::testing::TestInfo& test_info);
52+
53+
// Fired after a failed assertion or a SUCCEED() invocation.
54+
virtual void OnTestPartResult(
55+
const ::testing::TestPartResult& test_part_result
56+
);
57+
58+
// Fired after the test ends.
59+
virtual void OnTestEnd(const ::testing::TestInfo& test_info);
60+
61+
// Fired after the test case ends.
62+
virtual void OnTestCaseEnd(const ::testing::TestCase& test_case);
63+
64+
// Fired before environment tear-down for each iteration of tests starts.
65+
virtual void OnEnvironmentsTearDownStart(
66+
const ::testing::UnitTest& unit_test
67+
);
68+
69+
// Fired after environment tear-down for each iteration of tests ends.
70+
virtual void OnEnvironmentsTearDownEnd(const ::testing::UnitTest& unit_test);
71+
72+
// Fired after each iteration of tests finishes.
73+
virtual void OnTestIterationEnd(
74+
const ::testing::UnitTest& unit_test,
75+
int iteration
76+
);
77+
78+
// Fired after all test activities have ended.
79+
virtual void OnTestProgramEnd(const ::testing::UnitTest& unit_test);
80+
81+
private:
82+
std::ofstream m_log;
83+
::testing::TestPartResult m_test_part_result;
84+
};
85+
86+
////////////////////////////////////////////////////////////////////////////////
87+
#endif

libcpptest/src/gtest_main.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,25 @@ You should have received a copy of the GNU General Public License
1515
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
1616

1717
#include "gtest/gtest.h"
18+
#include "MCTapListener.h"
1819

1920
int main(int argc, char **argv) {
20-
testing::InitGoogleTest(&argc, argv);
21-
return RUN_ALL_TESTS();
21+
testing::InitGoogleTest(&argc, argv);
22+
23+
for (int i = 0; i < argc; i++) {
24+
char* arg = argv[i];
25+
if (arg[0] == '-' &&
26+
arg[1] == '-' &&
27+
arg[2] == 't' &&
28+
arg[3] == 'a' &&
29+
arg[4] == 'p' &&
30+
arg[5] == '=') {
31+
32+
testing::TestEventListeners& listeners
33+
= testing::UnitTest::GetInstance()->listeners();
34+
listeners.Append(new MCTapListener(&arg[6]));
35+
}
36+
}
37+
38+
return RUN_ALL_TESTS();
2239
}

util/run-tests.pl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
$test_wrapper = $ARGV[0];
66
$test_exec = $ARGV[1];
77
$test_dir = dirname($test_exec);
8+
$test_log = $ARGV[2];
9+
10+
$test_command_line = "$test_wrapper $test_exec --tap=$test_log";
811

912
chdir $test_dir or die "Failed to change directory!";
10-
print "$test_wrapper $test_exec\n";
11-
exec "$test_wrapper $test_exec" || die "Failed to run tests!";
13+
print "$test_command_line\n";
14+
exec "$test_command_line" || die "Failed to run tests!";

0 commit comments

Comments
 (0)