1 package com.puppycrawl.tools.checkstyle.checks; 2 3 import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; 4 import com.puppycrawl.tools.checkstyle.DefaultConfiguration; 5 6 public class GenericIllegalRegexpCheckTest 7 extends BaseCheckTestCase 8 { 9 private DefaultConfiguration mCheckConfig; 10 11 public void setUp() 12 { 13 mCheckConfig = createCheckConfig(GenericIllegalRegexpCheck.class); 14 } 15 16 public void testIt() 17 throws Exception 18 { 19 final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; 20 mCheckConfig.addAttribute("format", illegal); 21 final String [] expected = { 22 "69: Line matches the illegal pattern '" + illegal + "'." 23 }; 24 verify(mCheckConfig, getPath("InputSemantic.java"), expected); 25 } 26 27 public void testMessageProperty() 28 throws Exception 29 { 30 final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; 31 final String message = "Bad line :("; 32 mCheckConfig.addAttribute("format", illegal); 33 mCheckConfig.addAttribute("message", message); 34 final String [] expected = { 35 "69: " + message, 36 }; 37 verify(mCheckConfig, getPath("InputSemantic.java"), expected); 38 } 39 40 public void testIgnoreCaseTrue() 41 throws Exception 42 { 43 final String illegal = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("; 44 mCheckConfig.addAttribute("format", illegal); 45 mCheckConfig.addAttribute("ignoreCase", "true"); 46 final String [] expected = { 47 "69: Line matches the illegal pattern '" + illegal + "'." 48 }; 49 verify(mCheckConfig, getPath("InputSemantic.java"), expected); 50 } 51 52 public void testIgnoreCaseFalse() 53 throws Exception 54 { 55 final String illegal = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("; 56 mCheckConfig.addAttribute("format", illegal); 57 mCheckConfig.addAttribute("ignoreCase", "false"); 58 final String [] expected = {}; 59 verify(mCheckConfig, getPath("InputSemantic.java"), expected); 60 } 61 62 public void testIgnoreCommentsCppStyle() 63 throws Exception 64 { 65 final String illegal = "don't use trailing comments"; 67 mCheckConfig.addAttribute("format", illegal); 68 mCheckConfig.addAttribute("ignoreComments", "true"); 69 final String [] expected = { 70 }; 71 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 72 } 73 74 public void testIgnoreCommentsFalseCppStyle() 75 throws Exception 76 { 77 final String illegal = "don't use trailing comments"; 79 mCheckConfig.addAttribute("format", illegal); 80 mCheckConfig.addAttribute("ignoreComments", "false"); 81 final String [] expected = { 82 "2: Line matches the illegal pattern '" + illegal + "'." 83 }; 84 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 85 } 86 87 public void testIgnoreCommentsCStyle() 88 throws Exception 89 { 90 final String illegal = "c-style 1"; 92 mCheckConfig.addAttribute("format", illegal); 93 mCheckConfig.addAttribute("ignoreComments", "true"); 94 final String [] expected = { 95 }; 96 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 97 } 98 99 public void testIgnoreCommentsFalseCStyle() 100 throws Exception 101 { 102 final String illegal = "c-style 1"; 103 mCheckConfig.addAttribute("format", illegal); 104 mCheckConfig.addAttribute("ignoreComments", "false"); 105 final String [] expected = { 106 "17: Line matches the illegal pattern '" + illegal + "'." 107 }; 108 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 109 } 110 111 public void testIgnoreCommentsMultipleCStyle() 112 throws Exception 113 { 114 final String illegal = "c-style 2"; 116 mCheckConfig.addAttribute("format", illegal); 117 mCheckConfig.addAttribute("ignoreComments", "true"); 118 final String [] expected = { 119 }; 120 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 121 } 122 123 public void testIgnoreCommentsMultiLine() 124 throws Exception 125 { 126 final String illegal = "Let's check multi-line comments"; 127 mCheckConfig.addAttribute("format", illegal); 128 mCheckConfig.addAttribute("ignoreComments", "true"); 129 final String [] expected = { 130 }; 131 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 132 } 133 134 public void testIgnoreCommentsInlineStart() 135 throws Exception 136 { 137 final String illegal = "long ms /"; 138 mCheckConfig.addAttribute("format", illegal); 139 mCheckConfig.addAttribute("ignoreComments", "true"); 140 final String [] expected = { 141 }; 142 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 143 } 144 145 public void testIgnoreCommentsInlineEnd() 146 throws Exception 147 { 148 final String illegal = "int z"; 149 mCheckConfig.addAttribute("format", illegal); 150 mCheckConfig.addAttribute("ignoreComments", "true"); 151 final String [] expected = { 152 "20: Line matches the illegal pattern '" + illegal + "'." 153 }; 154 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 155 } 156 157 public void testIgnoreCommentsInlineMiddle() throws Exception 158 { 159 final String illegal = "int y"; 160 mCheckConfig.addAttribute("format", illegal); 161 mCheckConfig.addAttribute("ignoreComments", "true"); 162 final String [] expected = { 163 "21: Line matches the illegal pattern '" + illegal + "'." 164 }; 165 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 166 } 167 168 public void testIgnoreCommentsNoSpaces() 169 throws Exception 170 { 171 final String illegal = "long ms "; 173 mCheckConfig.addAttribute("format", illegal); 174 mCheckConfig.addAttribute("ignoreComments", "true"); 175 final String [] expected = { 176 }; 177 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 178 } 179 180 public void test1371588() 181 throws Exception 182 { 183 final String illegal = "\\s+$"; 185 mCheckConfig.addAttribute("format", illegal); 186 mCheckConfig.addAttribute("ignoreComments", "true"); 187 final String [] expected = { 188 }; 189 verify(mCheckConfig, getPath("InputTrailingComment.java"), expected); 190 } 191 } 192 | Popular Tags |