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 "15: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,\\n This is a multi-line message"
86 + ".\\n ']]/TEXT_BLOCK_LITERAL_END"
87 );
88
89 runVerifications(moduleConfig, fileToProcess, expectedViolation,
90 expectedXpathQueries);
91 }
92
93 @Test
94 public void testTextBlocksFormatClosingQuotesNotOnNewLine() throws Exception {
95 final File fileToProcess = new File(
96 getPath("InputXpathTextBlockGoogleStyleFormatting3.java"));
97
98 final DefaultConfiguration moduleConfig =
99 createModuleConfig(CLASS);
100
101 final String[] expectedViolation = {
102 "8:21: " + getCheckMessage(CLASS,
103 TextBlockGoogleStyleFormattingCheck.MSG_CLOSE_QUOTES_ERROR),
104 };
105
106 final List<String> expectedXpathQueries = List.of(
107 "/COMPILATION_UNIT/CLASS_DEF"
108 + "[./IDENT[@text='InputXpathTextBlockGoogleStyleFormatting3']]"
109 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='textFun1']]/SLIST/VARIABLE_DEF"
110 + "[./IDENT[@text='simpleScriptViolation']]/ASSIGN/EXPR/"
111 + "TEXT_BLOCK_LITERAL_BEGIN[./TEXT_BLOCK_CONTENT"
112 + "[@text='\\n this is sample text']]/TEXT_BLOCK_LITERAL_END"
113 );
114
115 runVerifications(moduleConfig, fileToProcess, expectedViolation,
116 expectedXpathQueries);
117 }
118 }