1*f6b66cbcSWei Yi Tee //===- StringMapEntryTest.cpp ---------------------------------------------===//
2*f6b66cbcSWei Yi Tee //
3*f6b66cbcSWei Yi Tee // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f6b66cbcSWei Yi Tee // See https://llvm.org/LICENSE.txt for license information.
5*f6b66cbcSWei Yi Tee // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f6b66cbcSWei Yi Tee //
7*f6b66cbcSWei Yi Tee //===----------------------------------------------------------------------===//
8*f6b66cbcSWei Yi Tee
9*f6b66cbcSWei Yi Tee #include "llvm/Testing/ADT/StringMapEntry.h"
10*f6b66cbcSWei Yi Tee #include "llvm/ADT/StringMap.h"
11*f6b66cbcSWei Yi Tee
12*f6b66cbcSWei Yi Tee #include "gtest/gtest.h"
13*f6b66cbcSWei Yi Tee #include <sstream>
14*f6b66cbcSWei Yi Tee
15*f6b66cbcSWei Yi Tee namespace llvm {
16*f6b66cbcSWei Yi Tee namespace {
17*f6b66cbcSWei Yi Tee
18*f6b66cbcSWei Yi Tee using testing::Gt;
19*f6b66cbcSWei Yi Tee using testing::Matcher;
20*f6b66cbcSWei Yi Tee using testing::StrCaseEq;
21*f6b66cbcSWei Yi Tee using testing::StringMatchResultListener;
22*f6b66cbcSWei Yi Tee using testing::UnorderedElementsAre;
23*f6b66cbcSWei Yi Tee
Describe(const Matcher<T> & M,bool Match)24*f6b66cbcSWei Yi Tee template <typename T> std::string Describe(const Matcher<T> &M, bool Match) {
25*f6b66cbcSWei Yi Tee std::stringstream SS;
26*f6b66cbcSWei Yi Tee if (Match) {
27*f6b66cbcSWei Yi Tee M.DescribeTo(&SS);
28*f6b66cbcSWei Yi Tee } else {
29*f6b66cbcSWei Yi Tee M.DescribeNegationTo(&SS);
30*f6b66cbcSWei Yi Tee }
31*f6b66cbcSWei Yi Tee return SS.str();
32*f6b66cbcSWei Yi Tee }
33*f6b66cbcSWei Yi Tee
34*f6b66cbcSWei Yi Tee template <typename T, typename V>
ExplainMatch(const Matcher<T> & Matcher,const V & Value)35*f6b66cbcSWei Yi Tee std::string ExplainMatch(const Matcher<T> &Matcher, const V &Value) {
36*f6b66cbcSWei Yi Tee StringMatchResultListener Listener;
37*f6b66cbcSWei Yi Tee Matcher.MatchAndExplain(Value, &Listener);
38*f6b66cbcSWei Yi Tee return Listener.str();
39*f6b66cbcSWei Yi Tee }
40*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,InnerMatchersAreExactValues)41*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, InnerMatchersAreExactValues) {
42*f6b66cbcSWei Yi Tee llvm::StringMap<int> Map = {{"A", 1}};
43*f6b66cbcSWei Yi Tee EXPECT_THAT(*Map.find("A"), IsStringMapEntry("A", 1));
44*f6b66cbcSWei Yi Tee }
45*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,InnerMatchersAreOtherMatchers)46*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, InnerMatchersAreOtherMatchers) {
47*f6b66cbcSWei Yi Tee llvm::StringMap<int> Map = {{"A", 1}};
48*f6b66cbcSWei Yi Tee EXPECT_THAT(*Map.find("A"), IsStringMapEntry(StrCaseEq("a"), Gt(0)));
49*f6b66cbcSWei Yi Tee }
50*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,UseAsInnerMatcher)51*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, UseAsInnerMatcher) {
52*f6b66cbcSWei Yi Tee llvm::StringMap<int> Map = {{"A", 1}, {"B", 2}};
53*f6b66cbcSWei Yi Tee EXPECT_THAT(Map, UnorderedElementsAre(IsStringMapEntry("A", 1),
54*f6b66cbcSWei Yi Tee IsStringMapEntry("B", 2)));
55*f6b66cbcSWei Yi Tee }
56*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,DescribeSelf)57*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, DescribeSelf) {
58*f6b66cbcSWei Yi Tee Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry("A", 1);
59*f6b66cbcSWei Yi Tee EXPECT_EQ(
60*f6b66cbcSWei Yi Tee R"(has a string key that is equal to "A", and has a value that is equal to 1)",
61*f6b66cbcSWei Yi Tee Describe(M, true));
62*f6b66cbcSWei Yi Tee EXPECT_EQ(
63*f6b66cbcSWei Yi Tee R"(has a string key that isn't equal to "A", or has a value that isn't equal to 1)",
64*f6b66cbcSWei Yi Tee Describe(M, false));
65*f6b66cbcSWei Yi Tee }
66*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,ExplainSelfMatchSuccess)67*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, ExplainSelfMatchSuccess) {
68*f6b66cbcSWei Yi Tee llvm::StringMap<int> Map = {{"A", 1}};
69*f6b66cbcSWei Yi Tee Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry("A", 1);
70*f6b66cbcSWei Yi Tee EXPECT_EQ(R"(which is a match)", ExplainMatch(M, *Map.find("A")));
71*f6b66cbcSWei Yi Tee }
72*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,ExplainSelfMatchFailsOnKey)73*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, ExplainSelfMatchFailsOnKey) {
74*f6b66cbcSWei Yi Tee llvm::StringMap<int> Map = {{"B", 1}};
75*f6b66cbcSWei Yi Tee Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry("A", 1);
76*f6b66cbcSWei Yi Tee EXPECT_EQ(R"(which has a string key that doesn't match)",
77*f6b66cbcSWei Yi Tee ExplainMatch(M, *Map.find("B")));
78*f6b66cbcSWei Yi Tee }
79*f6b66cbcSWei Yi Tee
TEST(IsStringMapEntryTest,ExplainSelfMatchFailsOnValue)80*f6b66cbcSWei Yi Tee TEST(IsStringMapEntryTest, ExplainSelfMatchFailsOnValue) {
81*f6b66cbcSWei Yi Tee llvm::StringMap<int> Map = {{"A", 2}};
82*f6b66cbcSWei Yi Tee Matcher<llvm::StringMapEntry<int>> M = IsStringMapEntry("A", 1);
83*f6b66cbcSWei Yi Tee EXPECT_EQ(R"(which has a value that doesn't match)",
84*f6b66cbcSWei Yi Tee ExplainMatch(M, *Map.find("A")));
85*f6b66cbcSWei Yi Tee }
86*f6b66cbcSWei Yi Tee
87*f6b66cbcSWei Yi Tee } // namespace
88*f6b66cbcSWei Yi Tee } // namespace llvm
89