1
2
3
4
5 package com.qulice.checkstyle;
6
7 import com.qulice.spi.Environment;
8 import com.qulice.spi.Violation;
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.Collection;
12 import org.cactoos.io.ResourceOf;
13 import org.cactoos.text.FormattedText;
14 import org.cactoos.text.IoCheckedText;
15 import org.cactoos.text.TextOf;
16 import org.hamcrest.MatcherAssert;
17 import org.hamcrest.Matchers;
18 import org.junit.jupiter.api.Test;
19
20
21
22
23
24
25 final class CheckstyleLocalVariableNameTest {
26
27 @Test
28 @SuppressWarnings("unchecked")
29 void allowsOnlyProperlyNamedLocalVariables() throws Exception {
30 final String file = "LocalVariableNames.java";
31 MatcherAssert.assertThat(
32 "Only invalid variables name should be found",
33 this.runValidation(file, false),
34 Matchers.<Iterable<Violation>>allOf(
35 Matchers.iterableWithSize(10),
36 Matchers.not(
37 Matchers.hasItems(
38 new ViolationMatcher("aaa", file),
39 new ViolationMatcher("twelveletter", file),
40 new ViolationMatcher("ise", file),
41 new ViolationMatcher("id", file),
42 new ViolationMatcher("parametername", file)
43 )
44 ),
45 Matchers.hasItems(
46 new ViolationMatcher(
47 "Name 'prolongations' must match pattern", file
48 ),
49 new ViolationMatcher(
50 "Name 'very_long_variable_id' must match pattern", file
51 ),
52 new ViolationMatcher(
53 "Name 'camelCase' must match pattern", file
54 ),
55 new ViolationMatcher(
56 "Name 'it' must match pattern", file
57 ),
58 new ViolationMatcher(
59 "Name 'number1' must match pattern", file
60 ),
61 new ViolationMatcher(
62 "Name 'ex' must match pattern", file
63 ),
64 new ViolationMatcher(
65 "Name 'a' must match pattern", file
66 ),
67 new ViolationMatcher(
68 "Name 'ae' must match pattern", file
69 ),
70 new ViolationMatcher(
71 "Name 'e' must match pattern", file
72 ),
73 new ViolationMatcher(
74 "Name 'it' must match pattern", file
75 )
76 )
77 )
78 );
79 }
80
81 private Collection<Violation> runValidation(final String file,
82 final boolean passes) throws IOException {
83 final Environment.Mock mock = new Environment.Mock();
84 final Environment env = mock.withParam(
85 "license",
86 String.format(
87 "file:%s",
88 new License().savePackageInfo(
89 new File(mock.basedir(), "src/main/java/foo")
90 ).withLines("Hello.")
91 .withEol(String.valueOf('\n')).file()
92 )
93 ).withFile(
94 String.format("src/main/java/foo/%s", file),
95 new IoCheckedText(
96 new TextOf(
97 new ResourceOf(
98 new FormattedText("com/qulice/checkstyle/%s", file)
99 )
100 )
101 ).asString()
102 );
103 final Collection<Violation> results =
104 new CheckstyleValidator(env).validate(env.files(file));
105 MatcherAssert.assertThat(
106 "validation result should match expected state",
107 results.isEmpty(),
108 Matchers.is(passes)
109 );
110 return results;
111 }
112 }