Ninja
explanations_test.cc
Go to the documentation of this file.
1// Copyright 2024 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "explanations.h"
16
17#include "test.h"
18
19namespace {
20
21const void* MakeItem(size_t v) {
22 return reinterpret_cast<const void*>(v);
23}
24
25} // namespace
26
28 Explanations exp;
29
30 exp.Record(MakeItem(1), "first explanation");
31 exp.Record(MakeItem(1), "second explanation");
32 exp.Record(MakeItem(2), "third explanation");
33 exp.Record(MakeItem(2), "fourth %s", "explanation");
34
35 std::vector<std::string> list;
36
37 exp.LookupAndAppend(MakeItem(0), &list);
38 ASSERT_TRUE(list.empty());
39
40 exp.LookupAndAppend(MakeItem(1), &list);
41 ASSERT_EQ(2u, list.size());
42 EXPECT_EQ(list[0], "first explanation");
43 EXPECT_EQ(list[1], "second explanation");
44
45 exp.LookupAndAppend(MakeItem(2), &list);
46 ASSERT_EQ(4u, list.size());
47 EXPECT_EQ(list[0], "first explanation");
48 EXPECT_EQ(list[1], "second explanation");
49 EXPECT_EQ(list[2], "third explanation");
50 EXPECT_EQ(list[3], "fourth explanation");
51}
52
53TEST(Explanations, OptionalExplanationsNonNull) {
54 Explanations parent;
55 OptionalExplanations exp(&parent);
56
57 exp.Record(MakeItem(1), "first explanation");
58 exp.Record(MakeItem(1), "second explanation");
59 exp.Record(MakeItem(2), "third explanation");
60 exp.Record(MakeItem(2), "fourth %s", "explanation");
61
62 std::vector<std::string> list;
63
64 exp.LookupAndAppend(MakeItem(0), &list);
65 ASSERT_TRUE(list.empty());
66
67 exp.LookupAndAppend(MakeItem(1), &list);
68 ASSERT_EQ(2u, list.size());
69 EXPECT_EQ(list[0], "first explanation");
70 EXPECT_EQ(list[1], "second explanation");
71
72 exp.LookupAndAppend(MakeItem(2), &list);
73 ASSERT_EQ(4u, list.size());
74 EXPECT_EQ(list[0], "first explanation");
75 EXPECT_EQ(list[1], "second explanation");
76 EXPECT_EQ(list[2], "third explanation");
77 EXPECT_EQ(list[3], "fourth explanation");
78}
79
80TEST(Explanations, OptionalExplanationsWithNullPointer) {
81 OptionalExplanations exp(nullptr);
82
83 exp.Record(MakeItem(1), "first explanation");
84 exp.Record(MakeItem(1), "second explanation");
85 exp.Record(MakeItem(2), "third explanation");
86 exp.Record(MakeItem(2), "fourth %s", "explanation");
87
88 std::vector<std::string> list;
89 exp.LookupAndAppend(MakeItem(0), &list);
90 ASSERT_TRUE(list.empty());
91
92 exp.LookupAndAppend(MakeItem(1), &list);
93 ASSERT_TRUE(list.empty());
94
95 exp.LookupAndAppend(MakeItem(2), &list);
96 ASSERT_TRUE(list.empty());
97}
TEST(Explanations, Explanations)
A class used to record a list of explanation strings associated with a given 'item' pointer.
void LookupAndAppend(const void *item, std::vector< std::string > *out)
Lookup the explanations recorded for |item|, and append them to |*out|, if any.
void Record(const void *item, const char *fmt,...)
Record an explanation for |item| if this instance is enabled.
Convenience wrapper for an Explanations pointer, which can be null if no explanations need to be reco...
void Record(const void *item, const char *fmt,...)
void LookupAndAppend(const void *item, std::vector< std::string > *out)