diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/tools.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/tools.rs b/tests/tools.rs index 3df353f..8463d8d 100644 --- a/tests/tools.rs +++ b/tests/tools.rs | |||
| @@ -201,3 +201,51 @@ fn read_handles_a_missing_trailing_newline() { | |||
| 201 | assert!(text.contains(" 2\ttwo"), "{text}"); | 201 | assert!(text.contains(" 2\ttwo"), "{text}"); |
| 202 | assert!(!text.contains("continue with offset"), "{text}"); | 202 | assert!(!text.contains("continue with offset"), "{text}"); |
| 203 | } | 203 | } |
| 204 | |||
| 205 | #[test] | ||
| 206 | fn grep_uses_regular_expressions_by_default() { | ||
| 207 | let dir = temp_dir("grep-regex"); | ||
| 208 | fs::write( | ||
| 209 | dir.join("f.rs"), | ||
| 210 | "fn main() {\n fn other() {\n}\n", | ||
| 211 | ) | ||
| 212 | .unwrap(); | ||
| 213 | |||
| 214 | let text = tools::run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); | ||
| 215 | assert_eq!(text.lines().count(), 1, "{text}"); | ||
| 216 | |||
| 217 | let text = tools::run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); | ||
| 218 | assert_eq!(text.lines().count(), 2, "{text}"); | ||
| 219 | |||
| 220 | let text = tools::run( | ||
| 221 | "grep", | ||
| 222 | &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }), | ||
| 223 | ) | ||
| 224 | .unwrap(); | ||
| 225 | assert_eq!(text.lines().count(), 1, "{text}"); | ||
| 226 | } | ||
| 227 | |||
| 228 | #[test] | ||
| 229 | fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { | ||
| 230 | let dir = temp_dir("grep-fixed"); | ||
| 231 | fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap(); | ||
| 232 | |||
| 233 | // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b". | ||
| 234 | let text = tools::run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); | ||
| 235 | assert_eq!(text.lines().count(), 1, "{text}"); | ||
| 236 | assert!(text.contains("and ab"), "{text}"); | ||
| 237 | |||
| 238 | let text = tools::run( | ||
| 239 | "grep", | ||
| 240 | &json!({ "pattern": "a+b", "path": dir, "fixed": true }), | ||
| 241 | ) | ||
| 242 | .unwrap(); | ||
| 243 | assert_eq!(text.lines().count(), 1, "{text}"); | ||
| 244 | assert!(text.contains("cost is a+b"), "{text}"); | ||
| 245 | |||
| 246 | let error = tools::run("grep", &json!({ "pattern": "(", "path": dir })) | ||
| 247 | .err() | ||
| 248 | .expect("invalid regex must fail"); | ||
| 249 | assert!(error.contains("invalid regex"), "{error}"); | ||
| 250 | assert!(error.contains("fixed"), "{error}"); | ||
| 251 | } | ||
