aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/include/llvm/Testing/ADT/StringMapEntry.h
blob: 7cb5016205c5309d065b855d1f3a3e67dc1f4861 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- llvm/Testing/ADT/StringMapEntry.h ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TESTING_ADT_STRINGMAPENTRY_H_
#define LLVM_TESTING_ADT_STRINGMAPENTRY_H_

#include "llvm/ADT/StringMapEntry.h"
#include "gmock/gmock.h"
#include <ostream>
#include <type_traits>

namespace llvm {
namespace detail {

template <typename T, typename = std::void_t<>>
struct CanOutputToOStream : std::false_type {};

template <typename T>
struct CanOutputToOStream<T, std::void_t<decltype(std::declval<std::ostream &>()
                                                  << std::declval<T>())>>
    : std::true_type {};

} // namespace detail

/// Support for printing to std::ostream, for use with e.g. producing more
/// useful error messages with Google Test.
template <typename T>
std::ostream &operator<<(std::ostream &OS, const StringMapEntry<T> &E) {
  OS << "{\"" << E.getKey().data() << "\": ";
  if constexpr (detail::CanOutputToOStream<decltype(E.getValue())>::value) {
    OS << E.getValue();
  } else {
    OS << "non-printable value";
  }
  return OS << "}";
}

namespace detail {

template <typename StringMapEntryT>
class StringMapEntryMatcherImpl
    : public testing::MatcherInterface<StringMapEntryT> {
public:
  using ValueT = typename std::remove_reference_t<StringMapEntryT>::ValueType;

  template <typename KeyMatcherT, typename ValueMatcherT>
  StringMapEntryMatcherImpl(KeyMatcherT KeyMatcherArg,
                            ValueMatcherT ValueMatcherArg)
      : KeyMatcher(
            testing::SafeMatcherCast<const std::string &>(KeyMatcherArg)),
        ValueMatcher(
            testing::SafeMatcherCast<const ValueT &>(ValueMatcherArg)) {}

  void DescribeTo(std::ostream *OS) const override {
    *OS << "has a string key that ";
    KeyMatcher.DescribeTo(OS);
    *OS << ", and has a value that ";
    ValueMatcher.DescribeTo(OS);
  }

  void DescribeNegationTo(std::ostream *OS) const override {
    *OS << "has a string key that ";
    KeyMatcher.DescribeNegationTo(OS);
    *OS << ", or has a value that ";
    ValueMatcher.DescribeNegationTo(OS);
  }

  bool
  MatchAndExplain(StringMapEntryT Entry,
                  testing::MatchResultListener *ResultListener) const override {
    testing::StringMatchResultListener KeyListener;
    if (!KeyMatcher.MatchAndExplain(Entry.getKey().data(), &KeyListener)) {
      *ResultListener << ("which has a string key " +
                          (KeyListener.str().empty() ? "that doesn't match"
                                                     : KeyListener.str()));
      return false;
    }
    testing::StringMatchResultListener ValueListener;
    if (!ValueMatcher.MatchAndExplain(Entry.getValue(), &ValueListener)) {
      *ResultListener << ("which has a value " + (ValueListener.str().empty()
                                                      ? "that doesn't match"
                                                      : ValueListener.str()));
      return false;
    }
    *ResultListener << "which is a match";
    return true;
  }

private:
  const testing::Matcher<const std::string &> KeyMatcher;
  const testing::Matcher<const ValueT &> ValueMatcher;
};

template <typename KeyMatcherT, typename ValueMatcherT>
class StringMapEntryMatcher {
public:
  StringMapEntryMatcher(KeyMatcherT KMArg, ValueMatcherT VMArg)
      : KM(std::move(KMArg)), VM(std::move(VMArg)) {}

  template <typename StringMapEntryT>
  operator testing::Matcher<StringMapEntryT>() const { // NOLINT
    return testing::Matcher<StringMapEntryT>(
        new StringMapEntryMatcherImpl<const StringMapEntryT &>(KM, VM));
  }

private:
  const KeyMatcherT KM;
  const ValueMatcherT VM;
};

} // namespace detail

/// Returns a gMock matcher that matches a `StringMapEntry` whose string key
/// matches `KeyMatcher`, and whose value matches `ValueMatcher`.
template <typename KeyMatcherT, typename ValueMatcherT>
detail::StringMapEntryMatcher<KeyMatcherT, ValueMatcherT>
IsStringMapEntry(KeyMatcherT KM, ValueMatcherT VM) {
  return detail::StringMapEntryMatcher<KeyMatcherT, ValueMatcherT>(
      std::move(KM), std::move(VM));
}

} // namespace llvm

#endif

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif