Skip to content
Snippets Groups Projects
Commit f9048677 authored by Nat Goodspeed's avatar Nat Goodspeed
Browse files

Make CaptureLog::withMessage() raise tut::failure if not found.

All known callers were using ensure(! withMessage(...).empty()). Centralize
that logic. Make failure message report the string being sought and the log
messages in which it wasn't found.
In case someone does want to permit the search to fail, add an optional
'required' parameter, default true.
Leverage new functionality in llprocess_test.cpp.
parent 22fcb563
No related branches found
No related tags found
No related merge requests found
...@@ -788,7 +788,6 @@ namespace tut ...@@ -788,7 +788,6 @@ namespace tut
py.mPy = LLProcess::create(py.mParams); py.mPy = LLProcess::create(py.mParams);
ensure("should have rejected 'bogus'", ! py.mPy); ensure("should have rejected 'bogus'", ! py.mPy);
std::string message(recorder.messageWith("bogus")); std::string message(recorder.messageWith("bogus"));
ensure("did not log 'bogus' type", ! message.empty());
ensure_contains("did not name 'stdin'", message, "stdin"); ensure_contains("did not name 'stdin'", message, "stdin");
} }
...@@ -820,7 +819,6 @@ namespace tut ...@@ -820,7 +819,6 @@ namespace tut
py.mPy = LLProcess::create(py.mParams); py.mPy = LLProcess::create(py.mParams);
ensure("should have rejected 'tpipe'", ! py.mPy); ensure("should have rejected 'tpipe'", ! py.mPy);
std::string message(recorder.messageWith("tpipe")); std::string message(recorder.messageWith("tpipe"));
ensure("did not log 'tpipe' type", ! message.empty());
ensure_contains("did not name 'stdout'", message, "stdout"); ensure_contains("did not name 'stdout'", message, "stdout");
} }
...@@ -839,7 +837,6 @@ namespace tut ...@@ -839,7 +837,6 @@ namespace tut
py.mPy = LLProcess::create(py.mParams); py.mPy = LLProcess::create(py.mParams);
ensure("should have rejected 'npipe'", ! py.mPy); ensure("should have rejected 'npipe'", ! py.mPy);
std::string message(recorder.messageWith("npipe")); std::string message(recorder.messageWith("npipe"));
ensure("did not log 'npipe' type", ! message.empty());
ensure_contains("did not name 'stderr'", message, "stderr"); ensure_contains("did not name 'stderr'", message, "stderr");
} }
...@@ -856,7 +853,6 @@ namespace tut ...@@ -856,7 +853,6 @@ namespace tut
ensure_equals("Status.mState", py.mPy->getStatus().mState, LLProcess::EXITED); ensure_equals("Status.mState", py.mPy->getStatus().mState, LLProcess::EXITED);
ensure_equals("Status.mData", py.mPy->getStatus().mData, 7); ensure_equals("Status.mData", py.mPy->getStatus().mData, 7);
std::string message(recorder.messageWith("not yet supported")); std::string message(recorder.messageWith("not yet supported"));
ensure("did not log pipe name warning", ! message.empty());
ensure_contains("log message did not mention internal pipe name", ensure_contains("log message did not mention internal pipe name",
message, "somename"); message, "somename");
} }
...@@ -905,7 +901,7 @@ namespace tut ...@@ -905,7 +901,7 @@ namespace tut
{ \ { \
CaptureLog recorder; \ CaptureLog recorder; \
ensure(#CODE " succeeded", ! (CODE)); \ ensure(#CODE " succeeded", ! (CODE)); \
ensure("wrong log message", ! recorder.messageWith(EXPECT).empty()); \ recorder.messageWith(EXPECT); \
} while (0) } while (0)
template<> template<> template<> template<>
......
...@@ -29,11 +29,13 @@ ...@@ -29,11 +29,13 @@
#if ! defined(LL_WRAPLLERRS_H) #if ! defined(LL_WRAPLLERRS_H)
#define LL_WRAPLLERRS_H #define LL_WRAPLLERRS_H
#include <tut/tut.hpp>
#include "llerrorcontrol.h" #include "llerrorcontrol.h"
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <list> #include <list>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <sstream>
// statically reference the function in test.cpp... it's short, we could // statically reference the function in test.cpp... it's short, we could
// replicate, but better to reuse // replicate, but better to reuse
...@@ -117,17 +119,26 @@ class CaptureLog : public LLError::Recorder ...@@ -117,17 +119,26 @@ class CaptureLog : public LLError::Recorder
/// Don't assume the message we want is necessarily the LAST log message /// Don't assume the message we want is necessarily the LAST log message
/// emitted by the underlying code; search backwards through all messages /// emitted by the underlying code; search backwards through all messages
/// for the sought string. /// for the sought string.
std::string messageWith(const std::string& search) std::string messageWith(const std::string& search, bool required=true)
{ {
for (std::list<std::string>::const_reverse_iterator rmi(mMessages.rbegin()), for (MessageList::const_reverse_iterator rmi(mMessages.rbegin()), rmend(mMessages.rend());
rmend(mMessages.rend());
rmi != rmend; ++rmi) rmi != rmend; ++rmi)
{ {
if (rmi->find(search) != std::string::npos) if (rmi->find(search) != std::string::npos)
return *rmi; return *rmi;
} }
// failed to find any such message // failed to find any such message
return std::string(); if (! required)
return std::string();
std::ostringstream out;
out << "failed to find '" << search << "' in captured log messages:";
for (MessageList::const_iterator mi(mMessages.begin()), mend(mMessages.end());
mi != mend; ++mi)
{
out << '\n' << *mi;
}
throw tut::failure(out.str());
} }
typedef std::list<std::string> MessageList; typedef std::list<std::string> MessageList;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment