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