2015-11-27 19:24:00 +01:00
|
|
|
|
/*
|
2015-11-28 19:20:10 +01:00
|
|
|
|
EVENT-LOG.hpp - test facility to verify the occurrence of expected events
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
2015-11-28 19:20:10 +01:00
|
|
|
|
2015, Hermann Vosseler <Ichthyostega@web.de>
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-28 19:20:10 +01:00
|
|
|
|
/** @file event-log.hpp
|
|
|
|
|
|
** Support for verifying the occurrence of events from unit tests.
|
|
|
|
|
|
** Typically used within special rigging and instrumentation for tests,
|
|
|
|
|
|
** the [EventLog] allows to record invocations and similar events. It is
|
|
|
|
|
|
** implemented as a "PImpl" to allow sharing of logs. The front-end used
|
|
|
|
|
|
** for access offers a query facility, so the test code may express some
|
|
|
|
|
|
** expected patterns of incidence and verify match or non-match.
|
|
|
|
|
|
**
|
|
|
|
|
|
** Failure of match deliberately throws an assertion failure, in order to
|
|
|
|
|
|
** deliver a precise indication what part of the condition failed.
|
|
|
|
|
|
**
|
|
|
|
|
|
** @todo as of 11/2015 this is complete WIP-WIP-WIP
|
|
|
|
|
|
**
|
|
|
|
|
|
** @see ////TODO_test usage example
|
|
|
|
|
|
**
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-28 18:36:35 +01:00
|
|
|
|
#ifndef LIB_TEST_EVENT_LOG_H
|
|
|
|
|
|
#define LIB_TEST_EVENT_LOG_H
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
2015-11-28 19:20:10 +01:00
|
|
|
|
#include "lib/error.hpp"
|
2015-11-28 23:50:56 +01:00
|
|
|
|
#include "lib/idi/entry-id.hpp"
|
2015-12-06 03:35:07 +01:00
|
|
|
|
#include "lib/iter-adapter.hpp"
|
|
|
|
|
|
#include "lib/iter-cursor.hpp"
|
2015-12-05 03:13:01 +01:00
|
|
|
|
#include "lib/format-string.hpp"
|
|
|
|
|
|
#include "lib/format-util.hpp"
|
|
|
|
|
|
#include "lib/diff/record.hpp"
|
2015-12-02 23:37:54 +01:00
|
|
|
|
#include "lib/util.hpp"
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
2015-11-28 19:20:10 +01:00
|
|
|
|
//#include <boost/lexical_cast.hpp>
|
2015-11-28 23:50:56 +01:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <string>
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace test{
|
2015-12-02 23:37:54 +01:00
|
|
|
|
namespace error = lumiera::error;
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
2015-11-28 19:20:10 +01:00
|
|
|
|
// using lib::Literal;
|
2015-11-28 23:50:56 +01:00
|
|
|
|
using std::string;
|
2015-12-05 02:40:03 +01:00
|
|
|
|
using util::contains;
|
2015-12-02 23:37:54 +01:00
|
|
|
|
using util::isnil;
|
2015-12-05 03:13:01 +01:00
|
|
|
|
using util::_Fmt;
|
2015-11-28 19:20:10 +01:00
|
|
|
|
// using std::rand;
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
2015-11-28 23:50:56 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @internal ongoing evaluation and match within an [EventLog].
|
|
|
|
|
|
* @throws error::Fatal when the expected match fails (error::LUMIERA_ERROR_ASSERTION)
|
|
|
|
|
|
*/
|
|
|
|
|
|
class EventMatch
|
|
|
|
|
|
{
|
2015-12-05 02:07:25 +01:00
|
|
|
|
using Entry = lib::diff::Record<string>;
|
|
|
|
|
|
using Log = std::vector<Entry>;
|
2015-12-06 03:35:07 +01:00
|
|
|
|
using Iter = lib::IterCursor<Log::const_iterator>;
|
2015-12-05 02:07:25 +01:00
|
|
|
|
using Filter = ExtensibleFilterIter<Iter>;
|
|
|
|
|
|
|
2015-12-05 03:13:01 +01:00
|
|
|
|
/** match predicate evaluator */
|
2015-12-05 02:07:25 +01:00
|
|
|
|
Filter solution_;
|
2015-12-02 23:37:54 +01:00
|
|
|
|
|
2015-12-05 03:13:01 +01:00
|
|
|
|
/** record last match for diagnostics */
|
|
|
|
|
|
string lastMatch_;
|
|
|
|
|
|
|
|
|
|
|
|
/** core of the evaluation machinery:
|
|
|
|
|
|
* apply a filter predicate and then pull
|
|
|
|
|
|
* through the log to find a acceptable entry
|
|
|
|
|
|
*/
|
2015-12-02 23:37:54 +01:00
|
|
|
|
bool
|
|
|
|
|
|
eval()
|
|
|
|
|
|
{
|
2015-12-05 02:07:25 +01:00
|
|
|
|
return !isnil (solution_);
|
2015-12-02 23:37:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-05 03:13:01 +01:00
|
|
|
|
/** this is actually called after each refinement
|
|
|
|
|
|
* of the filter and matching conditions. The effect is
|
|
|
|
|
|
* to search for an (intermediary) solution right away
|
|
|
|
|
|
* and to throw an assertion failure as soon as some
|
|
|
|
|
|
* condition can not be satisfied. Rationale is to
|
|
|
|
|
|
* indicate the point where a chained match fails
|
|
|
|
|
|
* @par matchSpec diagnostics description of the predicate just being added
|
|
|
|
|
|
* @par rel indication of the searching relation / direction
|
|
|
|
|
|
* @throws error::Fatal ([assertion failure][error::LUMIERA_ERROR_ASSERTION]
|
|
|
|
|
|
* when the filtering condition built up thus far can not be
|
|
|
|
|
|
* satisfied at all on the current event log contents
|
|
|
|
|
|
*/
|
2015-12-02 23:37:54 +01:00
|
|
|
|
void
|
2015-12-05 03:13:01 +01:00
|
|
|
|
enforce (string matchSpec, Literal rel = "after")
|
2015-12-02 23:37:54 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (!eval())
|
2015-12-05 03:13:01 +01:00
|
|
|
|
throw error::Fatal(_Fmt("Failed to %s %s %s")
|
|
|
|
|
|
% matchSpec % rel % lastMatch_
|
|
|
|
|
|
, error::LUMIERA_ERROR_ASSERTION);
|
|
|
|
|
|
|
|
|
|
|
|
lastMatch_ = matchSpec+" @ "+string(*solution_);
|
2015-12-02 23:37:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-05 02:40:03 +01:00
|
|
|
|
/** @internal for creating EventLog matchers */
|
2015-12-06 03:35:07 +01:00
|
|
|
|
EventMatch(Log const& srcSeq)
|
|
|
|
|
|
: solution_(Iter(srcSeq))
|
2015-12-05 03:13:01 +01:00
|
|
|
|
, lastMatch_("HEAD "+ solution_->get("ID"))
|
2015-12-05 02:40:03 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
friend class EventLog;
|
|
|
|
|
|
|
2015-11-28 23:50:56 +01:00
|
|
|
|
|
2015-12-05 02:40:03 +01:00
|
|
|
|
/* == elementary matchers == */
|
|
|
|
|
|
|
|
|
|
|
|
auto
|
|
|
|
|
|
find (string match)
|
|
|
|
|
|
{
|
2015-12-05 03:13:01 +01:00
|
|
|
|
return [=](Entry const& entry)
|
2015-12-05 02:40:03 +01:00
|
|
|
|
{
|
|
|
|
|
|
return contains (string(entry), match);
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2015-12-05 03:37:25 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* find a match (substring match) of the given text
|
|
|
|
|
|
* in an EventLog entry after the current position
|
|
|
|
|
|
* @remarks the name of this junctor might seem counter intuitive;
|
|
|
|
|
|
* it was chosen due to expected DSL usage: `log.verify("α").before("β")`.
|
|
|
|
|
|
* Operationally this means first to find a Record matching the substring "α"
|
|
|
|
|
|
* and then to forward from this point until hitting a record to match "β".
|
|
|
|
|
|
*/
|
2015-11-28 23:50:56 +01:00
|
|
|
|
EventMatch&
|
|
|
|
|
|
before (string match)
|
|
|
|
|
|
{
|
2015-12-06 03:35:07 +01:00
|
|
|
|
solution_.underlying().switchForwards();
|
2015-12-05 03:37:25 +01:00
|
|
|
|
solution_.setNewFilter(find(match));
|
|
|
|
|
|
enforce ("match(\""+match+"\")");
|
|
|
|
|
|
return *this;
|
2015-11-28 23:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
beforeMatch (string regExp)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process combined relational regular expression match");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
beforeEvent (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process combined relational match");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
beforeCall (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process combined relational match");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
after (string match)
|
|
|
|
|
|
{
|
2015-12-06 03:35:07 +01:00
|
|
|
|
solution_.underlying().switchBackwards();
|
|
|
|
|
|
solution_.setNewFilter(find(match));
|
|
|
|
|
|
enforce ("match(\""+match+"\")", "before");
|
|
|
|
|
|
return *this;
|
2015-11-28 23:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
afterMatch (string regExp)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process combined relational regular expression match backwards");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
afterEvent (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process combined relational match backwards");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
afterCall (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process combined relational match backwards");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
arg ()
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process match on no-arg call argument list");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename... MORE>
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
arg (string match, MORE...args)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process match on call argument list");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename... MORE>
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
argMatch (string regExp, MORE...args)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process regular expression match on call argument list");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch&
|
|
|
|
|
|
on (string sourceID)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("process additional filter on source of log entry");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
2015-11-28 19:20:10 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Helper to log and verify the occurrence of events.
|
2015-11-27 19:24:00 +01:00
|
|
|
|
*/
|
2015-11-28 19:20:10 +01:00
|
|
|
|
class EventLog
|
|
|
|
|
|
{
|
2015-11-28 23:50:56 +01:00
|
|
|
|
using Entry = lib::diff::Record<string>;
|
|
|
|
|
|
using Log = std::vector<Entry>;
|
2015-12-05 02:40:03 +01:00
|
|
|
|
using Iter = lib::RangeIter<Log::const_iterator>;
|
2015-11-28 23:50:56 +01:00
|
|
|
|
|
|
|
|
|
|
string logID_;
|
|
|
|
|
|
Log log_;
|
|
|
|
|
|
|
2015-12-02 01:31:37 +01:00
|
|
|
|
void
|
|
|
|
|
|
log (std::initializer_list<string> const&& ili)
|
|
|
|
|
|
{
|
|
|
|
|
|
log_.emplace_back(ili);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-28 23:50:56 +01:00
|
|
|
|
public:
|
|
|
|
|
|
explicit
|
|
|
|
|
|
EventLog (string logID)
|
|
|
|
|
|
: logID_(logID)
|
|
|
|
|
|
{
|
2015-12-02 01:31:37 +01:00
|
|
|
|
log({"type=EventLogHeader", "ID="+logID_});
|
2015-11-28 23:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-06 03:35:07 +01:00
|
|
|
|
explicit
|
|
|
|
|
|
EventLog (const char* logID)
|
|
|
|
|
|
: EventLog(string(logID))
|
|
|
|
|
|
{ }
|
|
|
|
|
|
|
2015-11-28 23:50:56 +01:00
|
|
|
|
template<class X>
|
|
|
|
|
|
explicit
|
2015-12-02 01:31:37 +01:00
|
|
|
|
EventLog (const X *const obj)
|
|
|
|
|
|
: EventLog(idi::instanceTypeID (obj))
|
2015-11-28 23:50:56 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// standard copy operations acceptable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EventLog&
|
|
|
|
|
|
join (EventLog& otherLog)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("log join");
|
|
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ==== Logging API ==== */
|
|
|
|
|
|
|
|
|
|
|
|
EventLog&
|
|
|
|
|
|
event (string text)
|
|
|
|
|
|
{
|
2015-12-02 01:31:37 +01:00
|
|
|
|
log({"type=event", text});
|
2015-11-28 23:50:56 +01:00
|
|
|
|
return *this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ==== Iteration ==== */
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
|
empty() const
|
|
|
|
|
|
{
|
2015-12-02 01:31:37 +01:00
|
|
|
|
return 1 >= log_.size();
|
2015-11-28 23:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef Iter const_iterator;
|
|
|
|
|
|
typedef const Entry value_type;
|
|
|
|
|
|
|
2015-12-05 02:40:03 +01:00
|
|
|
|
const_iterator begin() const { return Iter(log_.begin(), log_.end()); }
|
|
|
|
|
|
const_iterator end() const { return Iter(); }
|
2015-11-28 23:50:56 +01:00
|
|
|
|
|
|
|
|
|
|
friend const_iterator begin (EventLog const& log) { return log.begin(); }
|
|
|
|
|
|
friend const_iterator end (EventLog const& log) { return log.end(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ==== Query/Verification API ==== */
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch
|
|
|
|
|
|
verify (string match)
|
|
|
|
|
|
{
|
2015-12-06 03:35:07 +01:00
|
|
|
|
EventMatch matcher(log_);
|
2015-12-05 03:37:25 +01:00
|
|
|
|
matcher.before (match);
|
2015-12-05 02:40:03 +01:00
|
|
|
|
return matcher;
|
2015-11-28 23:50:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch
|
|
|
|
|
|
verifyMatch (string regExp)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("start matching sequence for regular expression match");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch
|
|
|
|
|
|
verifyEvent (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("start matching sequence");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EventMatch
|
|
|
|
|
|
verifyCall (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("start matching sequence");
|
|
|
|
|
|
}
|
2015-11-28 19:20:10 +01:00
|
|
|
|
|
2015-11-28 23:50:56 +01:00
|
|
|
|
EventMatch
|
|
|
|
|
|
ensureNot (string match)
|
|
|
|
|
|
{
|
|
|
|
|
|
UNIMPLEMENTED("start matching sequence");
|
|
|
|
|
|
}
|
2015-12-06 04:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** equality comparison is based on the actual log contents */
|
|
|
|
|
|
friend bool
|
|
|
|
|
|
operator== (EventLog const& l1, EventLog const& l2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return l1.log_ == l2.log_;
|
|
|
|
|
|
}
|
|
|
|
|
|
friend bool
|
|
|
|
|
|
operator!= (EventLog const& l1, EventLog const& l2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return not (l1 == l2);
|
|
|
|
|
|
}
|
2015-11-28 19:20:10 +01:00
|
|
|
|
};
|
2015-11-27 19:24:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|
2015-11-28 19:20:10 +01:00
|
|
|
|
#endif /*LIB_TEST_EVENT_LOG_H*/
|