aboutsummaryrefslogtreecommitdiff
path: root/src/test_lib_json/jsontest.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/test_lib_json/jsontest.h')
-rw-r--r--src/test_lib_json/jsontest.h150
1 files changed, 79 insertions, 71 deletions
diff --git a/src/test_lib_json/jsontest.h b/src/test_lib_json/jsontest.h
index 5c56a40..4e8af0f 100644
--- a/src/test_lib_json/jsontest.h
+++ b/src/test_lib_json/jsontest.h
@@ -1,4 +1,4 @@
-// Copyright 2007-2010 Baptiste Lepilleur
+// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
// Distributed under MIT license, or public domain if desired and
// recognized in your jurisdiction.
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
@@ -6,11 +6,12 @@
#ifndef JSONTEST_H_INCLUDED
#define JSONTEST_H_INCLUDED
+#include <cstdio>
+#include <deque>
+#include <iomanip>
#include <json/config.h>
#include <json/value.h>
#include <json/writer.h>
-#include <stdio.h>
-#include <deque>
#include <sstream>
#include <string>
@@ -32,8 +33,8 @@ class Failure {
public:
const char* file_;
unsigned int line_;
- std::string expr_;
- std::string message_;
+ Json::String expr_;
+ Json::String message_;
unsigned int nestingLevel_;
};
@@ -41,7 +42,7 @@ public:
/// Must be a POD to allow inline initialisation without stepping
/// into the debugger.
struct PredicateContext {
- typedef unsigned int Id;
+ using Id = unsigned int;
Id id_;
const char* file_;
unsigned int line_;
@@ -60,16 +61,16 @@ public:
/// 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_;
+ PredicateContext::Id predicateId_{1};
/// \internal Implementation detail for predicate macros
PredicateContext* predicateStackTail_;
- void setTestName(const std::string& name);
+ void setTestName(const Json::String& name);
/// Adds an assertion failure.
- TestResult&
- addFailure(const char* file, unsigned int line, const char* expr = 0);
+ TestResult& addFailure(const char* file, unsigned int line,
+ const char* expr = nullptr);
/// Removes the last PredicateContext added to the predicate stack
/// chained list.
@@ -82,10 +83,8 @@ public:
// 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;
+ Json::OStringStream oss;
+ oss << std::setprecision(16) << std::hexfloat << value;
return addToLastFailure(oss.str());
}
@@ -96,23 +95,20 @@ public:
TestResult& operator<<(Json::UInt64 value);
private:
- TestResult& addToLastFailure(const std::string& message);
- unsigned int getAssertionNestingLevel() const;
+ TestResult& addToLastFailure(const Json::String& message);
/// Adds a failure or a predicate context
- void addFailureInfo(const char* file,
- unsigned int line,
- const char* expr,
+ 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);
+ static Json::String indentText(const Json::String& text,
+ const Json::String& indent);
- typedef std::deque<Failure> Failures;
+ using Failures = std::deque<Failure>;
Failures failures_;
- std::string name_;
+ Json::String name_;
PredicateContext rootPredicateNode_;
- PredicateContext::Id lastUsedPredicateId_;
+ PredicateContext::Id lastUsedPredicateId_{0};
/// Failure which is the target of the messages added using operator <<
- Failure* messageTarget_;
+ Failure* messageTarget_{nullptr};
};
class TestCase {
@@ -126,14 +122,14 @@ public:
virtual const char* testName() const = 0;
protected:
- TestResult* result_;
+ TestResult* result_{nullptr};
private:
virtual void runTestCase() = 0;
};
/// Function pointer type for TestCase factory
-typedef TestCase* (*TestCaseFactory)();
+using TestCaseFactory = TestCase* (*)();
class Runner {
public:
@@ -152,37 +148,33 @@ public:
bool runAllTest(bool printSummary) const;
/// Returns the number of test case in the suite
- unsigned int testCount() const;
+ size_t testCount() const;
/// Returns the name of the test case at the specified index
- std::string testNameAt(unsigned int index) const;
+ Json::String testNameAt(size_t index) const;
/// Runs the test case at the specified index using the specified TestResult
- void runTestAt(unsigned int index, TestResult& result) const;
+ void runTestAt(size_t index, TestResult& result) const;
static void printUsage(const char* appName);
private: // prevents copy construction and assignment
- Runner(const Runner& other);
- Runner& operator=(const Runner& other);
+ Runner(const Runner& other) = delete;
+ Runner& operator=(const Runner& other) = delete;
private:
void listTests() const;
- bool testIndex(const std::string& testName, unsigned int& index) const;
+ bool testIndex(const Json::String& testName, size_t& indexOut) const;
static void preventDialogOnCrash();
private:
- typedef std::deque<TestCaseFactory> Factories;
+ using Factories = std::deque<TestCaseFactory>;
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) {
+TestResult& checkEqual(TestResult& result, T expected, 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";
@@ -191,12 +183,15 @@ TestResult& checkEqual(TestResult& result,
return result;
}
-TestResult& checkStringEqual(TestResult& result,
- const std::string& expected,
- const std::string& actual,
- const char* file,
- unsigned int line,
- const char* expr);
+Json::String ToJsonString(const char* toConvert);
+Json::String ToJsonString(Json::String in);
+#if JSONCPP_USING_SECURE_MEMORY
+Json::String ToJsonString(std::string in);
+#endif
+
+TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
+ const Json::String& actual, const char* file,
+ unsigned int line, const char* expr);
} // namespace JsonTest
@@ -206,55 +201,46 @@ TestResult& checkStringEqual(TestResult& result,
#define JSONTEST_ASSERT(expr) \
if (expr) { \
} else \
- result_->addFailure(__FILE__, __LINE__, #expr)
+ 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) \
- { \
+ do { \
JsonTest::PredicateContext _minitest_Context = { \
- result_->predicateId_, __FILE__, __LINE__, #expr \
- }; \
+ result_->predicateId_, __FILE__, __LINE__, #expr, NULL, NULL}; \
result_->predicateStackTail_->next_ = &_minitest_Context; \
result_->predicateId_ += 1; \
result_->predicateStackTail_ = &_minitest_Context; \
(expr); \
result_->popPredicateContext(); \
- }
+ } while (0)
/// \brief Asserts that two values are equals.
#define JSONTEST_ASSERT_EQUAL(expected, actual) \
- JsonTest::checkEqual(*result_, \
- expected, \
- actual, \
- __FILE__, \
- __LINE__, \
+ 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)
+ JsonTest::checkStringEqual(*result_, JsonTest::ToJsonString(expected), \
+ JsonTest::ToJsonString(actual), __FILE__, \
+ __LINE__, #expected " == " #actual)
/// \brief Asserts that a given expression throws an exception
#define JSONTEST_ASSERT_THROWS(expr) \
- { \
+ do { \
bool _threw = false; \
try { \
expr; \
- } \
- catch (...) { \
+ } catch (...) { \
_threw = true; \
} \
if (!_threw) \
- result_->addFailure( \
- __FILE__, __LINE__, "expected exception thrown: " #expr); \
- }
+ result_->addFailure(__FILE__, __LINE__, \
+ "expected exception thrown: " #expr); \
+ } while (0)
/// \brief Begin a fixture test case.
#define JSONTEST_FIXTURE(FixtureType, name) \
@@ -264,9 +250,9 @@ TestResult& checkStringEqual(TestResult& result,
return new Test##FixtureType##name(); \
} \
\
- public: /* overidden from TestCase */ \
- virtual const char* testName() const { return #FixtureType "/" #name; } \
- virtual void runTestCase(); \
+ public: /* overridden from TestCase */ \
+ const char* testName() const override { return #FixtureType "/" #name; } \
+ void runTestCase() override; \
}; \
\
void Test##FixtureType##name::runTestCase()
@@ -277,4 +263,26 @@ TestResult& checkStringEqual(TestResult& result,
#define JSONTEST_REGISTER_FIXTURE(runner, FixtureType, name) \
(runner).add(JSONTEST_FIXTURE_FACTORY(FixtureType, name))
+/// \brief Begin a fixture test case.
+#define JSONTEST_FIXTURE_V2(FixtureType, name, collections) \
+ class Test##FixtureType##name : public FixtureType { \
+ public: \
+ static JsonTest::TestCase* factory() { \
+ return new Test##FixtureType##name(); \
+ } \
+ static bool collect() { \
+ (collections).push_back(JSONTEST_FIXTURE_FACTORY(FixtureType, name)); \
+ return true; \
+ } \
+ \
+ public: /* overridden from TestCase */ \
+ const char* testName() const override { return #FixtureType "/" #name; } \
+ void runTestCase() override; \
+ }; \
+ \
+ static bool test##FixtureType##name##collect = \
+ Test##FixtureType##name::collect(); \
+ \
+ void Test##FixtureType##name::runTestCase()
+
#endif // ifndef JSONTEST_H_INCLUDED