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.SeparatorWrapCheck.MSG_LINE_NEW;
24 import static com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck.MSG_LINE_PREVIOUS;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33 public class SeparatorWrapCheckTest
34 extends AbstractModuleTestSupport {
35
36 @Override
37 public String getPackageLocation() {
38 return "com/puppycrawl/tools/checkstyle/checks/whitespace/separatorwrap";
39 }
40
41 @Test
42 public void testDot()
43 throws Exception {
44 final String[] expected = {
45 "39:10: " + getCheckMessage(MSG_LINE_NEW, "."),
46 };
47 verifyWithInlineConfigParser(
48 getPath("InputSeparatorWrapForTestDot.java"), expected);
49 }
50
51 @Test
52 public void testComma() throws Exception {
53 final String[] expected = {
54 "47:17: " + getCheckMessage(MSG_LINE_PREVIOUS, ","),
55 };
56 verifyWithInlineConfigParser(
57 getPath("InputSeparatorWrapForTestComma.java"), expected);
58 }
59
60 @Test
61 public void testMethodRef() throws Exception {
62 final String[] expected = {
63 "25:56: " + getCheckMessage(MSG_LINE_NEW, "::"),
64 };
65 verifyWithInlineConfigParser(
66 getPath("InputSeparatorWrapForTestMethodRef.java"), expected);
67 }
68
69 @Test
70 public void testGetDefaultTokens() {
71 final SeparatorWrapCheck separatorWrapCheckObj = new SeparatorWrapCheck();
72 final int[] actual = separatorWrapCheckObj.getDefaultTokens();
73 final int[] expected = {
74 TokenTypes.DOT,
75 TokenTypes.COMMA,
76 };
77 assertWithMessage("Invalid default tokens")
78 .that(actual)
79 .isEqualTo(expected);
80 }
81
82 @Test
83 public void testInvalidOption() throws Exception {
84
85 try {
86 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
87
88 verifyWithInlineConfigParser(
89 getPath("InputSeparatorWrapForInvalidOption.java"), expected);
90 assertWithMessage("exception expected").fail();
91 }
92 catch (CheckstyleException exc) {
93 assertWithMessage("Invalid exception message")
94 .that(exc.getMessage())
95 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
96 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
97 + "whitespace.SeparatorWrapCheck");
98 }
99 }
100
101 @Test
102 public void testEllipsis() throws Exception {
103 final String[] expected = {
104 "19:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "..."),
105 };
106 verifyWithInlineConfigParser(
107 getPath("InputSeparatorWrapForEllipsis.java"), expected);
108 }
109
110 @Test
111 public void testArrayDeclarator() throws Exception {
112 final String[] expected = {
113 "17:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "["),
114 };
115 verifyWithInlineConfigParser(
116 getPath("InputSeparatorWrapForArrayDeclarator.java"), expected);
117 }
118
119 @Test
120 public void testWithEmoji() throws Exception {
121 final String[] expected = {
122 "13:39: " + getCheckMessage(MSG_LINE_NEW, '['),
123 "16:57: " + getCheckMessage(MSG_LINE_NEW, '['),
124 "19:39: " + getCheckMessage(MSG_LINE_NEW, "..."),
125 "26:19: " + getCheckMessage(MSG_LINE_NEW, '.'),
126 "39:50: " + getCheckMessage(MSG_LINE_NEW, ','),
127 "41:50: " + getCheckMessage(MSG_LINE_NEW, "::"),
128 };
129 verifyWithInlineConfigParser(
130 getPath("InputSeparatorWrapWithEmoji.java"), expected);
131 }
132
133 @Test
134 public void testTrimOptionProperty() throws Exception {
135 final String[] expected = {
136 "18:44: " + getCheckMessage(MSG_LINE_NEW, "::"),
137 };
138 verifyWithInlineConfigParser(
139 getPath("InputSeparatorWrapSetOptionTrim.java"), expected);
140 }
141
142 @Test
143 public void testCommaOnNewLine() throws Exception {
144 final String[] expected = {
145 "16:10: " + getCheckMessage(MSG_LINE_NEW, ","),
146 "21:26: " + getCheckMessage(MSG_LINE_NEW, ","),
147 };
148 verifyWithInlineConfigParser(
149 getPath("InputSeparatorWrapForTestTrailingWhitespace.java"), expected);
150 }
151 }