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.api;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
24
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.Locale;
28 import java.util.ResourceBundle;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31
32 import org.junit.jupiter.api.Test;
33
34 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
35
36 public class TokenTypesTest {
37
38 @Test
39 public void testAllTokenTypesHasDescription() {
40 final String tokenTypes = "com.puppycrawl.tools.checkstyle.api.tokentypes";
41 final ResourceBundle bundle = ResourceBundle.getBundle(tokenTypes, Locale.ROOT);
42
43 final Set<String> expected = Arrays.stream(TokenUtil.getAllTokenIds())
44 .mapToObj(TokenUtil::getTokenName)
45 .filter(name -> name.charAt(0) != '$')
46 .collect(Collectors.toUnmodifiableSet());
47 final Set<String> actual = bundle.keySet();
48 assertWithMessage("TokenTypes without description")
49 .that(actual)
50 .isEqualTo(expected);
51 }
52
53 @Test
54 public void testAllDescriptionsEndsWithPeriod() {
55 final Set<String> badDescriptions = Arrays.stream(TokenUtil.getAllTokenIds())
56 .mapToObj(TokenUtil::getTokenName)
57 .filter(name -> name.charAt(0) != '$')
58 .map(TokenUtil::getShortDescription)
59 .filter(desc -> desc.charAt(desc.length() - 1) != '.')
60 .collect(Collectors.toUnmodifiableSet());
61 assertWithMessage("Malformed TokenType descriptions")
62 .that(badDescriptions)
63 .isEqualTo(Collections.emptySet());
64 }
65
66 @Test
67 public void testGetShortDescription() {
68 assertWithMessage("short description for EQUAL")
69 .that(TokenUtil.getShortDescription("EQUAL"))
70 .isEqualTo("The <code>==</code> (equal) operator.");
71
72 assertWithMessage("short description for LAND")
73 .that(TokenUtil.getShortDescription("LAND"))
74 .isEqualTo("The <code>&&</code> (conditional AND) operator.");
75
76 assertWithMessage("short description for LCURLY")
77 .that(TokenUtil.getShortDescription("LCURLY"))
78 .isEqualTo("A left curly brace (<code>{</code>).");
79
80 assertWithMessage("short description for SR_ASSIGN")
81 .that(TokenUtil.getShortDescription("SR_ASSIGN"))
82 .isEqualTo("The <code>>>=</code> (signed right shift assignment) operator.");
83
84 assertWithMessage("short description for SL")
85 .that(TokenUtil.getShortDescription("SL"))
86 .isEqualTo("The <code><<</code> (shift left) operator.");
87
88 assertWithMessage("short description for BSR")
89 .that(TokenUtil.getShortDescription("BSR"))
90 .isEqualTo("The <code>>>></code> (unsigned shift right) operator.");
91 }
92
93 @Test
94 public void testIsProperUtilsClass() throws ReflectiveOperationException {
95 assertWithMessage("Constructor is not private")
96 .that(isUtilsClassHasPrivateConstructor(TokenTypes.class))
97 .isTrue();
98 }
99
100 }