/* * matchers.hpp * Copyright (c) 2014 Eran Pe'er. * * This program is made available under the terms of the MIT License. * * Created on Aug 12, 2014 */ #pragma once #include #include #include #include #include "mockutils/TupleDispatcher.hpp" #include "mockutils/TuplePrinter.hpp" #include "mockutils/DefaultValue.hpp" #include "mockutils/type_utils.hpp" #include "fakeit/ActualInvocation.hpp" #include "fakeit/argument_matchers.hpp" namespace fakeit { template struct ArgumentsMatcherInvocationMatcher : public ActualInvocation::Matcher { virtual ~ArgumentsMatcherInvocationMatcher() { for (unsigned int i = 0; i < _matchers.size(); i++) delete _matchers[i]; } ArgumentsMatcherInvocationMatcher(const std::vector &args) : _matchers(args) { } virtual bool matches(ActualInvocation &invocation) override { if (invocation.getActualMatcher() == this) return true; return matches(invocation.getActualArguments()); } virtual std::string format() const override { std::ostringstream out; out << "("; for (unsigned int i = 0; i < _matchers.size(); i++) { if (i > 0) out << ", "; IMatcher *m = dynamic_cast(_matchers[i]); out << m->format(); } out << ")"; return out.str(); } private: struct MatchingLambda { MatchingLambda(const std::vector &matchers) : _matchers(matchers) { } template void operator()(int index, A &actualArg) { TypedMatcher::type> *matcher = dynamic_cast::type> *>(_matchers[index]); if (_matching) _matching = matcher->matches(actualArg); } bool isMatching() { return _matching; } private: bool _matching = true; const std::vector &_matchers; }; virtual bool matches(const std::tuple &actualArgs) { MatchingLambda l(_matchers); fakeit::for_each(actualArgs, l); return l.isMatching(); } const std::vector _matchers; }; //template //struct ExpectedArgumentsInvocationMatcher: public ActualInvocation::Matcher { // // virtual ~ExpectedArgumentsInvocationMatcher() = default; // // ExpectedArgumentsInvocationMatcher(const arglist&... args) // : expectedArguments(args...) { // } // // ExpectedArgumentsInvocationMatcher(const std::tuple& expectedArguments) // : expectedArguments(expectedArguments) { // } // // virtual bool matches(ActualInvocation& invocation) override { // if (invocation.getActualMatcher() == this) // return true; // return matches(invocation.getActualArguments()); // } // // virtual std::string format() const override { // std::ostringstream out; // print(out, expectedArguments); // return out.str(); // } //private: // virtual bool matches(const std::tuple& actualArgs) { // return expectedArguments == actualArgs; // } // const std::tuple expectedArguments; //}; template struct UserDefinedInvocationMatcher : public ActualInvocation::Matcher { virtual ~UserDefinedInvocationMatcher() = default; UserDefinedInvocationMatcher(std::function matcher) : matcher{matcher} { } virtual bool matches(ActualInvocation &invocation) override { if (invocation.getActualMatcher() == this) return true; return matches(invocation.getActualArguments()); } virtual std::string format() const { return {"( user defined matcher )"}; } private: virtual bool matches(const std::tuple &actualArgs) { return invoke(matcher, actualArgs); } const std::function matcher; }; template struct DefaultInvocationMatcher : public ActualInvocation::Matcher { virtual ~DefaultInvocationMatcher() = default; DefaultInvocationMatcher() { } virtual bool matches(ActualInvocation &invocation) override { return matches(invocation.getActualArguments()); } virtual std::string format() const { return {"( Any arguments )"}; } private: virtual bool matches(const std::tuple &) { return true; } }; }