1 //===- StringMapEntryTest.cpp ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Testing/ADT/StringMapEntry.h"
10 #include "llvm/ADT/StringMap.h"
12 #include "gtest/gtest.h"
19 using testing::Matcher
;
20 using testing::StrCaseEq
;
21 using testing::StringMatchResultListener
;
22 using testing::UnorderedElementsAre
;
24 template <typename T
> std::string
Describe(const Matcher
<T
> &M
, bool Match
) {
29 M
.DescribeNegationTo(&SS
);
34 template <typename T
, typename V
>
35 std::string
ExplainMatch(const Matcher
<T
> &Matcher
, const V
&Value
) {
36 StringMatchResultListener Listener
;
37 Matcher
.MatchAndExplain(Value
, &Listener
);
38 return Listener
.str();
41 TEST(IsStringMapEntryTest
, InnerMatchersAreExactValues
) {
42 llvm::StringMap
<int> Map
= {{"A", 1}};
43 EXPECT_THAT(*Map
.find("A"), IsStringMapEntry("A", 1));
46 TEST(IsStringMapEntryTest
, InnerMatchersAreOtherMatchers
) {
47 llvm::StringMap
<int> Map
= {{"A", 1}};
48 EXPECT_THAT(*Map
.find("A"), IsStringMapEntry(StrCaseEq("a"), Gt(0)));
51 TEST(IsStringMapEntryTest
, UseAsInnerMatcher
) {
52 llvm::StringMap
<int> Map
= {{"A", 1}, {"B", 2}};
53 EXPECT_THAT(Map
, UnorderedElementsAre(IsStringMapEntry("A", 1),
54 IsStringMapEntry("B", 2)));
57 TEST(IsStringMapEntryTest
, DescribeSelf
) {
58 Matcher
<llvm::StringMapEntry
<int>> M
= IsStringMapEntry("A", 1);
60 R
"(has a string key that is equal to "A
", and has a value that is equal to 1)",
63 R
"(has a string key that isn't equal to "A
", or has a value that isn't equal to 1)",
67 TEST(IsStringMapEntryTest
, ExplainSelfMatchSuccess
) {
68 llvm::StringMap
<int> Map
= {{"A", 1}};
69 Matcher
<llvm::StringMapEntry
<int>> M
= IsStringMapEntry("A", 1);
70 EXPECT_EQ(R
"(which is a match)", ExplainMatch(M
, *Map
.find("A")));
73 TEST(IsStringMapEntryTest
, ExplainSelfMatchFailsOnKey
) {
74 llvm::StringMap
<int> Map
= {{"B", 1}};
75 Matcher
<llvm::StringMapEntry
<int>> M
= IsStringMapEntry("A", 1);
76 EXPECT_EQ(R
"(which has a string key that doesn't match)",
77 ExplainMatch(M
, *Map
.find("B")));
80 TEST(IsStringMapEntryTest
, ExplainSelfMatchFailsOnValue
) {
81 llvm::StringMap
<int> Map
= {{"A", 2}};
82 Matcher
<llvm::StringMapEntry
<int>> M
= IsStringMapEntry("A", 1);
83 EXPECT_EQ(R
"(which has a value that doesn't match)",
84 ExplainMatch(M
, *Map
.find("A")));