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
15namespace llvm {
16namespace {
17
18using testing::Gt;
19using testing::Matcher;
20using testing::StrCaseEq;
21using testing::StringMatchResultListener;
22using testing::UnorderedElementsAre;
23
24template <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
34template <typename T, typename V>
35std::string ExplainMatch(const Matcher<T> &Matcher, const V &Value) {
36 StringMatchResultListener Listener;
37 Matcher.MatchAndExplain(Value, &Listener);
38 return Listener.str();
39}
40
41TEST(IsStringMapEntryTest, InnerMatchersAreExactValues) {
42 llvm::StringMap<int> Map = {{"A", 1}};
43 EXPECT_THAT(*Map.find("A"), IsStringMapEntry("A", 1));
44}
45
46TEST(IsStringMapEntryTest, InnerMatchersAreOtherMatchers) {
47 llvm::StringMap<int> Map = {{"A", 1}};
48 EXPECT_THAT(*Map.find("A"), IsStringMapEntry(StrCaseEq("a"), Gt(0)));
49}
50
51TEST(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
57TEST(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
67TEST(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
73TEST(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
80TEST(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

source code of llvm/unittests/Testing/ADT/StringMapEntryTest.cpp