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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.WhenShouldBeUsedCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31 public class WhenShouldBeUsedCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/coding/whenshouldbeused";
37 }
38
39 @Test
40 public void testTokensNotNull() {
41 final WhenShouldBeUsedCheck check = new WhenShouldBeUsedCheck();
42 final int[] expected = {TokenTypes.LITERAL_CASE};
43
44 assertWithMessage("Acceptable tokens is not valid")
45 .that(check.getAcceptableTokens())
46 .isEqualTo(expected);
47 assertWithMessage("Default tokens is not valid")
48 .that(check.getDefaultTokens())
49 .isEqualTo(expected);
50 assertWithMessage("Required tokens is not valid")
51 .that(check.getRequiredTokens())
52 .isEqualTo(expected);
53 }
54
55 @Test
56 public void testSwitchStatements() throws Exception {
57 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
58 verifyWithInlineConfigParser(
59 getNonCompilablePath("InputWhenShouldBeUsedSwitchStatements.java"),
60 expected);
61 }
62
63 @Test
64 public void testSwitchRule() throws Exception {
65 final String[] expected = {
66 "13:13: " + getCheckMessage(MSG_KEY),
67 "18:13: " + getCheckMessage(MSG_KEY),
68 "82:13: " + getCheckMessage(MSG_KEY),
69 "96:13: " + getCheckMessage(MSG_KEY),
70 "105:13: " + getCheckMessage(MSG_KEY),
71 };
72 verifyWithInlineConfigParser(
73 getNonCompilablePath("InputWhenShouldBeUsedSwitchRule.java"),
74 expected);
75
76 }
77
78 @Test
79 public void testSwitchExpression() throws Exception {
80 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81 verifyWithInlineConfigParser(
82 getNonCompilablePath("InputWhenShouldBeUsedSwitchExpression.java"),
83 expected);
84 }
85
86 @Test
87 public void testNonPatternsSwitch() throws Exception {
88 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89 verifyWithInlineConfigParser(
90 getNonCompilablePath("InputWhenShouldBeUsedNonPatternsSwitch.java"),
91 expected);
92 }
93 }