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.EmptyForIteratorPadCheck.MSG_WS_FOLLOWED;
24 import static com.puppycrawl.tools.checkstyle.checks.whitespace.EmptyForIteratorPadCheck.MSG_WS_NOT_FOLLOWED;
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.api.CheckstyleException;
31 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33
34 public class EmptyForIteratorPadCheckTest
35 extends AbstractModuleTestSupport {
36
37 @Override
38 public String getPackageLocation() {
39 return "com/puppycrawl/tools/checkstyle/checks/whitespace/emptyforiteratorpad";
40 }
41
42 @Test
43 public void testGetRequiredTokens() {
44 final EmptyForIteratorPadCheck checkObj = new EmptyForIteratorPadCheck();
45 final int[] expected = {TokenTypes.FOR_ITERATOR};
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 "30:32: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
55 "46:33: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
56 "58:12: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
57 };
58 verifyWithInlineConfigParser(
59 getPath("InputEmptyForIteratorPad.java"), expected);
60 }
61
62 @Test
63 public void testSpaceOption() throws Exception {
64 final String[] expected = {
65 "26:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
66 };
67 verifyWithInlineConfigParser(
68 getPath("InputEmptyForIteratorPad1.java"), expected);
69 }
70
71 @Test
72 public void testWithEmoji() throws Exception {
73 final String[] expected = {
74 "24:40: " + getCheckMessage(MSG_WS_FOLLOWED, ";"),
75 };
76 verifyWithInlineConfigParser(
77 getPath("InputEmptyForIteratorPadWithEmoji.java"), expected);
78
79 }
80
81 @Test
82 public void testGetAcceptableTokens() {
83 final EmptyForIteratorPadCheck emptyForIteratorPadCheckObj = new EmptyForIteratorPadCheck();
84 final int[] actual = emptyForIteratorPadCheckObj.getAcceptableTokens();
85 final int[] expected = {
86 TokenTypes.FOR_ITERATOR,
87 };
88 assertWithMessage("Default acceptable tokens are invalid")
89 .that(actual)
90 .isEqualTo(expected);
91 }
92
93 @Test
94 public void testInvalidOption() {
95 final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class, () -> {
96 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
97
98 verifyWithInlineConfigParser(getPath("InputEmptyForIteratorPad2.java"), expected);
99 });
100 assertWithMessage("Invalid exception message")
101 .that(exc.getMessage())
102 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
103 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
104 + "whitespace.EmptyForIteratorPadCheck");
105 }
106
107 @Test
108 public void testTrimOptionProperty() throws Exception {
109 final String[] expected = {
110 "20:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
111 };
112 verifyWithInlineConfigParser(
113 getPath("InputEmptyForIteratorPadToCheckTrimFunctionInOptionProperty.java"),
114 expected);
115
116 }
117
118 @Test
119 public void testUppercaseOptionProperty() throws Exception {
120 final String[] expected = {
121 "20:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
122 };
123 verifyWithInlineConfigParser(
124 getPath("InputEmptyForIteratorPadToCheckUppercaseFunctionInOptionProperty.java"),
125 expected);
126
127 }
128 }