Skip to content

Commit 70fb719

Browse files
committed
Added ability to output results to Output Window within Visual Studio and fixed a summary bug not reporting count of failures for issue #6
1 parent f54e36b commit 70fb719

File tree

3 files changed

+167
-11
lines changed

3 files changed

+167
-11
lines changed

test/TestClass.hpp

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class TestClass
7373
, m_failure_check_count(0)
7474
, m_test_number(0)
7575
, m_logged(false)
76+
, m_multi_line_errors(false)
7677
{
7778
GetSharedData().SetClassName(class_name);
7879

@@ -320,6 +321,16 @@ class TestClass
320321
return m_current_time;
321322
}
322323

324+
bool GetMultiLineErrors() const
325+
{
326+
return m_multi_line_errors;
327+
}
328+
329+
void LogMultiLineErrors(bool multi_line_errors)
330+
{
331+
m_multi_line_errors = multi_line_errors;
332+
}
333+
323334
void LogTests()
324335
{
325336
if (m_logged)
@@ -556,6 +567,7 @@ class TestClass
556567
if (time_complete && (m_timed_function_calls < min_iterations))
557568
{
558569
++m_failure_check_count;
570+
GetSharedData().IncTotalFailedChecks();
559571
TestString error;
560572
error.Append(m_timed_function_calls);
561573
error.Append(" iterations less than expected ");
@@ -697,6 +709,7 @@ class TestClass
697709
if (failed)
698710
{
699711
++m_failure_check_count;
712+
GetSharedData().IncTotalFailedChecks();
700713
privateCheckFailed(expression, line_number);
701714
}
702715

@@ -890,7 +903,7 @@ class TestClass
890903
}
891904

892905
// Log the output for a failed check macro.
893-
void privateLogFailures()
906+
void privateLogFailuresMultiLine()
894907
{
895908
TestString const indent(' ', error_padding);
896909

@@ -913,17 +926,53 @@ class TestClass
913926
{
914927
m_check_failures.GetSubString(msg, 0, pos + 1, true);
915928
msg.Prepend(indent);
916-
LogWrite(msg);
917929
}
918930
else
919931
{
920-
m_check_failures.Prepend(indent);
921-
LogWrite(m_check_failures);
932+
msg.Assign(indent);
933+
msg.Append(m_check_failures);
934+
m_check_failures.Clear();
935+
}
936+
LogWrite(msg);
937+
}
938+
}
939+
940+
void privateLogFailuresSingleLine()
941+
{
942+
TestString const indent(' ', error_padding);
943+
944+
TestString msg;
945+
946+
// Extract each line from the string until there are no more failed checks.
947+
while (!m_check_failures.IsEmpty())
948+
{
949+
ocl_size_type pos = 0;
950+
msg.Append(indent);
951+
msg.Append(m_filename);
952+
if (m_check_failures.Find('\n', pos))
953+
{
954+
TestString submsg;
955+
m_check_failures.GetSubString(submsg, 0, pos + 1, true);
956+
msg.Append(submsg);
957+
}
958+
else
959+
{
960+
msg.Append(m_check_failures);
922961
m_check_failures.Clear();
923962
}
963+
LogWrite(msg);
964+
msg.Clear();
924965
}
925966
}
926967

968+
void privateLogFailures()
969+
{
970+
if (m_multi_line_errors)
971+
privateLogFailuresMultiLine();
972+
else
973+
privateLogFailuresSingleLine();
974+
}
975+
927976
// Log the number of checks for a tested function, e.g. 5 TESTS or 1 TEST.
928977
static void privateLogCount(TestString const& msg, ocl_size_type count)
929978
{
@@ -941,14 +990,14 @@ class TestClass
941990
{
942991
GetLogger()->WriteEOL();
943992
privateLogCount("Total checks", GetSharedData().GetTotalChecks());
944-
if (GetSharedData().GetTotalNotTested() > 0)
945-
privateLogCount("Total not tested", GetSharedData().GetTotalNotTested());
946993
if (HasSharedFailure())
947994
privateLogCount("Total failed checks", GetSharedData().GetTotalFailedChecks());
995+
if (GetSharedData().GetTotalNotTested() > 0)
996+
privateLogCount("Total functions not tested", GetSharedData().GetTotalNotTested());
948997
privateLogCount("Total functions tested", GetSharedData().GetTotalFunctionsTested());
949998
if (GetSharedData().GetTotalTimedFunctions() > 0)
950999
privateLogCount("Total functions timed", GetSharedData().GetTotalTimedFunctions());
951-
privateLogCount("Total tests", GetSharedData().GetTotalTests());
1000+
privateLogCount("Total function tests", GetSharedData().GetTotalTests());
9521001
if (GetSharedData().GetTotalLeakedTests() > 0)
9531002
privateLogCount("Total memory leaks", GetSharedData().GetTotalLeakedTests());
9541003
}
@@ -983,19 +1032,31 @@ class TestClass
9831032
void privateCheckFailed(TestString const& expression,
9841033
ocl_size_type line_number)
9851034
{
1035+
// If multi line, display line and expression on separate lines.
1036+
// For single line errors, display filename, line number in brackets, ": error: EXPRESSION: " and expression last.
9861037
if (line_number > 0)
9871038
{
988-
GetSharedData().IncTotalFailedChecks();
989-
m_check_failures.Append("LINE: ");
990-
m_check_failures.Append(static_cast<unsigned long>(line_number));
991-
m_check_failures.Append("\n");
1039+
if (m_multi_line_errors)
1040+
{
1041+
m_check_failures.Append("LINE: ");
1042+
m_check_failures.Append(static_cast<unsigned long>(line_number));
1043+
m_check_failures.Append("\n");
1044+
}
1045+
else
1046+
{
1047+
m_check_failures.Append("(");
1048+
m_check_failures.Append(static_cast<unsigned long>(line_number));
1049+
m_check_failures.Append("): error: ");
1050+
}
9921051
}
9931052
if ((expression != NULL) && (*expression != '\0'))
9941053
{
9951054
m_check_failures.Append("EXPRESSION: ");
9961055
m_check_failures.Append(expression);
9971056
m_check_failures.Append("\n");
9981057
}
1058+
else if (!m_multi_line_errors)
1059+
m_check_failures.Append("\n"); // Always ensure at least one new line exists.
9991060
}
10001061

10011062
private:
@@ -1061,6 +1122,10 @@ class TestClass
10611122
// Only log test results once.
10621123
bool m_logged;
10631124

1125+
// Log errors across multiple lines for a single file,
1126+
// or each error with each file on the same line.
1127+
bool m_multi_line_errors;
1128+
10641129
private:
10651130
TestClass(TestClass const&);
10661131
TestClass& operator =(TestClass const&);

test/TestClassSharedData.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ limitations under the License.
1717
#ifndef OCL_GUARD_TEST_TESTCLASSSHAREDDATA_HPP
1818
#define OCL_GUARD_TEST_TESTCLASSSHAREDDATA_HPP
1919

20+
#ifdef _MSC_VER
21+
#include "WinConsoleTestLog.hpp"
22+
#endif
23+
2024
#include "StdioTestLog.hpp"
2125
#include "TestString.hpp"
2226
#include <cstddef>
@@ -60,7 +64,11 @@ class TestClassSharedData
6064
TestLog* GetLogger()
6165
{
6266
if (m_logger == NULL)
67+
#ifdef _MSC_VER
68+
m_logger = new WinConsoleTestLog;
69+
#else
6370
m_logger = new StdioTestLog;
71+
#endif
6472
return m_logger;
6573
}
6674

test/WinConsoleTestLog.hpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2016 Colin Girling
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#ifndef OCL_GUARD_UNIT_TEST_FRAMEWORK_TEST_WINCONSOLETESTLOG_HPP
18+
#define OCL_GUARD_UNIT_TEST_FRAMEWORK_TEST_WINCONSOLETESTLOG_HPP
19+
20+
#ifdef _MSC_VER
21+
22+
#include <Windows.h>
23+
#include "TestLog.hpp"
24+
#include <iostream>
25+
26+
namespace ocl
27+
{
28+
29+
class WinConsoleTestLog : public TestLog
30+
{
31+
public:
32+
static bool IsDebugging()
33+
{
34+
BOOL debugging_status = FALSE;
35+
bool is_debugging;
36+
if (::CheckRemoteDebuggerPresent(GetCurrentProcess(), &debugging_status) != FALSE)
37+
is_debugging = debugging_status != FALSE;
38+
else
39+
is_debugging = false;
40+
return is_debugging;
41+
}
42+
43+
virtual void Write(TestString const& str)
44+
{
45+
static bool is_debugging = IsDebugging();
46+
47+
TestString::size_type len = str.GetLength();
48+
bool eol = (len > 0) && (str[len-1] == '\n');
49+
if (eol)
50+
{
51+
if (len > 1)
52+
{
53+
TestString sub_str = str.GetSubString(0, len - 1);
54+
if (is_debugging)
55+
::OutputDebugStringA(sub_str.Ptr());
56+
else
57+
std::cout << sub_str.Ptr();
58+
59+
}
60+
WriteEOL();
61+
}
62+
else if (is_debugging)
63+
::OutputDebugStringA(str.Ptr());
64+
else
65+
std::cout << str.Ptr();
66+
}
67+
68+
virtual void WriteEOL()
69+
{
70+
static bool is_debugging = IsDebugging();
71+
72+
if (is_debugging)
73+
::OutputDebugStringA("\n");
74+
else
75+
std::cout << std::endl;
76+
}
77+
};
78+
79+
} // namespace ocl
80+
81+
#endif // defined _MSC_VER
82+
83+
#endif // OCL_GUARD_UNIT_TEST_FRAMEWORK_TEST_WINCONSOLETESTLOG_HPP

0 commit comments

Comments
 (0)