mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-10-15 07:14:45 +02:00
Ran clang-format over all .h and .cpp files.
clang-format -i $(find . -name '*.h' -or -name '*.cpp')
This commit is contained in:
@@ -4,15 +4,15 @@
|
||||
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
||||
|
||||
#ifndef JSONTEST_H_INCLUDED
|
||||
# define JSONTEST_H_INCLUDED
|
||||
#define JSONTEST_H_INCLUDED
|
||||
|
||||
# include <json/config.h>
|
||||
# include <json/value.h>
|
||||
# include <json/writer.h>
|
||||
# include <stdio.h>
|
||||
# include <deque>
|
||||
# include <sstream>
|
||||
# include <string>
|
||||
#include <json/config.h>
|
||||
#include <json/value.h>
|
||||
#include <json/writer.h>
|
||||
#include <stdio.h>
|
||||
#include <deque>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
@@ -20,8 +20,6 @@
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
// //////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
/** \brief Unit testing framework.
|
||||
* \warning: all assertions are non-aborting, test case execution will continue
|
||||
* even if an assertion namespace.
|
||||
@@ -30,244 +28,239 @@
|
||||
*/
|
||||
namespace JsonTest {
|
||||
|
||||
class Failure {
|
||||
public:
|
||||
const char *file_;
|
||||
unsigned int line_;
|
||||
std::string expr_;
|
||||
std::string message_;
|
||||
unsigned int nestingLevel_;
|
||||
};
|
||||
|
||||
class Failure
|
||||
{
|
||||
public:
|
||||
const char *file_;
|
||||
unsigned int line_;
|
||||
std::string expr_;
|
||||
std::string message_;
|
||||
unsigned int nestingLevel_;
|
||||
};
|
||||
/// Context used to create the assertion callstack on failure.
|
||||
/// Must be a POD to allow inline initialisation without stepping
|
||||
/// into the debugger.
|
||||
struct PredicateContext {
|
||||
typedef unsigned int Id;
|
||||
Id id_;
|
||||
const char *file_;
|
||||
unsigned int line_;
|
||||
const char *expr_;
|
||||
PredicateContext *next_;
|
||||
/// Related Failure, set when the PredicateContext is converted
|
||||
/// into a Failure.
|
||||
Failure *failure_;
|
||||
};
|
||||
|
||||
class TestResult {
|
||||
public:
|
||||
TestResult();
|
||||
|
||||
/// Context used to create the assertion callstack on failure.
|
||||
/// Must be a POD to allow inline initialisation without stepping
|
||||
/// into the debugger.
|
||||
struct PredicateContext
|
||||
{
|
||||
typedef unsigned int Id;
|
||||
Id id_;
|
||||
const char *file_;
|
||||
unsigned int line_;
|
||||
const char *expr_;
|
||||
PredicateContext *next_;
|
||||
/// Related Failure, set when the PredicateContext is converted
|
||||
/// into a Failure.
|
||||
Failure *failure_;
|
||||
};
|
||||
/// \internal Implementation detail for assertion macros
|
||||
/// Not encapsulated to prevent step into when debugging failed assertions
|
||||
/// Incremented by one on assertion predicate entry, decreased by one
|
||||
/// by addPredicateContext().
|
||||
PredicateContext::Id predicateId_;
|
||||
|
||||
class TestResult
|
||||
{
|
||||
public:
|
||||
TestResult();
|
||||
/// \internal Implementation detail for predicate macros
|
||||
PredicateContext *predicateStackTail_;
|
||||
|
||||
/// \internal Implementation detail for assertion macros
|
||||
/// Not encapsulated to prevent step into when debugging failed assertions
|
||||
/// Incremented by one on assertion predicate entry, decreased by one
|
||||
/// by addPredicateContext().
|
||||
PredicateContext::Id predicateId_;
|
||||
void setTestName(const std::string &name);
|
||||
|
||||
/// \internal Implementation detail for predicate macros
|
||||
PredicateContext *predicateStackTail_;
|
||||
/// Adds an assertion failure.
|
||||
TestResult &
|
||||
addFailure(const char *file, unsigned int line, const char *expr = 0);
|
||||
|
||||
void setTestName( const std::string &name );
|
||||
/// Removes the last PredicateContext added to the predicate stack
|
||||
/// chained list.
|
||||
/// Next messages will be targed at the PredicateContext that was removed.
|
||||
TestResult &popPredicateContext();
|
||||
|
||||
/// Adds an assertion failure.
|
||||
TestResult &addFailure( const char *file, unsigned int line,
|
||||
const char *expr = 0 );
|
||||
bool failed() const;
|
||||
|
||||
/// Removes the last PredicateContext added to the predicate stack
|
||||
/// chained list.
|
||||
/// Next messages will be targed at the PredicateContext that was removed.
|
||||
TestResult &popPredicateContext();
|
||||
void printFailure(bool printTestName) const;
|
||||
|
||||
bool failed() const;
|
||||
// Generic operator that will work with anything ostream can deal with.
|
||||
template <typename T> TestResult &operator<<(const T &value) {
|
||||
std::ostringstream oss;
|
||||
oss.precision(16);
|
||||
oss.setf(std::ios_base::floatfield);
|
||||
oss << value;
|
||||
return addToLastFailure(oss.str());
|
||||
}
|
||||
|
||||
void printFailure( bool printTestName ) const;
|
||||
// Specialized versions.
|
||||
TestResult &operator<<(bool value);
|
||||
// std:ostream does not support 64bits integers on all STL implementation
|
||||
TestResult &operator<<(Json::Int64 value);
|
||||
TestResult &operator<<(Json::UInt64 value);
|
||||
|
||||
// Generic operator that will work with anything ostream can deal with.
|
||||
template <typename T>
|
||||
TestResult &operator << ( const T& value ) {
|
||||
std::ostringstream oss;
|
||||
oss.precision( 16 );
|
||||
oss.setf( std::ios_base::floatfield );
|
||||
oss << value;
|
||||
return addToLastFailure(oss.str());
|
||||
}
|
||||
private:
|
||||
TestResult &addToLastFailure(const std::string &message);
|
||||
unsigned int getAssertionNestingLevel() const;
|
||||
/// Adds a failure or a predicate context
|
||||
void addFailureInfo(const char *file,
|
||||
unsigned int line,
|
||||
const char *expr,
|
||||
unsigned int nestingLevel);
|
||||
static std::string indentText(const std::string &text,
|
||||
const std::string &indent);
|
||||
|
||||
// Specialized versions.
|
||||
TestResult &operator << ( bool value );
|
||||
// std:ostream does not support 64bits integers on all STL implementation
|
||||
TestResult &operator << ( Json::Int64 value );
|
||||
TestResult &operator << ( Json::UInt64 value );
|
||||
typedef std::deque<Failure> Failures;
|
||||
Failures failures_;
|
||||
std::string name_;
|
||||
PredicateContext rootPredicateNode_;
|
||||
PredicateContext::Id lastUsedPredicateId_;
|
||||
/// Failure which is the target of the messages added using operator <<
|
||||
Failure *messageTarget_;
|
||||
};
|
||||
|
||||
private:
|
||||
TestResult &addToLastFailure( const std::string &message );
|
||||
unsigned int getAssertionNestingLevel() const;
|
||||
/// Adds a failure or a predicate context
|
||||
void addFailureInfo( const char *file, unsigned int line,
|
||||
const char *expr, unsigned int nestingLevel );
|
||||
static std::string indentText( const std::string &text,
|
||||
const std::string &indent );
|
||||
class TestCase {
|
||||
public:
|
||||
TestCase();
|
||||
|
||||
typedef std::deque<Failure> Failures;
|
||||
Failures failures_;
|
||||
std::string name_;
|
||||
PredicateContext rootPredicateNode_;
|
||||
PredicateContext::Id lastUsedPredicateId_;
|
||||
/// Failure which is the target of the messages added using operator <<
|
||||
Failure *messageTarget_;
|
||||
};
|
||||
virtual ~TestCase();
|
||||
|
||||
void run(TestResult &result);
|
||||
|
||||
class TestCase
|
||||
{
|
||||
public:
|
||||
TestCase();
|
||||
virtual const char *testName() const = 0;
|
||||
|
||||
virtual ~TestCase();
|
||||
protected:
|
||||
TestResult *result_;
|
||||
|
||||
void run( TestResult &result );
|
||||
private:
|
||||
virtual void runTestCase() = 0;
|
||||
};
|
||||
|
||||
virtual const char *testName() const = 0;
|
||||
/// Function pointer type for TestCase factory
|
||||
typedef TestCase *(*TestCaseFactory)();
|
||||
|
||||
protected:
|
||||
TestResult *result_;
|
||||
class Runner {
|
||||
public:
|
||||
Runner();
|
||||
|
||||
private:
|
||||
virtual void runTestCase() = 0;
|
||||
};
|
||||
/// Adds a test to the suite
|
||||
Runner &add(TestCaseFactory factory);
|
||||
|
||||
/// Function pointer type for TestCase factory
|
||||
typedef TestCase *(*TestCaseFactory)();
|
||||
/// Runs test as specified on the command-line
|
||||
/// If no command-line arguments are provided, run all tests.
|
||||
/// If --list-tests is provided, then print the list of all test cases
|
||||
/// If --test <testname> is provided, then run test testname.
|
||||
int runCommandLine(int argc, const char *argv[]) const;
|
||||
|
||||
class Runner
|
||||
{
|
||||
public:
|
||||
Runner();
|
||||
/// Runs all the test cases
|
||||
bool runAllTest(bool printSummary) const;
|
||||
|
||||
/// Adds a test to the suite
|
||||
Runner &add( TestCaseFactory factory );
|
||||
/// Returns the number of test case in the suite
|
||||
unsigned int testCount() const;
|
||||
|
||||
/// Runs test as specified on the command-line
|
||||
/// If no command-line arguments are provided, run all tests.
|
||||
/// If --list-tests is provided, then print the list of all test cases
|
||||
/// If --test <testname> is provided, then run test testname.
|
||||
int runCommandLine( int argc, const char *argv[] ) const;
|
||||
/// Returns the name of the test case at the specified index
|
||||
std::string testNameAt(unsigned int index) const;
|
||||
|
||||
/// Runs all the test cases
|
||||
bool runAllTest( bool printSummary ) const;
|
||||
/// Runs the test case at the specified index using the specified TestResult
|
||||
void runTestAt(unsigned int index, TestResult &result) const;
|
||||
|
||||
/// Returns the number of test case in the suite
|
||||
unsigned int testCount() const;
|
||||
static void printUsage(const char *appName);
|
||||
|
||||
/// Returns the name of the test case at the specified index
|
||||
std::string testNameAt( unsigned int index ) const;
|
||||
private: // prevents copy construction and assignment
|
||||
Runner(const Runner &other);
|
||||
Runner &operator=(const Runner &other);
|
||||
|
||||
/// Runs the test case at the specified index using the specified TestResult
|
||||
void runTestAt( unsigned int index, TestResult &result ) const;
|
||||
private:
|
||||
void listTests() const;
|
||||
bool testIndex(const std::string &testName, unsigned int &index) const;
|
||||
static void preventDialogOnCrash();
|
||||
|
||||
static void printUsage( const char *appName );
|
||||
private:
|
||||
typedef std::deque<TestCaseFactory> Factories;
|
||||
Factories tests_;
|
||||
};
|
||||
|
||||
private: // prevents copy construction and assignment
|
||||
Runner( const Runner &other );
|
||||
Runner &operator =( const Runner &other );
|
||||
template <typename T, typename U>
|
||||
TestResult &checkEqual(TestResult &result,
|
||||
const T &expected,
|
||||
const U &actual,
|
||||
const char *file,
|
||||
unsigned int line,
|
||||
const char *expr) {
|
||||
if (static_cast<U>(expected) != actual) {
|
||||
result.addFailure(file, line, expr);
|
||||
result << "Expected: " << static_cast<U>(expected) << "\n";
|
||||
result << "Actual : " << actual;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
void listTests() const;
|
||||
bool testIndex( const std::string &testName, unsigned int &index ) const;
|
||||
static void preventDialogOnCrash();
|
||||
|
||||
private:
|
||||
typedef std::deque<TestCaseFactory> Factories;
|
||||
Factories tests_;
|
||||
};
|
||||
|
||||
template<typename T, typename U>
|
||||
TestResult &
|
||||
checkEqual( TestResult &result, const T &expected, const U &actual,
|
||||
const char *file, unsigned int line, const char *expr )
|
||||
{
|
||||
if ( static_cast< U >( expected ) != actual )
|
||||
{
|
||||
result.addFailure( file, line, expr );
|
||||
result << "Expected: " << static_cast< U >( expected ) << "\n";
|
||||
result << "Actual : " << actual;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
TestResult &
|
||||
checkStringEqual( TestResult &result,
|
||||
const std::string &expected, const std::string &actual,
|
||||
const char *file, unsigned int line, const char *expr );
|
||||
TestResult &checkStringEqual(TestResult &result,
|
||||
const std::string &expected,
|
||||
const std::string &actual,
|
||||
const char *file,
|
||||
unsigned int line,
|
||||
const char *expr);
|
||||
|
||||
} // namespace JsonTest
|
||||
|
||||
|
||||
/// \brief Asserts that the given expression is true.
|
||||
/// JSONTEST_ASSERT( x == y ) << "x=" << x << ", y=" << y;
|
||||
/// JSONTEST_ASSERT( x == y );
|
||||
#define JSONTEST_ASSERT( expr ) \
|
||||
if ( expr ) \
|
||||
{ \
|
||||
} \
|
||||
else \
|
||||
result_->addFailure( __FILE__, __LINE__, #expr )
|
||||
#define JSONTEST_ASSERT(expr) \
|
||||
if (expr) { \
|
||||
} else \
|
||||
result_->addFailure(__FILE__, __LINE__, #expr)
|
||||
|
||||
/// \brief Asserts that the given predicate is true.
|
||||
/// The predicate may do other assertions and be a member function of the fixture.
|
||||
#define JSONTEST_ASSERT_PRED( expr ) \
|
||||
{ \
|
||||
JsonTest::PredicateContext _minitest_Context = { \
|
||||
result_->predicateId_, __FILE__, __LINE__, #expr }; \
|
||||
result_->predicateStackTail_->next_ = &_minitest_Context; \
|
||||
result_->predicateId_ += 1; \
|
||||
result_->predicateStackTail_ = &_minitest_Context; \
|
||||
(expr); \
|
||||
result_->popPredicateContext(); \
|
||||
}
|
||||
/// The predicate may do other assertions and be a member function of the
|
||||
/// fixture.
|
||||
#define JSONTEST_ASSERT_PRED(expr) \
|
||||
{ \
|
||||
JsonTest::PredicateContext _minitest_Context = { \
|
||||
result_->predicateId_, __FILE__, __LINE__, #expr \
|
||||
}; \
|
||||
result_->predicateStackTail_->next_ = &_minitest_Context; \
|
||||
result_->predicateId_ += 1; \
|
||||
result_->predicateStackTail_ = &_minitest_Context; \
|
||||
(expr); \
|
||||
result_->popPredicateContext(); \
|
||||
}
|
||||
|
||||
/// \brief Asserts that two values are equals.
|
||||
#define JSONTEST_ASSERT_EQUAL( expected, actual ) \
|
||||
JsonTest::checkEqual( *result_, expected, actual, \
|
||||
__FILE__, __LINE__, \
|
||||
#expected " == " #actual )
|
||||
#define JSONTEST_ASSERT_EQUAL(expected, actual) \
|
||||
JsonTest::checkEqual(*result_, \
|
||||
expected, \
|
||||
actual, \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
#expected " == " #actual)
|
||||
|
||||
/// \brief Asserts that two values are equals.
|
||||
#define JSONTEST_ASSERT_STRING_EQUAL( expected, actual ) \
|
||||
JsonTest::checkStringEqual( *result_, \
|
||||
std::string(expected), std::string(actual), \
|
||||
__FILE__, __LINE__, \
|
||||
#expected " == " #actual )
|
||||
#define JSONTEST_ASSERT_STRING_EQUAL(expected, actual) \
|
||||
JsonTest::checkStringEqual(*result_, \
|
||||
std::string(expected), \
|
||||
std::string(actual), \
|
||||
__FILE__, \
|
||||
__LINE__, \
|
||||
#expected " == " #actual)
|
||||
|
||||
/// \brief Begin a fixture test case.
|
||||
#define JSONTEST_FIXTURE( FixtureType, name ) \
|
||||
class Test##FixtureType##name : public FixtureType \
|
||||
{ \
|
||||
public: \
|
||||
static JsonTest::TestCase *factory() \
|
||||
{ \
|
||||
return new Test##FixtureType##name(); \
|
||||
} \
|
||||
public: /* overidden from TestCase */ \
|
||||
virtual const char *testName() const \
|
||||
{ \
|
||||
return #FixtureType "/" #name; \
|
||||
} \
|
||||
virtual void runTestCase(); \
|
||||
}; \
|
||||
\
|
||||
void Test##FixtureType##name::runTestCase()
|
||||
#define JSONTEST_FIXTURE(FixtureType, name) \
|
||||
class Test##FixtureType##name : public FixtureType { \
|
||||
public: \
|
||||
static JsonTest::TestCase *factory() { \
|
||||
return new Test##FixtureType##name(); \
|
||||
} \
|
||||
\
|
||||
public: /* overidden from TestCase */ \
|
||||
virtual const char *testName() const { return #FixtureType "/" #name; } \
|
||||
virtual void runTestCase(); \
|
||||
}; \
|
||||
\
|
||||
void Test##FixtureType##name::runTestCase()
|
||||
|
||||
#define JSONTEST_FIXTURE_FACTORY( FixtureType, name ) \
|
||||
&Test##FixtureType##name::factory
|
||||
#define JSONTEST_FIXTURE_FACTORY(FixtureType, name) \
|
||||
&Test##FixtureType##name::factory
|
||||
|
||||
#define JSONTEST_REGISTER_FIXTURE( runner, FixtureType, name ) \
|
||||
(runner).add( JSONTEST_FIXTURE_FACTORY( FixtureType, name ) )
|
||||
#define JSONTEST_REGISTER_FIXTURE(runner, FixtureType, name) \
|
||||
(runner).add(JSONTEST_FIXTURE_FACTORY(FixtureType, name))
|
||||
|
||||
#endif // ifndef JSONTEST_H_INCLUDED
|
||||
// vim: et ts=4 sts=4 sw=4 tw=0
|
||||
|
Reference in New Issue
Block a user