View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.errorprone;
6   
7   import com.qulice.spi.Violation;
8   import java.util.Collection;
9   import java.util.List;
10  import org.hamcrest.MatcherAssert;
11  import org.hamcrest.Matchers;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * Test case for {@link Diagnostics}.
16   *
17   * @since 1.0
18   */
19  final class DiagnosticsTest {
20  
21      @Test
22      void namesTheErrorProneCheck() {
23          MatcherAssert.assertThat(
24              "bracketed check name must survive into the violation",
25              new Diagnostics("ErrorProne", "/prj").violations(
26                  List.of("/prj/Foo.java:42: warning: [SelfAssignment] assigned to itself")
27              ).iterator().next().name(),
28              Matchers.equalTo("SelfAssignment")
29          );
30      }
31  
32      @Test
33      void namesThePlainCompilerAsCheck() {
34          MatcherAssert.assertThat(
35              "an unlabelled diagnostic cannot be left nameless",
36              new Diagnostics("ErrorProne", "/prj").violations(
37                  List.of("/prj/Foo.java:7: error: cannot find symbol")
38              ).iterator().next().name(),
39              Matchers.equalTo("javac")
40          );
41      }
42  
43      @Test
44      void keepsTheSourcePosition() {
45          MatcherAssert.assertThat(
46              "line number of the diagnostic must not be lost",
47              new Diagnostics("ErrorProne", "/prj").violations(
48                  List.of("/prj/Foo.java:19: warning: [removal] old() is deprecated")
49              ).iterator().next().lines(),
50              Matchers.equalTo("19")
51          );
52      }
53  
54      @Test
55      void readsWindowsDriveLetterPath() {
56          MatcherAssert.assertThat(
57              "a drive letter cannot be mistaken for the line separator",
58              new Diagnostics("ErrorProne", "C:\\prj").violations(
59                  List.of("C:\\prj\\Foo.java:7: error: cannot find symbol")
60              ).iterator().next().file(),
61              Matchers.equalTo("C:\\prj\\Foo.java")
62          );
63      }
64  
65      @Test
66      void blamesProjectForPositionlessDiagnostic() {
67          MatcherAssert.assertThat(
68              "a diagnostic with no source position must still land somewhere",
69              new Diagnostics("ErrorProne", "/prj").violations(
70                  List.of("error: warnings found and -Werror specified")
71              ).iterator().next().file(),
72              Matchers.equalTo("/prj")
73          );
74      }
75  
76      @Test
77      void dontReportCompilerChatter() {
78          final Collection<Violation> violations =
79              new Diagnostics("ErrorProne", "/prj").violations(
80                  List.of(
81                      "    int count() { return Missing.value(); }",
82                      "                         ^",
83                      "  symbol:   variable Missing",
84                      "  location: class Broken",
85                      "Note: Some input files use unchecked operations.",
86                      "1 error",
87                      "5 warnings"
88                  )
89              );
90          MatcherAssert.assertThat(
91              String.format("carets and counters are not violations: %s", violations),
92              violations,
93              Matchers.<Violation>empty()
94          );
95      }
96  }