/** * Copyright (C) 2021-present MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the Server Side Public License, version 1, * as published by MongoDB, Inc. * * 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 * Server Side Public License for more details. * * You should have received a copy of the Server Side Public License * along with this program. If not, see * . * * As a special exception, the copyright holders give permission to link the * code of portions of this program with the OpenSSL library under certain * conditions as described in each individual source file and distribute * linked combinations including the program with the OpenSSL library. You * must comply with the Server Side Public License in all respects for * all of the code used other than as permitted herein. If you modify file(s) * with this exception, you may extend this exception to your version of the * file(s), but you are not obligated to do so. If you do not wish to do so, * delete this exception statement from your version. If you delete this * exception statement from all source files in the program, then also delete * it in the license file. */ #pragma once #include #include #include #include #include #include #include "mongo/base/string_data.h" #include "mongo/bson/bsonelement.h" #include "mongo/bson/bsonobj.h" #include "mongo/stdx/type_traits.h" #include "mongo/unittest/assert.h" #include "mongo/unittest/matcher_core.h" /** * Defines a basic set of matchers to be used with the ASSERT_THAT macro (see * `assert_that.h`). It's intended that matchers to support higher-level * components will be defined alongside that component's other unit testing * support classes, rather than in this file. */ namespace mongo::unittest::match { /* * A uniform wrapper around any matcher that accepts a `T`, so they can * be treated homogeneously. * * Example: * std::vector> vec{Eq(123), AllOf(Gt(100), Lt(200))}; **/ template class TypeErasedMatcher { public: using value_type = T; template explicit TypeErasedMatcher(const M& m) : _m{std::make_shared>(m)} {} virtual ~TypeErasedMatcher() = default; std::string describe() const { return _m->describe(); } MatchResult match(const T& v) const { return _m->match(v); } private: struct BasicMatch { virtual std::string describe() const = 0; virtual MatchResult match(const T& v) const = 0; }; template class TypedMatch : public BasicMatch { template using CanMatchOp = decltype(std::declval().match(std::declval())); public: explicit TypedMatch(const M& m) : _m{&m} {} virtual ~TypedMatch() = default; std::string describe() const override { return _m->describe(); } MatchResult match(const T& v) const override { if constexpr (!stdx::is_detected_v) { return MatchResult{ false, format(FMT_STRING("Matcher does not accept {}"), demangleName(typeid(T)))}; } else { return _m->match(v); } } private: const M* _m; }; std::shared_ptr _m; }; /** Always true: matches any value of any type. */ class Any : public Matcher { public: std::string describe() const { return "Any"; } template MatchResult match(const X&) const { return MatchResult{true}; } }; namespace detail { /** * MatchResult will be false when `m.match(v)` fails template substitution. * Can be used e.g. to produce a runtime-dispatched matcher for variant types. * * Example: * typeTolerantMatch(Eq("hello"), 1234); // Fails to match but compiles */ template MatchResult typeTolerantMatch(const M& m, const T& v) { return TypeErasedMatcher(m).match(v); } template