/* * argument_matchers.hpp * Copyright (c) 2014 Eran Pe'er. * * This program is made available under the terms of the MIT License. * * Created on Jan 12, 2015 */ #pragma once namespace fakeit { struct IMatcher : public Destructible { virtual std::string format() const = 0; }; template struct TypedMatcher : public IMatcher { virtual bool matches(const T &actual) const = 0; }; template struct TypedMatcherCreator { virtual ~TypedMatcherCreator() = default; virtual TypedMatcher *createMatcher() const = 0; }; template struct ComparisonMatcherCreator : public TypedMatcherCreator { virtual ~ComparisonMatcherCreator() = default; ComparisonMatcherCreator(const T &arg) : _expected(arg) { } struct Matcher : public TypedMatcher { Matcher(const T &expected) : _expected(expected) { } const T _expected; }; const T &_expected; }; namespace internal { template struct TypedAnyMatcher : public TypedMatcherCreator { virtual ~TypedAnyMatcher() = default; TypedAnyMatcher() { } struct Matcher : public TypedMatcher { virtual bool matches(const T &) const { return true; } virtual std::string format() const override { return "Any"; } }; virtual TypedMatcher *createMatcher() const override { return new Matcher(); } }; template struct EqMatcherCreator : public ComparisonMatcherCreator { virtual ~EqMatcherCreator() = default; EqMatcherCreator(const T &expected) : ComparisonMatcherCreator(expected) { } struct Matcher : public ComparisonMatcherCreator::Matcher { Matcher(const T &expected) : ComparisonMatcherCreator::Matcher(expected) { } virtual std::string format() const override { return Formatter::format(this->_expected); } virtual bool matches(const T &actual) const override { return actual == this->_expected; } }; virtual TypedMatcher *createMatcher() const { return new Matcher(this->_expected); } }; template struct GtMatcherCreator : public ComparisonMatcherCreator { virtual ~GtMatcherCreator() = default; GtMatcherCreator(const T &expected) : ComparisonMatcherCreator(expected) { } struct Matcher : public ComparisonMatcherCreator::Matcher { Matcher(const T &expected) : ComparisonMatcherCreator::Matcher(expected) { } virtual bool matches(const T &actual) const override { return actual > this->_expected; } virtual std::string format() const override { return std::string(">") + Formatter::format(this->_expected); } }; virtual TypedMatcher *createMatcher() const override { return new Matcher(this->_expected); } }; template struct GeMatcherCreator : public ComparisonMatcherCreator { virtual ~GeMatcherCreator() = default; GeMatcherCreator(const T &expected) : ComparisonMatcherCreator(expected) { } struct Matcher : public ComparisonMatcherCreator::Matcher { Matcher(const T &expected) : ComparisonMatcherCreator::Matcher(expected) { } virtual bool matches(const T &actual) const override { return actual >= this->_expected; } virtual std::string format() const override { return std::string(">=") + Formatter::format(this->_expected); } }; virtual TypedMatcher *createMatcher() const override { return new Matcher(this->_expected); } }; template struct LtMatcherCreator : public ComparisonMatcherCreator { virtual ~LtMatcherCreator() = default; LtMatcherCreator(const T &expected) : ComparisonMatcherCreator(expected) { } struct Matcher : public ComparisonMatcherCreator::Matcher { Matcher(const T &expected) : ComparisonMatcherCreator::Matcher(expected) { } virtual bool matches(const T &actual) const override { return actual < this->_expected; } virtual std::string format() const override { return std::string("<") + Formatter::format(this->_expected); } }; virtual TypedMatcher *createMatcher() const override { return new Matcher(this->_expected); } }; template struct LeMatcherCreator : public ComparisonMatcherCreator { virtual ~LeMatcherCreator() = default; LeMatcherCreator(const T &expected) : ComparisonMatcherCreator(expected) { } struct Matcher : public ComparisonMatcherCreator::Matcher { Matcher(const T &expected) : ComparisonMatcherCreator::Matcher(expected) { } virtual bool matches(const T &actual) const override { return actual <= this->_expected; } virtual std::string format() const override { return std::string("<=") + Formatter::format(this->_expected); } }; virtual TypedMatcher *createMatcher() const override { return new Matcher(this->_expected); } }; template struct NeMatcherCreator : public ComparisonMatcherCreator { virtual ~NeMatcherCreator() = default; NeMatcherCreator(const T &expected) : ComparisonMatcherCreator(expected) { } struct Matcher : public ComparisonMatcherCreator::Matcher { Matcher(const T &expected) : ComparisonMatcherCreator::Matcher(expected) { } virtual bool matches(const T &actual) const override { return actual != this->_expected; } virtual std::string format() const override { return std::string("!=") + Formatter::format(this->_expected); } }; virtual TypedMatcher *createMatcher() const override { return new Matcher(this->_expected); } }; } struct AnyMatcher { } static _; template internal::TypedAnyMatcher Any() { internal::TypedAnyMatcher rv; return rv; } template internal::EqMatcherCreator Eq(const T &arg) { internal::EqMatcherCreator rv(arg); return rv; } template internal::GtMatcherCreator Gt(const T &arg) { internal::GtMatcherCreator rv(arg); return rv; } template internal::GeMatcherCreator Ge(const T &arg) { internal::GeMatcherCreator rv(arg); return rv; } template internal::LtMatcherCreator Lt(const T &arg) { internal::LtMatcherCreator rv(arg); return rv; } template internal::LeMatcherCreator Le(const T &arg) { internal::LeMatcherCreator rv(arg); return rv; } template internal::NeMatcherCreator Ne(const T &arg) { internal::NeMatcherCreator rv(arg); return rv; } }