aboutsummaryrefslogtreecommitdiff
path: root/effcee/check_test.cc
blob: d5c118a4a6b24053775381d102d7ea440d1933fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2017 The Effcee Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <vector>
#include "gmock/gmock.h"

#include "check.h"

namespace {

using effcee::Check;
using effcee::Options;
using effcee::CheckList;
using effcee::ParseChecks;
using effcee::Result;
using effcee::StringPiece;
using ::testing::Combine;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::ValuesIn;

using Part = effcee::Check::Part;
using Status = effcee::Result::Status;
using Type = Check::Type;
using VarMapping = effcee::VarMapping;

// Check class

// Returns a vector of all Check types.
std::vector<Type> AllTypes() {
  return {Type::Simple, Type::Next,  Type::Same,
          Type::DAG,    Type::Label, Type::Not};
}

using CheckTypeTest = ::testing::TestWithParam<Type>;

TEST_P(CheckTypeTest, ConstructWithAnyType) {
  Check check(GetParam(), "");
  EXPECT_THAT(check.type(), Eq(GetParam()));
}

INSTANTIATE_TEST_SUITE_P(AllTypes, CheckTypeTest, ValuesIn(AllTypes()));

using CheckParamTest = ::testing::TestWithParam<StringPiece>;

TEST_P(CheckParamTest, ConstructWithSampleParamValue) {
  Check check(Type::Simple, GetParam());
  // The contents are the same.
  EXPECT_THAT(check.param(), Eq(GetParam()));
  // The referenced storage is the same.
  EXPECT_THAT(check.param().data(), Eq(GetParam().data()));
}

INSTANTIATE_TEST_SUITE_P(SampleParams, CheckParamTest,
                         ValuesIn(std::vector<StringPiece>{
                             "", "a b c", "The wind {{in}} the willows\n",
                             "Bring me back to the mountains of yore."}));

// Equality operator
TEST(CheckEqualityTest, TrueWhenAllComponentsSame) {
  EXPECT_TRUE(Check(Type::Simple, "abc") == Check(Type::Simple, "abc"));
}

TEST(CheckEqualityTest, FalseWhenTypeDifferent) {
  EXPECT_FALSE(Check(Type::Simple, "abc") == Check(Type::Next, "abc"));
}

TEST(CheckEqualityTest, FalseWhenParamDifferent) {
  EXPECT_FALSE(Check(Type::Simple, "abc") == Check(Type::Simple, "def"));
}

// Inequality operator
TEST(CheckInequalityTest, FalseWhenAllComponentsSame) {
  EXPECT_FALSE(Check(Type::Simple, "abc") != Check(Type::Simple, "abc"));
}

TEST(CheckInequalityTest, TrueWhenTypeDifferent) {
  EXPECT_TRUE(Check(Type::Simple, "abc") != Check(Type::Next, "abc"));
}

TEST(CheckInequalityTest, TrueWhenParamDifferent) {
  EXPECT_TRUE(Check(Type::Simple, "abc") != Check(Type::Simple, "def"));
}

// ParseChecks free function

TEST(ParseChecks, FreeFunctionLinks) {
  std::pair<Result, CheckList> parsed(ParseChecks("", Options()));
}

TEST(ParseChecks, FailWhenRulePrefixIsEmpty) {
  const auto parsed(ParseChecks("CHECK: now", Options().SetPrefix("")));
  const Result& result = parsed.first;
  const CheckList& pattern = parsed.second;
  EXPECT_THAT(result.status(), Eq(Status::BadOption));
  EXPECT_THAT(result.message(), Eq("Rule prefix is empty"));
  EXPECT_THAT(pattern.size(), Eq(0));
}

TEST(ParseChecks, FailWhenRulePrefixIsWhitespace) {
  const auto parsed(ParseChecks("CHECK: now", Options().SetPrefix("\t\n ")));
  const Result& result = parsed.first;
  const CheckList& pattern = parsed.second;
  EXPECT_THAT(result.status(), Eq(Status::BadOption));
  EXPECT_THAT(result.message(),
              Eq("Rule prefix is whitespace.  That's silly."));
  EXPECT_THAT(pattern.size(), Eq(0));
}

TEST(ParseChecks, FailWhenChecksAbsent) {
  const auto parsed(ParseChecks("no checks", Options()));
  const Result& result = parsed.first;
  const CheckList& pattern = parsed.second;
  EXPECT_THAT(result.status(), Eq(Status::NoRules));
  EXPECT_THAT(result.message(),
              Eq("No check rules specified. Looking for prefix CHECK"));
  EXPECT_THAT(pattern.size(), Eq(0));
}

TEST(ParseChecks, FailWhenChecksAbsentWithCustomPrefix) {
  const auto parsed(ParseChecks("CHECK: now", Options().SetPrefix("FOO")));
  const Result& result = parsed.first;
  const CheckList& pattern = parsed.second;
  EXPECT_THAT(result.status(), Eq(Status::NoRules));
  EXPECT_THAT(result.message(),
              Eq("No check rules specified. Looking for prefix FOO"));
  EXPECT_THAT(pattern.size(), Eq(0));
}

TEST(ParseChecks, FindSimpleCheck) {
  const auto parsed = ParseChecks("CHECK: now", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "now")})));
}

TEST(ParseChecks, FindSimpleCheckWithCustomPrefix) {
  const auto parsed = ParseChecks("FOO: how", Options().SetPrefix("FOO"));
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "how")})));
}

TEST(ParseChecks, FindSimpleCheckWithCustomPrefixHavingRegexpMetachars) {
  const auto parsed = ParseChecks("[::alpha::]^\\d: how",
                                  Options().SetPrefix("[::alpha::]^\\d"));
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "how")})));
}

TEST(ParseChecks, FindSimpleCheckPartwayThroughLine) {
  const auto parsed = ParseChecks("some other garbageCHECK: now", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "now")})));
}

TEST(ParseChecks, FindSimpleCheckCheckListWithoutSurroundingWhitespace) {
  const auto parsed = ParseChecks("CHECK:now", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "now")})));
}

TEST(ParseChecks, FindSimpleCheckCheckListWhileStrippingSurroundingWhitespace) {
  const auto parsed = ParseChecks("CHECK: \t   now\t\t  ", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "now")})));
}

TEST(ParseChecks, FindSimpleCheckCountsLinesCorrectly) {
  const auto parsed = ParseChecks("\n\nCHECK: now", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "now")})));
}

TEST(ParseChecks, FindSimpleChecksOnSeparateLines) {
  const auto parsed =
      ParseChecks("CHECK: now\n\n\nCHECK: and \n CHECK: then", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second, Eq(CheckList({Check(Type::Simple, "now"),
                                           Check(Type::Simple, "and"),
                                           Check(Type::Simple, "then")})));
}

TEST(ParseChecks, FindSimpleChecksOnlyOncePerLine) {
  const auto parsed = ParseChecks("CHECK: now CHECK: then", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second,
              Eq(CheckList({Check(Type::Simple, "now CHECK: then")})));
}

// Test parsing of the different check rule types.

using ParseChecksTypeTest = ::testing::TestWithParam<
    std::tuple<std::string, std::pair<std::string, Type>>>;

TEST_P(ParseChecksTypeTest, Successful) {
  const auto& prefix = std::get<0>(GetParam());
  const auto& type_str = std::get<0>(std::get<1>(GetParam()));
  const Type& type = std::get<1>(std::get<1>(GetParam()));
  // A CHECK-SAME rule can't appear first, so insert a CHECK: rule first.
  const std::string input = prefix + ": here\n" + prefix + type_str + ": now";
  const auto parsed = ParseChecks(input, Options().SetPrefix(prefix));
  EXPECT_THAT(parsed.first.status(), Eq(Status::Ok));
  EXPECT_THAT(parsed.second,
              Eq(CheckList({Check(Type::Simple, "here"), Check(type, "now")})));
}

// Returns a vector of pairs. Each pair has first member being a check type
// suffix, and the second member is the corresponding check type.
std::vector<std::pair<std::string, Type>> AllCheckTypesAsPairs() {
  return {
      {"", Type::Simple},  {"-NEXT", Type::Next},   {"-SAME", Type::Same},
      {"-DAG", Type::DAG}, {"-LABEL", Type::Label}, {"-NOT", Type::Not},
  };
}

INSTANTIATE_TEST_SUITE_P(AllCheckTypes, ParseChecksTypeTest,
                         Combine(ValuesIn(std::vector<std::string>{"CHECK",
                                                                   "FOO"}),
                                 ValuesIn(AllCheckTypesAsPairs())));

using ParseChecksTypeFailTest = ::testing::TestWithParam<
    std::tuple<std::string, std::pair<std::string, Type>>>;

// This is just one way to fail.
TEST_P(ParseChecksTypeFailTest, FailureWhenNoColon) {
  const auto& prefix = std::get<0>(GetParam());
  const auto& type_str = std::get<0>(std::get<1>(GetParam()));
  const std::string input = prefix + type_str + "BAD now";
  const auto parsed = ParseChecks(input, Options().SetPrefix(prefix));
  EXPECT_THAT(parsed.first.status(), Eq(Status::NoRules));
  EXPECT_THAT(parsed.second, Eq(CheckList{}));
}

INSTANTIATE_TEST_SUITE_P(AllCheckTypes, ParseChecksTypeFailTest,
                         Combine(ValuesIn(std::vector<std::string>{"CHECK",
                                                                   "FOO"}),
                                 ValuesIn(AllCheckTypesAsPairs())));

TEST(ParseChecks, BadRegexpMatchTrailingSlashFails) {
  const auto parsed = ParseChecks("CHECK: {{\\}}", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::BadRule));
  EXPECT_THAT(parsed.first.message(), HasSubstr("invalid regex: \\"));
  EXPECT_THAT(parsed.second, Eq(CheckList({})));
}

TEST(ParseChecks, BadRegexpVardefUnboundOptionalFails) {
  const auto parsed = ParseChecks("CHECK: [[VAR:?]]", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::BadRule));
  EXPECT_THAT(parsed.first.message(),
              HasSubstr("invalid regex in variable definition for VAR: ?"));
  EXPECT_THAT(parsed.second, Eq(CheckList({})));
}

TEST(ParseChecks, CheckSameCantBeFirst) {
  const auto parsed = ParseChecks("CHECK-SAME: now", Options());
  EXPECT_THAT(parsed.first.status(), Eq(Status::BadRule));
  EXPECT_THAT(parsed.first.message(),
              HasSubstr("CHECK-SAME can't be the first check rule"));
  EXPECT_THAT(parsed.second, Eq(CheckList({})));
}

TEST(ParseChecks, CheckSameCantBeFirstDifferentPrefix) {
  const auto parsed = ParseChecks("BOO-SAME: now", Options().SetPrefix("BOO"));
  EXPECT_THAT(parsed.first.status(), Eq(Status::BadRule));
  EXPECT_THAT(parsed.first.message(),
              HasSubstr("BOO-SAME can't be the first check rule"));
  EXPECT_THAT(parsed.second, Eq(CheckList({})));
}

// Check::Matches
struct CheckMatchCase {
  std::string input;
  Check check;
  bool expected;
  std::string remaining;
  std::string captured;
};

using CheckMatchTest = ::testing::TestWithParam<CheckMatchCase>;

TEST_P(CheckMatchTest, Samples) {
  StringPiece str = GetParam().input;
  StringPiece captured;
  VarMapping vars;
  const bool matched = GetParam().check.Matches(&str, &captured, &vars);
  EXPECT_THAT(matched, Eq(GetParam().expected))
      << "Failed on input " << GetParam().input;
  EXPECT_THAT(std::string(str.data(), str.size()), Eq(GetParam().remaining));
  EXPECT_THAT(std::string(captured.data(), captured.size()),
              Eq(GetParam().captured));
  EXPECT_TRUE(vars.empty());
}

INSTANTIATE_TEST_SUITE_P(
    Simple, CheckMatchTest,
    ValuesIn(std::vector<CheckMatchCase>{
        {"hello", Check(Type::Simple, "hello"), true, "", "hello"},
        {"world", Check(Type::Simple, "hello"), false, "world", ""},
        {"in hello now", Check(Type::Simple, "hello"), true, " now", "hello"},
        {"hello", Check(Type::Same, "hello"), true, "", "hello"},
        {"world", Check(Type::Same, "hello"), false, "world", ""},
        {"in hello now", Check(Type::Same, "hello"), true, " now", "hello"},
        {"hello", Check(Type::Next, "hello"), true, "", "hello"},
        {"world", Check(Type::Next, "hello"), false, "world", ""},
        {"in hello now", Check(Type::Next, "hello"), true, " now", "hello"},
        {"hello", Check(Type::DAG, "hello"), true, "", "hello"},
        {"world", Check(Type::DAG, "hello"), false, "world", ""},
        {"in hello now", Check(Type::DAG, "hello"), true, " now", "hello"},
        {"hello", Check(Type::Label, "hello"), true, "", "hello"},
        {"world", Check(Type::Label, "hello"), false, "world", ""},
        {"in hello now", Check(Type::Label, "hello"), true, " now", "hello"},
        {"hello", Check(Type::Label, "hello"), true, "", "hello"},
        {"world", Check(Type::Label, "hello"), false, "world", ""},
        {"in hello now", Check(Type::Label, "hello"), true, " now", "hello"},
        {"hello", Check(Type::Not, "hello"), true, "", "hello"},
        {"world", Check(Type::Not, "hello"), false, "world", ""},
        {"in hello now", Check(Type::Not, "hello"), true, " now", "hello"},
    }));

// Check::Part::Regex

TEST(CheckPart, FixedPartRegex) {
  VarMapping vm;
  EXPECT_THAT(Part(Part::Type::Fixed, "abc").Regex(vm), Eq("abc"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a.bc").Regex(vm), Eq("a\\.bc"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a?bc").Regex(vm), Eq("a\\?bc"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a+bc").Regex(vm), Eq("a\\+bc"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a*bc").Regex(vm), Eq("a\\*bc"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a[b]").Regex(vm), Eq("a\\[b\\]"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a[-]").Regex(vm), Eq("a\\[\\-\\]"));
  EXPECT_THAT(Part(Part::Type::Fixed, "a(-)b").Regex(vm), Eq("a\\(\\-\\)b"));
}

TEST(CheckPart, RegexPartRegex) {
  VarMapping vm;
  EXPECT_THAT(Part(Part::Type::Regex, "abc").Regex(vm), Eq("abc"));
  EXPECT_THAT(Part(Part::Type::Regex, "a.bc").Regex(vm), Eq("a.bc"));
  EXPECT_THAT(Part(Part::Type::Regex, "a?bc").Regex(vm), Eq("a?bc"));
  EXPECT_THAT(Part(Part::Type::Regex, "a+bc").Regex(vm), Eq("a+bc"));
  EXPECT_THAT(Part(Part::Type::Regex, "a*bc").Regex(vm), Eq("a*bc"));
  EXPECT_THAT(Part(Part::Type::Regex, "a[b]").Regex(vm), Eq("a[b]"));
  EXPECT_THAT(Part(Part::Type::Regex, "a[-]").Regex(vm), Eq("a[-]"));
  EXPECT_THAT(Part(Part::Type::Regex, "a(-)b").Regex(vm), Eq("a(-)b"));
}

}  // namespace