|
| 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 | +} |
0 commit comments