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 RegexpCheckTest extends BaseCheckTestCase 7 { 8 public void testRequiredPass() 9 throws Exception 10 { 11 final String required = "Test case file"; 12 final DefaultConfiguration checkConfig = 13 createCheckConfig(RegexpCheck.class); 14 checkConfig.addAttribute("format", required); 15 final String [] expected = { 16 }; 17 verify(checkConfig, getPath("InputSemantic.java"), expected); 18 } 19 20 public void testRequiredFail() 21 throws Exception 22 { 23 final String required = "This text is not in the file"; 24 final DefaultConfiguration checkConfig = 25 createCheckConfig(RegexpCheck.class); 26 checkConfig.addAttribute("format", required); 27 final String [] expected = { 28 "0: Required pattern '" + required + "' missing in file." 29 }; 30 verify(checkConfig, getPath("InputSemantic.java"), expected); 31 } 32 33 public void testRequiredNoDuplicatesPass() 34 throws Exception 35 { 36 final String required = "Test case file"; 37 final DefaultConfiguration checkConfig = 38 createCheckConfig(RegexpCheck.class); 39 checkConfig.addAttribute("format", required); 40 checkConfig.addAttribute("duplicateLimit", "0"); 41 final String [] expected = { 42 }; 43 verify(checkConfig, getPath("InputSemantic.java"), expected); 44 } 45 46 public void testRequiredNoDuplicatesFail() 47 throws Exception 48 { 49 final String required = "Boolean x = new Boolean"; 50 final DefaultConfiguration checkConfig = 51 createCheckConfig(RegexpCheck.class); 52 checkConfig.addAttribute("format", required); 53 checkConfig.addAttribute("duplicateLimit", "0"); 54 final String [] expected = { 55 "24: Found duplicate pattern '" + required + "'." 56 }; 57 verify(checkConfig, getPath("InputSemantic.java"), expected); 58 } 59 60 public void testIllegalPass() 61 throws Exception 62 { 63 final String illegal = "This text is not in the file"; 64 final DefaultConfiguration checkConfig = 65 createCheckConfig(RegexpCheck.class); 66 checkConfig.addAttribute("format", illegal); 67 checkConfig.addAttribute("illegalPattern", "true"); 68 final String [] expected = { 69 }; 70 verify(checkConfig, getPath("InputSemantic.java"), expected); 71 } 72 73 public void testIllegalFailBelowErrorLimit() 74 throws Exception 75 { 76 final String illegal = "^import"; 77 final DefaultConfiguration checkConfig = 78 createCheckConfig(RegexpCheck.class); 79 checkConfig.addAttribute("format", illegal); 80 checkConfig.addAttribute("illegalPattern", "true"); 81 checkConfig.addAttribute("errorLimit", "4"); 82 final String [] expected = { 83 "7: Line matches the illegal pattern '" + illegal + "'.", 84 "8: Line matches the illegal pattern '" + illegal + "'.", 85 "9: Line matches the illegal pattern '" + illegal + "'." 86 }; 87 verify(checkConfig, getPath("InputSemantic.java"), expected); 88 } 89 90 public void testIllegalFailAboveErrorLimit() 91 throws Exception 92 { 93 final String illegal = "^import"; 94 final String error = "The error limit has been exceeded, " 95 + "the check is aborting, there may be more unreported errors."; 96 final DefaultConfiguration checkConfig = 97 createCheckConfig(RegexpCheck.class); 98 checkConfig.addAttribute("format", illegal); 99 checkConfig.addAttribute("illegalPattern", "true"); 100 checkConfig.addAttribute("errorLimit", "3"); 101 final String [] expected = { 102 "7: Line matches the illegal pattern '" + illegal + "'.", 103 "8: Line matches the illegal pattern '" + illegal + "'.", 104 "9: Line matches the illegal pattern '" + error + illegal + "'." 105 }; 106 verify(checkConfig, getPath("InputSemantic.java"), expected); 107 } 108 109 public void testMessagePropertyGood() 110 throws Exception 111 { 112 final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; 113 final String message = "Bad line :("; 114 final DefaultConfiguration checkConfig = 115 createCheckConfig(RegexpCheck.class); 116 checkConfig.addAttribute("format", illegal); 117 checkConfig.addAttribute("illegalPattern", "true"); 118 checkConfig.addAttribute("message", message); 119 final String [] expected = { 120 "69: Line matches the illegal pattern '" + message + "'." 121 }; 122 verify(checkConfig, getPath("InputSemantic.java"), expected); 123 } 124 125 public void testMessagePropertyBad() 126 throws Exception 127 { 128 final String illegal = "System\\.(out)|(err)\\.print(ln)?\\("; 129 final DefaultConfiguration checkConfig = 130 createCheckConfig(RegexpCheck.class); 131 checkConfig.addAttribute("format", illegal); 132 checkConfig.addAttribute("illegalPattern", "true"); 133 checkConfig.addAttribute("message", null); 134 final String [] expected = { 135 "69: Line matches the illegal pattern '" + illegal + "'." 136 }; 137 verify(checkConfig, getPath("InputSemantic.java"), expected); 138 } 139 140 public void testIgnoreCaseTrue() 141 throws Exception 142 { 143 final String illegal = "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("; 144 final DefaultConfiguration checkConfig = 145 createCheckConfig(RegexpCheck.class); 146 checkConfig.addAttribute("format", illegal); 147 checkConfig.addAttribute("illegalPattern", "true"); 148 final String [] expected = { 149 "69: Line matches the illegal pattern '" + illegal + "'." 150 }; 151 verify(checkConfig, getPath("InputSemantic.java"), expected); 152 } 153 154 public void testIgnoreCaseFalse() 155 throws Exception 156 { 157 final String illegalTrue = "(?i)SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("; 158 final DefaultConfiguration checkConfigTrue = 159 createCheckConfig(RegexpCheck.class); 160 checkConfigTrue.addAttribute("format", illegalTrue); 161 checkConfigTrue.addAttribute("illegalPattern", "true"); 162 final String [] expectedTrue = { 163 "69: Line matches the illegal pattern '" + illegalTrue + "'."}; 164 verify(checkConfigTrue, getPath("InputSemantic.java"), expectedTrue); 165 166 final String illegalFalse = "SYSTEM\\.(OUT)|(ERR)\\.PRINT(LN)?\\("; 167 final DefaultConfiguration checkConfigFalse = 168 createCheckConfig(RegexpCheck.class); 169 checkConfigFalse.addAttribute("format", illegalFalse); 170 checkConfigFalse.addAttribute("illegalPattern", "true"); 171 final String [] expectedFalse = {}; 172 verify(checkConfigFalse, getPath("InputSemantic.java"), expectedFalse); 173 } 174 175 public void testIgnoreCommentsCppStyle() 176 throws Exception 177 { 178 final String illegal = "don't use trailing comments"; 180 final DefaultConfiguration checkConfig = 181 createCheckConfig(RegexpCheck.class); 182 checkConfig.addAttribute("format", illegal); 183 checkConfig.addAttribute("illegalPattern", "true"); 184 checkConfig.addAttribute("ignoreComments", "true"); 185 final String [] expected = { 186 }; 187 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 188 } 189 190 public void testIgnoreCommentsFalseCppStyle() 191 throws Exception 192 { 193 final String illegal = "don't use trailing comments"; 195 final DefaultConfiguration checkConfig = 196 createCheckConfig(RegexpCheck.class); 197 checkConfig.addAttribute("format", illegal); 198 checkConfig.addAttribute("illegalPattern", "true"); 199 checkConfig.addAttribute("ignoreComments", "false"); 200 final String [] expected = { 201 "2: Line matches the illegal pattern '" + illegal + "'." 202 }; 203 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 204 } 205 206 public void testIgnoreCommentsCStyle() 207 throws Exception 208 { 209 final String illegal = "c-style 1"; 211 final DefaultConfiguration checkConfig = 212 createCheckConfig(RegexpCheck.class); 213 checkConfig.addAttribute("format", illegal); 214 checkConfig.addAttribute("illegalPattern", "true"); 215 checkConfig.addAttribute("ignoreComments", "true"); 216 final String [] expected = { 217 }; 218 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 219 } 220 221 public void testIgnoreCommentsFalseCStyle() 222 throws Exception 223 { 224 final String illegal = "c-style 1"; 225 final DefaultConfiguration checkConfig = 226 createCheckConfig(RegexpCheck.class); 227 checkConfig.addAttribute("format", illegal); 228 checkConfig.addAttribute("illegalPattern", "true"); 229 checkConfig.addAttribute("ignoreComments", "false"); 230 final String [] expected = { 231 "17: Line matches the illegal pattern '" + illegal + "'." 232 }; 233 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 234 } 235 236 public void testIgnoreCommentsMultipleCStyle() 237 throws Exception 238 { 239 final String illegal = "c-style 2"; 241 final DefaultConfiguration checkConfig = 242 createCheckConfig(RegexpCheck.class); 243 checkConfig.addAttribute("format", illegal); 244 checkConfig.addAttribute("illegalPattern", "true"); 245 checkConfig.addAttribute("ignoreComments", "true"); 246 final String [] expected = { 247 }; 248 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 249 } 250 251 public void testIgnoreCommentsMultiLine() 252 throws Exception 253 { 254 final String illegal = "Let's check multi-line comments"; 255 final DefaultConfiguration checkConfig = 256 createCheckConfig(RegexpCheck.class); 257 checkConfig.addAttribute("format", illegal); 258 checkConfig.addAttribute("illegalPattern", "true"); 259 checkConfig.addAttribute("ignoreComments", "true"); 260 final String [] expected = { 261 }; 262 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 263 } 264 265 public void testIgnoreCommentsInlineStart() 266 throws Exception 267 { 268 final String illegal = "long ms /"; 269 final DefaultConfiguration checkConfig = 270 createCheckConfig(RegexpCheck.class); 271 checkConfig.addAttribute("format", illegal); 272 checkConfig.addAttribute("illegalPattern", "true"); 273 checkConfig.addAttribute("ignoreComments", "true"); 274 final String [] expected = { 275 }; 276 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 277 } 278 279 public void testIgnoreCommentsInlineEnd() 280 throws Exception 281 { 282 final String illegal = "int z"; 283 final DefaultConfiguration checkConfig = 284 createCheckConfig(RegexpCheck.class); 285 checkConfig.addAttribute("format", illegal); 286 checkConfig.addAttribute("illegalPattern", "true"); 287 checkConfig.addAttribute("ignoreComments", "true"); 288 final String [] expected = { 289 "20: Line matches the illegal pattern '" + illegal + "'." 290 }; 291 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 292 } 293 294 public void testIgnoreCommentsInlineMiddle() 295 throws Exception 296 { 297 final String illegal = "int y"; 298 final DefaultConfiguration checkConfig = 299 createCheckConfig(RegexpCheck.class); 300 checkConfig.addAttribute("format", illegal); 301 checkConfig.addAttribute("illegalPattern", "true"); 302 checkConfig.addAttribute("ignoreComments", "true"); 303 final String [] expected = { 304 "21: Line matches the illegal pattern '" + illegal + "'." 305 }; 306 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 307 } 308 309 public void testIgnoreCommentsNoSpaces() 310 throws Exception 311 { 312 final String illegal = "long ms "; 314 final DefaultConfiguration checkConfig = 315 createCheckConfig(RegexpCheck.class); 316 checkConfig.addAttribute("format", illegal); 317 checkConfig.addAttribute("illegalPattern", "true"); 318 checkConfig.addAttribute("ignoreComments", "true"); 319 final String [] expected = { 320 }; 321 verify(checkConfig, getPath("InputTrailingComment.java"), expected); 322 } 323 } 324 | Popular Tags |