1 | //===- StringMapEntryTest.cpp ---------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/Testing/ADT/StringMapEntry.h" |
10 | #include "llvm/ADT/StringMap.h" |
11 | |
12 | #include "gtest/gtest.h" |
13 | #include <sstream> |
14 | |
15 | namespace llvm { |
16 | namespace { |
17 | |
18 | using testing::Gt; |
19 | using testing::Matcher; |
20 | using testing::StrCaseEq; |
21 | using testing::StringMatchResultListener; |
22 | using testing::UnorderedElementsAre; |
23 | |
24 | template <typename T> std::string Describe(const Matcher<T> &M, bool Match) { |
25 | std::stringstream SS; |
26 | if (Match) { |
27 | M.DescribeTo(&SS); |
28 | } else { |
29 | M.DescribeNegationTo(&SS); |
30 | } |
31 | return SS.str(); |
32 | } |
33 | |
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(); |
39 | } |
40 | |
41 | TEST(IsStringMapEntryTest, InnerMatchersAreExactValues) { |
42 | llvm::StringMap<int> Map = {{"A" , 1}}; |
43 | EXPECT_THAT(*Map.find("A" ), IsStringMapEntry("A" , 1)); |
44 | } |
45 | |
46 | TEST(IsStringMapEntryTest, InnerMatchersAreOtherMatchers) { |
47 | llvm::StringMap<int> Map = {{"A" , 1}}; |
48 | EXPECT_THAT(*Map.find("A" ), IsStringMapEntry(StrCaseEq("a" ), Gt(0))); |
49 | } |
50 | |
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))); |
55 | } |
56 | |
57 | TEST(IsStringMapEntryTest, DescribeSelf) { |
58 | Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry(KM: "A" , VM: 1); |
59 | EXPECT_EQ( |
60 | R"(has a string key that is equal to "A", and has a value that is equal to 1)" , |
61 | Describe(M, true)); |
62 | EXPECT_EQ( |
63 | R"(has a string key that isn't equal to "A", or has a value that isn't equal to 1)" , |
64 | Describe(M, false)); |
65 | } |
66 | |
67 | TEST(IsStringMapEntryTest, ExplainSelfMatchSuccess) { |
68 | llvm::StringMap<int> Map = {{"A" , 1}}; |
69 | Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry(KM: "A" , VM: 1); |
70 | EXPECT_EQ(R"(which is a match)" , ExplainMatch(M, *Map.find("A" ))); |
71 | } |
72 | |
73 | TEST(IsStringMapEntryTest, ExplainSelfMatchFailsOnKey) { |
74 | llvm::StringMap<int> Map = {{"B" , 1}}; |
75 | Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry(KM: "A" , VM: 1); |
76 | EXPECT_EQ(R"(which has a string key that doesn't match)" , |
77 | ExplainMatch(M, *Map.find("B" ))); |
78 | } |
79 | |
80 | TEST(IsStringMapEntryTest, ExplainSelfMatchFailsOnValue) { |
81 | llvm::StringMap<int> Map = {{"A" , 2}}; |
82 | Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry(KM: "A" , VM: 1); |
83 | EXPECT_EQ(R"(which has a value that doesn't match)" , |
84 | ExplainMatch(M, *Map.find("A" ))); |
85 | } |
86 | |
87 | } // namespace |
88 | } // namespace llvm |
89 | |