1
0
forked from boostorg/core

Compare commits

...

2 Commits

Author SHA1 Message Date
Andrey Semashev
78c87b848c Added API in lightweight_test to obtain the number of test failures.
The new API allows to check if the number of check failures satisfies
a certain condition. The report_errors function returns test_report,
which allows to obtain the error count. It also protects against
unintended comparisons to the error count, which was a misconception
before.

Also added a get_error_count function to explicitly query for the current
number of errors.
2019-06-26 22:48:28 +03:00
Andrey Semashev
6871034931 Revert "report_errors with expected failures (#51)"
This reverts commit 02041f6c9f and
https://github.com/boostorg/core/pull/51.

Reason for revert: The change allows for incorrect result code returned
from the process if the number of failures is a multiple of 256. This
may result in test failures being taken as success by the parent process.
2019-06-26 19:35:02 +03:00
6 changed files with 102 additions and 11 deletions

View File

@@ -49,7 +49,9 @@ When using `lightweight_test.hpp`, *do not forget* to
namespace boost
{
int report_errors();
class test_report;
int get_error_count();
test_report report_errors();
}
``
@@ -212,10 +214,47 @@ nothing and `expr` is not evaluated.
[section report_errors]
``
int boost::report_errors()
boost::test_report boost::report_errors()
``
Return the error count from `main`.
Returns the test report. The report can be converted to an `int` to be returned from `main`.
[endsect]
[section test_report]
``
class test_report
{
public:
int error_count() const noexcept;
operator int() const noexcept;
};
``
Contains information about test results.
``
int test_report::error_count() const noexcept;
``
The method returns the number of failed checks.
``
test_report::operator int() const noexcept;
``
Returns 0 if there are no failed checks and 1 otherwise. This operator is suitable for obtaining result code to return from `main`.
[endsect]
[section get_error_count]
``
int boost::get_error_count();
``
Returns the number of failed checks.
[endsect]

View File

@@ -373,7 +373,58 @@ void test_all_with_impl(FormattedOutputFunction& output,
} // namespace detail
inline int report_errors()
//! Testing report
class test_report
{
private:
int m_error_count;
public:
explicit test_report(int error_count) BOOST_NOEXCEPT :
m_error_count(error_count)
{
}
//! Returns the number of test errors
int error_count() const BOOST_NOEXCEPT { return m_error_count; }
//! Operator for converting to result of main()
operator int () const BOOST_NOEXCEPT { return m_error_count > 0 ? 1 : 0; }
// Protection against comparing report_errors() with the number of errors. Earlier versions of report_errors() did not return
// the number of errors, so such comparisons used to give a wrong result and now cause a hard compile time error.
// Call report_errors().error_count() if you need to check the error count, but make sure you return the correct value from main().
BOOST_DELETED_FUNCTION(bool operator== (int))
BOOST_DELETED_FUNCTION(bool operator!= (int))
BOOST_DELETED_FUNCTION(bool operator< (int))
BOOST_DELETED_FUNCTION(bool operator> (int))
BOOST_DELETED_FUNCTION(bool operator>= (int))
BOOST_DELETED_FUNCTION(bool operator<= (int))
};
#if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
#define BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK = delete
#else
#define BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK
#endif
bool operator== (int, test_report const&) BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK;
bool operator!= (int, test_report const&) BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK;
bool operator< (int, test_report const&) BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK;
bool operator> (int, test_report const&) BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK;
bool operator<= (int, test_report const&) BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK;
bool operator>= (int, test_report const&) BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK;
#undef BOOST_LIGHTWEIGHT_TEST_DELETED_FUNCTION_MARK
//! Returns the number of test errors encountered
inline int get_error_count() BOOST_NOEXCEPT
{
return boost::detail::test_results().errors();
}
//! Prints the number of test errors and returns test report suitable for returning from main()
inline test_report report_errors()
{
boost::detail::test_result& result = boost::detail::test_results();
result.done();
@@ -389,7 +440,8 @@ inline int report_errors()
BOOST_LIGHTWEIGHT_TEST_OSTREAM
<< errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
}
return errors;
return test_report(errors);
}
} // namespace boost

View File

@@ -75,9 +75,10 @@ run lightweight_test_gt_ge_test.cpp ;
run lightweight_test_eq_nullptr.cpp ;
run lightweight_test_test3.cpp ;
run lightweight_test_test4.cpp ;
run lightweight_test_test5.cpp ;
run-fail lightweight_test_all_eq_test.cpp ;
run-fail lightweight_test_all_with_fail.cpp ;
run lightweight_test_all_eq_test.cpp ;
run lightweight_test_all_with_fail.cpp ;
run-fail lightweight_test_fail.cpp ;
run-fail lightweight_test_fail2.cpp ;
@@ -92,7 +93,6 @@ run-fail lightweight_test_fail8.cpp : : : <rtti>off : lightweight_test_fail8_no_
run-fail lightweight_test_fail9.cpp ;
run-fail lightweight_test_fail10.cpp ;
run-fail lightweight_test_fail11.cpp ;
run-fail lightweight_test_fail12.cpp ;
run-fail lightweight_test_lt_fail.cpp ;
run-fail lightweight_test_le_fail.cpp ;
run-fail lightweight_test_gt_fail.cpp ;

View File

@@ -120,5 +120,5 @@ int main()
++test_cases;
}
return boost::report_errors() == test_cases;
return boost::report_errors().error_count() == test_cases ? 0 : 1;
}

View File

@@ -101,5 +101,5 @@ int main()
test_cases += fail_vector();
test_cases += fail_tolerance_predicate();
return boost::report_errors() == test_cases;
return boost::report_errors().error_count() == test_cases ? 0 : 1;
}

View File

@@ -64,5 +64,5 @@ int main()
#endif
return boost::report_errors() == expected;
return boost::report_errors().error_count() == expected ? 0 : 1;
}