aboutsummaryrefslogtreecommitdiff
path: root/src/test_lib_json/jsontest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test_lib_json/jsontest.cpp')
-rw-r--r--src/test_lib_json/jsontest.cpp167
1 files changed, 77 insertions, 90 deletions
diff --git a/src/test_lib_json/jsontest.cpp b/src/test_lib_json/jsontest.cpp
index ef9c543..0b7d12b 100644
--- a/src/test_lib_json/jsontest.cpp
+++ b/src/test_lib_json/jsontest.cpp
@@ -1,11 +1,11 @@
-// 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
#define _CRT_SECURE_NO_WARNINGS 1 // Prevents deprecation warning with MSVC
#include "jsontest.h"
-#include <stdio.h>
+#include <cstdio>
#include <string>
#if defined(_MSC_VER)
@@ -73,28 +73,27 @@ namespace JsonTest {
// class TestResult
// //////////////////////////////////////////////////////////////////
-TestResult::TestResult()
- : predicateId_(1), lastUsedPredicateId_(0), messageTarget_(0) {
+TestResult::TestResult() {
// The root predicate has id 0
rootPredicateNode_.id_ = 0;
- rootPredicateNode_.next_ = 0;
+ rootPredicateNode_.next_ = nullptr;
predicateStackTail_ = &rootPredicateNode_;
}
-void TestResult::setTestName(const std::string& name) { name_ = name; }
+void TestResult::setTestName(const Json::String& name) { name_ = name; }
-TestResult&
-TestResult::addFailure(const char* file, unsigned int line, const char* expr) {
+TestResult& TestResult::addFailure(const char* file, unsigned int line,
+ const char* expr) {
/// Walks the PredicateContext stack adding them to failures_ if not already
/// added.
unsigned int nestingLevel = 0;
PredicateContext* lastNode = rootPredicateNode_.next_;
- for (; lastNode != 0; lastNode = lastNode->next_) {
+ for (; lastNode != nullptr; lastNode = lastNode->next_) {
if (lastNode->id_ > lastUsedPredicateId_) // new PredicateContext
{
lastUsedPredicateId_ = lastNode->id_;
- addFailureInfo(
- lastNode->file_, lastNode->line_, lastNode->expr_, nestingLevel);
+ addFailureInfo(lastNode->file_, lastNode->line_, lastNode->expr_,
+ nestingLevel);
// Link the PredicateContext to the failure for message target when
// popping the PredicateContext.
lastNode->failure_ = &(failures_.back());
@@ -108,10 +107,8 @@ TestResult::addFailure(const char* file, unsigned int line, const char* expr) {
return *this;
}
-void TestResult::addFailureInfo(const char* file,
- unsigned int line,
- const char* expr,
- unsigned int nestingLevel) {
+void TestResult::addFailureInfo(const char* file, unsigned int line,
+ const char* expr, unsigned int nestingLevel) {
Failure failure;
failure.file_ = file;
failure.line_ = line;
@@ -124,32 +121,22 @@ void TestResult::addFailureInfo(const char* file,
TestResult& TestResult::popPredicateContext() {
PredicateContext* lastNode = &rootPredicateNode_;
- while (lastNode->next_ != 0 && lastNode->next_->next_ != 0) {
+ while (lastNode->next_ != nullptr && lastNode->next_->next_ != nullptr) {
lastNode = lastNode->next_;
}
// Set message target to popped failure
PredicateContext* tail = lastNode->next_;
- if (tail != 0 && tail->failure_ != 0) {
+ if (tail != nullptr && tail->failure_ != nullptr) {
messageTarget_ = tail->failure_;
}
// Remove tail from list
predicateStackTail_ = lastNode;
- lastNode->next_ = 0;
+ lastNode->next_ = nullptr;
return *this;
}
bool TestResult::failed() const { return !failures_.empty(); }
-unsigned int TestResult::getAssertionNestingLevel() const {
- unsigned int level = 0;
- const PredicateContext* lastNode = &rootPredicateNode_;
- while (lastNode->next_ != 0) {
- lastNode = lastNode->next_;
- ++level;
- }
- return level;
-}
-
void TestResult::printFailure(bool printTestName) const {
if (failures_.empty()) {
return;
@@ -160,12 +147,10 @@ void TestResult::printFailure(bool printTestName) const {
}
// Print in reverse to display the callstack in the right order
- Failures::const_iterator itEnd = failures_.end();
- for (Failures::const_iterator it = failures_.begin(); it != itEnd; ++it) {
- const Failure& failure = *it;
- std::string indent(failure.nestingLevel_ * 2, ' ');
+ for (const auto& failure : failures_) {
+ Json::String indent(failure.nestingLevel_ * 2, ' ');
if (failure.file_) {
- printf("%s%s(%d): ", indent.c_str(), failure.file_, failure.line_);
+ printf("%s%s(%u): ", indent.c_str(), failure.file_, failure.line_);
}
if (!failure.expr_.empty()) {
printf("%s\n", failure.expr_.c_str());
@@ -173,19 +158,19 @@ void TestResult::printFailure(bool printTestName) const {
printf("\n");
}
if (!failure.message_.empty()) {
- std::string reindented = indentText(failure.message_, indent + " ");
+ Json::String reindented = indentText(failure.message_, indent + " ");
printf("%s\n", reindented.c_str());
}
}
}
-std::string TestResult::indentText(const std::string& text,
- const std::string& indent) {
- std::string reindented;
- std::string::size_type lastIndex = 0;
+Json::String TestResult::indentText(const Json::String& text,
+ const Json::String& indent) {
+ Json::String reindented;
+ Json::String::size_type lastIndex = 0;
while (lastIndex < text.size()) {
- std::string::size_type nextIndex = text.find('\n', lastIndex);
- if (nextIndex == std::string::npos) {
+ Json::String::size_type nextIndex = text.find('\n', lastIndex);
+ if (nextIndex == Json::String::npos) {
nextIndex = text.size() - 1;
}
reindented += indent;
@@ -195,8 +180,8 @@ std::string TestResult::indentText(const std::string& text,
return reindented;
}
-TestResult& TestResult::addToLastFailure(const std::string& message) {
- if (messageTarget_ != 0) {
+TestResult& TestResult::addToLastFailure(const Json::String& message) {
+ if (messageTarget_ != nullptr) {
messageTarget_->message_ += message;
}
return *this;
@@ -217,9 +202,9 @@ TestResult& TestResult::operator<<(bool value) {
// class TestCase
// //////////////////////////////////////////////////////////////////
-TestCase::TestCase() : result_(0) {}
+TestCase::TestCase() = default;
-TestCase::~TestCase() {}
+TestCase::~TestCase() = default;
void TestCase::run(TestResult& result) {
result_ = &result;
@@ -229,25 +214,23 @@ void TestCase::run(TestResult& result) {
// class Runner
// //////////////////////////////////////////////////////////////////
-Runner::Runner() {}
+Runner::Runner() = default;
Runner& Runner::add(TestCaseFactory factory) {
tests_.push_back(factory);
return *this;
}
-unsigned int Runner::testCount() const {
- return static_cast<unsigned int>(tests_.size());
-}
+size_t Runner::testCount() const { return tests_.size(); }
-std::string Runner::testNameAt(unsigned int index) const {
+Json::String Runner::testNameAt(size_t index) const {
TestCase* test = tests_[index]();
- std::string name = test->testName();
+ Json::String name = test->testName();
delete test;
return name;
}
-void Runner::runTestAt(unsigned int index, TestResult& result) const {
+void Runner::runTestAt(size_t index, TestResult& result) const {
TestCase* test = tests_[index]();
result.setTestName(test->testName());
printf("Testing %s: ", test->testName());
@@ -257,8 +240,7 @@ void Runner::runTestAt(unsigned int index, TestResult& result) const {
#endif // if JSON_USE_EXCEPTION
test->run(result);
#if JSON_USE_EXCEPTION
- }
- catch (const std::exception& e) {
+ } catch (const std::exception& e) {
result.addFailure(__FILE__, __LINE__, "Unexpected exception caught:")
<< e.what();
}
@@ -270,9 +252,9 @@ void Runner::runTestAt(unsigned int index, TestResult& result) const {
}
bool Runner::runAllTest(bool printSummary) const {
- unsigned int count = testCount();
+ size_t const count = testCount();
std::deque<TestResult> failures;
- for (unsigned int index = 0; index < count; ++index) {
+ for (size_t index = 0; index < count; ++index) {
TestResult result;
runTestAt(index, result);
if (result.failed()) {
@@ -282,31 +264,26 @@ bool Runner::runAllTest(bool printSummary) const {
if (failures.empty()) {
if (printSummary) {
- printf("All %d tests passed\n", count);
+ printf("All %zu tests passed\n", count);
}
return true;
- } else {
- for (unsigned int index = 0; index < failures.size(); ++index) {
- TestResult& result = failures[index];
- result.printFailure(count > 1);
- }
+ }
+ for (auto& result : failures) {
+ result.printFailure(count > 1);
+ }
- if (printSummary) {
- unsigned int failedCount = static_cast<unsigned int>(failures.size());
- unsigned int passedCount = count - failedCount;
- printf("%d/%d tests passed (%d failure(s))\n",
- passedCount,
- count,
- failedCount);
- }
- return false;
+ if (printSummary) {
+ size_t const failedCount = failures.size();
+ size_t const passedCount = count - failedCount;
+ printf("%zu/%zu tests passed (%zu failure(s))\n", passedCount, count,
+ failedCount);
}
+ return false;
}
-bool Runner::testIndex(const std::string& testName,
- unsigned int& indexOut) const {
- unsigned int count = testCount();
- for (unsigned int index = 0; index < count; ++index) {
+bool Runner::testIndex(const Json::String& testName, size_t& indexOut) const {
+ const size_t count = testCount();
+ for (size_t index = 0; index < count; ++index) {
if (testNameAt(index) == testName) {
indexOut = index;
return true;
@@ -316,26 +293,27 @@ bool Runner::testIndex(const std::string& testName,
}
void Runner::listTests() const {
- unsigned int count = testCount();
- for (unsigned int index = 0; index < count; ++index) {
+ const size_t count = testCount();
+ for (size_t index = 0; index < count; ++index) {
printf("%s\n", testNameAt(index).c_str());
}
}
int Runner::runCommandLine(int argc, const char* argv[]) const {
- typedef std::deque<std::string> TestNames;
+ // typedef std::deque<String> TestNames;
Runner subrunner;
for (int index = 1; index < argc; ++index) {
- std::string opt = argv[index];
+ Json::String opt = argv[index];
if (opt == "--list-tests") {
listTests();
return 0;
- } else if (opt == "--test-auto") {
+ }
+ if (opt == "--test-auto") {
preventDialogOnCrash();
} else if (opt == "--test") {
++index;
if (index < argc) {
- unsigned int testNameIndex;
+ size_t testNameIndex;
if (testIndex(argv[index], testNameIndex)) {
subrunner.add(tests_[testNameIndex]);
} else {
@@ -362,8 +340,8 @@ int Runner::runCommandLine(int argc, const char* argv[]) const {
#if defined(_MSC_VER) && defined(_DEBUG)
// Hook MSVCRT assertions to prevent dialog from appearing
-static int
-msvcrtSilentReportHook(int reportType, char* message, int* /*returnValue*/) {
+static int msvcrtSilentReportHook(int reportType, char* message,
+ int* /*returnValue*/) {
// The default CRT handling of error and assertion is to display
// an error dialog to the user.
// Instead, when an error or an assertion occurs, we force the
@@ -398,8 +376,8 @@ void Runner::preventDialogOnCrash() {
_CrtSetReportHook(&msvcrtSilentReportHook);
#endif // if defined(_MSC_VER)
-// @todo investiguate this handler (for buffer overflow)
-// _set_security_error_handler
+ // @todo investigate this handler (for buffer overflow)
+ // _set_security_error_handler
#if defined(_WIN32)
// Prevents the system from popping a dialog for debugging if the
@@ -426,12 +404,21 @@ void Runner::printUsage(const char* appName) {
// Assertion functions
// //////////////////////////////////////////////////////////////////
-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) {
+ return Json::String(toConvert);
+}
+
+Json::String ToJsonString(Json::String in) { return in; }
+
+#if JSONCPP_USING_SECURE_MEMORY
+Json::String ToJsonString(std::string in) {
+ return Json::String(in.data(), in.data() + in.length());
+}
+#endif
+
+TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
+ const Json::String& actual, const char* file,
+ unsigned int line, const char* expr) {
if (expected != actual) {
result.addFailure(file, line, expr);
result << "Expected: '" << expected << "'\n";