1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.whitespace;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForInitializerPadCheck.MSG_NOT_PRECEDED;
24 import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForInitializerPadCheck.MSG_PRECEDED;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
31 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33
34 public class EmptyForInitializerPadCheckTest
35 extends AbstractModuleTestSupport {
36
37 @Override
38 protected String getPackageLocation() {
39 return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptyforinitializerpad";
40 }
41
42 @Test
43 public void testGetRequiredTokens() {
44 final EmptyForInitializerPadCheck checkObj = new EmptyForInitializerPadCheck();
45 final int[] expected = {TokenTypes.FOR_INIT};
46 assertWithMessage("Default required tokens are invalid")
47 .that(checkObj.getRequiredTokens())
48 .isEqualTo(expected);
49 }
50
51 @Test
52 public void testDefault() throws Exception {
53 final String[] expected = {
54 "51:15: " + getCheckMessage(MSG_PRECEDED, ";"),
55 };
56 verifyWithInlineConfigParser(
57 getPath("InputEmptyForInitializerPadDefaultConfig.java"), expected);
58 }
59
60 @Test
61 public void testSpaceOption() throws Exception {
62 final String[] expected = {
63 "54:14: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
64 };
65 verifyWithInlineConfigParser(
66 getPath("InputEmptyForInitializerPad.java"), expected);
67 }
68
69 @Test
70 public void testGetAcceptableTokens() {
71 final EmptyForInitializerPadCheck emptyForInitializerPadCheckObj =
72 new EmptyForInitializerPadCheck();
73 final int[] actual = emptyForInitializerPadCheckObj.getAcceptableTokens();
74 final int[] expected = {
75 TokenTypes.FOR_INIT,
76 };
77 assertWithMessage("Default acceptable tokens are invalid")
78 .that(actual)
79 .isEqualTo(expected);
80 }
81
82
83
84
85
86 @Test
87 public void testPadOptionValueOf() {
88 final PadOption option = PadOption.valueOf("NOSPACE");
89 assertWithMessage("Result of valueOf is invalid")
90 .that(option)
91 .isEqualTo(PadOption.NOSPACE);
92 }
93
94
95
96
97
98 @Test
99 public void testWrapOptionValueOf() {
100 final WrapOption option = WrapOption.valueOf("EOL");
101 assertWithMessage("Result of valueOf is invalid")
102 .that(option)
103 .isEqualTo(WrapOption.EOL);
104 }
105
106 @Test
107 public void testWithEmoji() throws Exception {
108 final String[] expected = {
109 "23:13: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
110 "28:25: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
111 };
112 verifyWithInlineConfigParser(
113 getPath("InputEmptyForInitializerPadWithEmoji.java"), expected);
114 }
115
116 @Test
117 public void testInvalidOption() throws Exception {
118 final DefaultConfiguration checkConfig =
119 createModuleConfig(EmptyForInitializerPadCheck.class);
120 checkConfig.addProperty("option", "invalid_option");
121
122 try {
123 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
124
125 verifyWithInlineConfigParser(
126 getPath("InputEmptyForInitializerPad2.java"), expected);
127 assertWithMessage("exception expected").fail();
128 }
129 catch (CheckstyleException ex) {
130 assertWithMessage("Invalid exception message")
131 .that(ex.getMessage())
132 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
133 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
134 + "whitespace.EmptyForInitializerPadCheck - "
135 + "Cannot set property 'option' to 'invalid_option'");
136 }
137 }
138
139 @Test
140 public void testTrimOptionProperty() throws Exception {
141 final String[] expected = {
142 "15:14: " + getCheckMessage(MSG_NOT_PRECEDED, ";"),
143 };
144 verifyWithInlineConfigParser(
145 getPath("InputEmptyForInitializerPadSetOptionTrim.java"), expected);
146 }
147 }