1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter;
21
22 import java.io.File;
23 import java.util.List;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28 import com.puppycrawl.tools.checkstyle.checks.coding.TextBlockGoogleStyleFormattingCheck;
29
30 public class XpathRegressionTextBlockGoogleStyleFormattingTest extends AbstractXpathTestSupport {
31
32 private static final Class<TextBlockGoogleStyleFormattingCheck> CLASS =
33 TextBlockGoogleStyleFormattingCheck.class;
34
35 @Override
36 protected String getCheckName() {
37 return CLASS.getSimpleName();
38 }
39
40 @Test
41 public void testTextBlocksFormatNotVerticallyAligned() throws Exception {
42 final File fileToProcess = new File(
43 getPath("InputXpathTextBlockGoogleStyleFormatting.java"));
44
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(CLASS);
47
48 final String[] expectedViolation = {
49 "8:13: " + getCheckMessage(CLASS,
50 TextBlockGoogleStyleFormattingCheck.MSG_VERTICALLY_UNALIGNED),
51 };
52
53 final List<String> expectedXpathQueries = List.of(
54 "/COMPILATION_UNIT/CLASS_DEF"
55 + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun']]"
57 + "/SLIST/VARIABLE_DEF[./IDENT[@text='simpleScript']]/ASSIGN/EXPR"
58 + "/TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
59 + "[@text='\\n s\\n ']]/TEXT_BLOCK_LITERAL_END"
60 );
61
62 runVerifications(moduleConfig, fileToProcess, expectedViolation,
63 expectedXpathQueries);
64 }
65
66 @Test
67 public void testTextBlocksFormatNotVerticallyAlignedInMethodCall() throws Exception {
68 final File fileToProcess = new File(
69 getPath("InputXpathTextBlockGoogleStyleFormatting2.java"));
70
71 final DefaultConfiguration moduleConfig =
72 createModuleConfig(CLASS);
73
74 final String[] expectedViolation = {
75 "14:13: " + getCheckMessage(CLASS,
76 TextBlockGoogleStyleFormattingCheck.MSG_VERTICALLY_UNALIGNED),
77 };
78
79 final List<String> expectedXpathQueries = List.of(
80 "/COMPILATION_UNIT/CLASS_DEF"
81 + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting2']]"
82 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun1']]/SLIST/EXPR"
83 + "/METHOD_CALL[./IDENT[@text='getData']]/ELIST/EXPR/"
84 + "TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
85 + "[@text='\\n Hello,"
86 + "\\n This is a multi-line message"
87 + ".\\n ']]/TEXT_BLOCK_LITERAL_END"
88 );
89
90 runVerifications(moduleConfig, fileToProcess, expectedViolation,
91 expectedXpathQueries);
92 }
93
94 @Test
95 public void testTextBlocksFormatClosingQuotesNotOnNewLine() throws Exception {
96 final File fileToProcess = new File(
97 getPath("InputXpathTextBlockGoogleStyleFormatting3.java"));
98
99 final DefaultConfiguration moduleConfig =
100 createModuleConfig(CLASS);
101
102 final String[] expectedViolation = {
103 "9:20: " + getCheckMessage(CLASS,
104 TextBlockGoogleStyleFormattingCheck.MSG_VERTICALLY_UNALIGNED),
105 };
106
107 final List<String> expectedXpathQueries = List.of(
108 "/COMPILATION_UNIT/CLASS_DEF"
109 + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting3']]"
110 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun1']]/SLIST/VARIABLE_DEF"
111 + "[./IDENT[@text='simpleScriptViolation']]/ASSIGN/EXPR/"
112 + "TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
113 + "[@text='\\n this is sample text\\n"
114 + " ']]/TEXT_BLOCK_LITERAL_END"
115 );
116
117 runVerifications(moduleConfig, fileToProcess, expectedViolation,
118 expectedXpathQueries);
119 }
120 }