1 /*
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <thrift/compiler/ast/diagnostic.h>
18
19 #include <folly/portability/GTest.h>
20
21 namespace apache::thrift::compiler {
22 namespace {
23
24 class DiagnosticTest : public ::testing::Test {};
25
TEST_F(DiagnosticTest,Str)26 TEST_F(DiagnosticTest, Str) {
27 EXPECT_EQ(
28 diagnostic(diagnostic_level::debug, "m", "f", 1, "t").str(),
29 "[DEBUG:f:1] (last token was 't') m");
30 EXPECT_EQ(
31 diagnostic(diagnostic_level::failure, "m", "f", 0, "t").str(),
32 "[FAILURE:f] (last token was 't') m");
33 EXPECT_EQ(
34 diagnostic(diagnostic_level::info, "m", "f", 1).str(), "[INFO:f:1] m");
35 EXPECT_EQ(
36 diagnostic(diagnostic_level::parse_error, "m", "f").str(), "[ERROR:f] m");
37 EXPECT_EQ(
38 diagnostic(diagnostic_level::warning, "m", "").str(), "[WARNING:] m");
39 }
40
41 class DiagnosticResultsTest : public ::testing::Test {};
42
TEST_F(DiagnosticResultsTest,Empty)43 TEST_F(DiagnosticResultsTest, Empty) {
44 diagnostic_results results;
45 EXPECT_FALSE(results.has_failure());
46 for (int i = 0; i <= static_cast<int>(diagnostic_level::debug); ++i) {
47 EXPECT_EQ(results.count(static_cast<diagnostic_level>(i)), 0);
48 }
49 }
50
TEST_F(DiagnosticResultsTest,Count)51 TEST_F(DiagnosticResultsTest, Count) {
52 diagnostic_results results;
53 for (int i = 0; i <= static_cast<int>(diagnostic_level::debug); ++i) {
54 auto level = static_cast<diagnostic_level>(i);
55 for (int j = 0; j <= i; ++j) {
56 results.add({level, "hi", "file"});
57 }
58 }
59 for (int i = 0; i <= static_cast<int>(diagnostic_level::debug); ++i) {
60 EXPECT_EQ(results.count(static_cast<diagnostic_level>(i)), i + 1);
61 }
62 }
63
64 } // namespace
65 } // namespace apache::thrift::compiler
66