perl-corpus 0.12.2

Test corpus management and generators for Perl parsers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Glob expression test fixtures for file pattern matching and diamond operator.

/// A glob expression test fixture with metadata.
#[derive(Debug, Clone, Copy)]
pub struct GlobExpressionCase {
    /// Stable identifier for the fixture.
    pub id: &'static str,
    /// Short human-readable description.
    pub description: &'static str,
    /// Tags for filtering and grouping.
    pub tags: &'static [&'static str],
    /// Perl source for the glob expression.
    pub source: &'static str,
}

static GLOB_EXPRESSION_CASES: &[GlobExpressionCase] = &[
    GlobExpressionCase {
        id: "glob.function.basic",
        description: "Basic glob() function with simple wildcard.",
        tags: &["glob", "file", "builtin"],
        source: r#"my @files = glob("*.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.function.star",
        description: "Glob function with star wildcard.",
        tags: &["glob", "file", "wildcard"],
        source: r#"my @perl_files = glob("*.pl");
my @pm_files = glob("*.pm");
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.basic",
        description: "Diamond operator with simple pattern.",
        tags: &["glob", "diamond", "file"],
        source: r#"my @files = <*.pl>;
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.multiple",
        description: "Multiple diamond glob expressions.",
        tags: &["glob", "diamond", "file"],
        source: r#"my @files = <*.pl>;
my @more = <*.pm>;
my @libs = <lib/*.pm>;
"#,
    },
    GlobExpressionCase {
        id: "glob.wildcard.question",
        description: "Glob with question mark wildcard matching single character.",
        tags: &["glob", "file", "wildcard"],
        source: r#"my @files = glob("file?.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.wildcard.brackets",
        description: "Glob with character class brackets.",
        tags: &["glob", "file", "wildcard", "character-class"],
        source: r#"my @files = glob("file[abc].txt");
my @numbered = glob("test[0-9].pl");
"#,
    },
    GlobExpressionCase {
        id: "glob.wildcard.brackets.negated",
        description: "Glob with negated character class.",
        tags: &["glob", "file", "wildcard", "character-class"],
        source: r#"my @files = glob("file[!0-9].txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.brace.expansion",
        description: "Glob with brace expansion for alternatives.",
        tags: &["glob", "file", "brace-expansion"],
        source: r#"my @files = glob("{foo,bar,baz}.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.brace.nested",
        description: "Glob with nested brace expansion.",
        tags: &["glob", "file", "brace-expansion"],
        source: r#"my @files = glob("file{1,{2,3}}.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.directory.pattern",
        description: "Glob with directory path pattern.",
        tags: &["glob", "file", "directory"],
        source: r#"my @files = glob("lib/*.pm");
my @tests = glob("t/*.t");
"#,
    },
    GlobExpressionCase {
        id: "glob.directory.nested",
        description: "Glob with nested directory patterns.",
        tags: &["glob", "file", "directory"],
        source: r#"my @files = glob("lib/Foo/*.pm");
my @more = glob("lib/*/Helper.pm");
"#,
    },
    GlobExpressionCase {
        id: "glob.recursive.doublestar",
        description: "Glob with recursive doublestar pattern (system-dependent).",
        tags: &["glob", "file", "recursive"],
        source: r#"use File::Glob ':bsd_glob';
my @files = bsd_glob("lib/**/*.pm", GLOB_BRACE);
"#,
    },
    GlobExpressionCase {
        id: "glob.list.context",
        description: "Glob in list context returning multiple files.",
        tags: &["glob", "file", "list-context"],
        source: r#"my @all = glob("*.{pl,pm,t}");
for my $file (@all) {
    print "$file\n";
}
"#,
    },
    GlobExpressionCase {
        id: "glob.scalar.context",
        description: "Glob in scalar context returning one result at a time.",
        tags: &["glob", "file", "scalar-context"],
        source: r#"while (my $file = glob("*.txt")) {
    print "$file\n";
}
"#,
    },
    GlobExpressionCase {
        id: "glob.variable.pattern",
        description: "Glob with pattern from variable.",
        tags: &["glob", "file", "variable"],
        source: r#"my $pattern = "*.pl";
my @files = glob($pattern);
"#,
    },
    GlobExpressionCase {
        id: "glob.variable.interpolation",
        description: "Glob with interpolated variable in pattern.",
        tags: &["glob", "file", "variable", "interpolation"],
        source: r#"my $ext = "txt";
my @files = glob("*.$ext");
my $dir = "lib";
my @modules = glob("$dir/*.pm");
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.variable",
        description: "Diamond operator with variable interpolation.",
        tags: &["glob", "diamond", "file", "variable"],
        source: r#"my $pattern = "*.pl";
my @files = <$pattern>;
"#,
    },
    GlobExpressionCase {
        id: "glob.combined.wildcards",
        description: "Glob combining multiple wildcard types.",
        tags: &["glob", "file", "wildcard"],
        source: r#"my @files = glob("test?[0-9]*.{pl,pm}");
"#,
    },
    GlobExpressionCase {
        id: "glob.absolute.path",
        description: "Glob with absolute path pattern.",
        tags: &["glob", "file", "path"],
        source: r#"my @files = glob("/tmp/*.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.tilde.expansion",
        description: "Glob with tilde home directory expansion.",
        tags: &["glob", "file", "path"],
        source: r#"my @files = glob("~/*.txt");
my @config = glob("~/.config/*");
"#,
    },
    GlobExpressionCase {
        id: "glob.empty.result",
        description: "Glob pattern that may return empty list.",
        tags: &["glob", "file", "edge-case"],
        source: r#"my @files = glob("nonexistent*.txt");
if (@files) {
    print "found files\n";
} else {
    print "no matches\n";
}
"#,
    },
    GlobExpressionCase {
        id: "glob.special.chars.escaped",
        description: "Glob with escaped special characters.",
        tags: &["glob", "file", "escape"],
        source: r#"my @files = glob("file\\*.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.file.glob.module",
        description: "File::Glob module with explicit flags.",
        tags: &["glob", "file", "module"],
        source: r#"use File::Glob ':glob';
my @files = bsd_glob("*.txt", GLOB_NOCHECK);
"#,
    },
    GlobExpressionCase {
        id: "glob.file.glob.brace",
        description: "File::Glob with brace expansion enabled.",
        tags: &["glob", "file", "module", "brace-expansion"],
        source: r#"use File::Glob qw(:globally :nocase);
my @files = glob("{Foo,Bar}/*.pm");
"#,
    },
    GlobExpressionCase {
        id: "glob.for.loop",
        description: "Glob in for loop iteration.",
        tags: &["glob", "file", "loop"],
        source: r#"for my $file (glob("*.pl")) {
    print "processing $file\n";
}
"#,
    },
    GlobExpressionCase {
        id: "glob.map.grep",
        description: "Glob with map and grep filtering.",
        tags: &["glob", "file", "map", "grep"],
        source: r#"my @files = glob("*.pl");
my @filtered = grep { -f $_ } @files;
my @names = map { s/\.pl$//r } @filtered;
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.stdin",
        description: "Diamond operator reading from stdin or files.",
        tags: &["glob", "diamond", "io", "stdin"],
        source: r#"while (my $line = <>) {
    print $line;
}
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.filehandle",
        description: "Diamond operator with filehandle.",
        tags: &["glob", "diamond", "io", "filehandle"],
        source: r#"open my $fh, "<", "file.txt" or die $!;
while (my $line = <$fh>) {
    print $line;
}
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.data",
        description: "Diamond operator reading from DATA section.",
        tags: &["glob", "diamond", "io", "data"],
        source: r#"while (my $line = <DATA>) {
    print $line;
}
__DATA__
line 1
line 2
"#,
    },
    GlobExpressionCase {
        id: "glob.function.join",
        description: "Glob function with joined multiple patterns.",
        tags: &["glob", "file"],
        source: r#"my @files = glob("*.pl *.pm *.t");
"#,
    },
    GlobExpressionCase {
        id: "glob.hidden.files",
        description: "Glob pattern matching hidden files (dotfiles).",
        tags: &["glob", "file", "hidden"],
        source: r#"my @hidden = glob(".*");
my @config = glob(".config*");
"#,
    },
    GlobExpressionCase {
        id: "glob.extension.multiple",
        description: "Glob matching multiple file extensions.",
        tags: &["glob", "file", "brace-expansion"],
        source: r#"my @source = glob("*.{c,h,cpp,hpp}");
my @perl = glob("*.{pl,pm,pod,t}");
"#,
    },
    GlobExpressionCase {
        id: "glob.directory.wildcard",
        description: "Glob with wildcard in directory component.",
        tags: &["glob", "file", "directory", "wildcard"],
        source: r#"my @files = glob("lib/*/Config.pm");
my @all = glob("*/t/*.t");
"#,
    },
    GlobExpressionCase {
        id: "glob.range.expansion",
        description: "Glob with numeric range in brace expansion.",
        tags: &["glob", "file", "brace-expansion"],
        source: r#"my @files = glob("file{1..10}.txt");
"#,
    },
    GlobExpressionCase {
        id: "glob.assignment.scalar",
        description: "Glob assignment to scalar variable.",
        tags: &["glob", "file", "scalar-context"],
        source: r#"my $first = glob("*.txt");
print "$first\n" if defined $first;
"#,
    },
    GlobExpressionCase {
        id: "glob.sort.results",
        description: "Sorting glob results.",
        tags: &["glob", "file", "sort"],
        source: r#"my @files = sort glob("*.pl");
my @reversed = reverse sort glob("*.pm");
"#,
    },
    GlobExpressionCase {
        id: "glob.filetest.filter",
        description: "Glob with filetest operator filtering.",
        tags: &["glob", "file", "filetest"],
        source: r#"my @readable = grep { -r $_ } glob("*.txt");
my @writable = grep { -w $_ } glob("*.log");
my @executable = grep { -x $_ } glob("*.pl");
"#,
    },
    GlobExpressionCase {
        id: "glob.quote.meta",
        description: "Glob with quotemeta for literal special chars.",
        tags: &["glob", "file", "escape"],
        source: r#"my $literal = "file[1].txt";
my @files = glob(quotemeta($literal));
"#,
    },
    GlobExpressionCase {
        id: "glob.error.handling",
        description: "Glob with error handling and File::Glob constants.",
        tags: &["glob", "file", "error", "module"],
        source: r#"use File::Glob ':globally';
my @files = glob("*.txt");
if (GLOB_ERROR) {
    warn "Glob error occurred\n";
}
"#,
    },
    GlobExpressionCase {
        id: "glob.function.bare",
        description: "Bare glob function without parentheses.",
        tags: &["glob", "file", "builtin"],
        source: r#"my @files = glob "*.pl";
my @modules = glob "*.pm";
"#,
    },
    GlobExpressionCase {
        id: "glob.function.multi.pattern",
        description: "Glob with space-separated multiple patterns.",
        tags: &["glob", "file", "builtin"],
        source: r#"my @files = glob "*.pl *.pm";
my @all = glob("*.pl *.pm *.t");
"#,
    },
    GlobExpressionCase {
        id: "glob.diamond.dir.variable",
        description: "Diamond operator with directory variable and path.",
        tags: &["glob", "diamond", "file", "variable", "interpolation"],
        source: r#"my $dir = "/usr/lib";
my @files = <$dir/*.pl>;
"#,
    },
    GlobExpressionCase {
        id: "glob.readline.stdin",
        description: "Readline from STDIN (not glob).",
        tags: &["readline", "io", "disambiguation"],
        source: r#"while (<STDIN>) {
    chomp;
    print "Got: $_\n";
}
"#,
    },
    GlobExpressionCase {
        id: "glob.readline.filehandle",
        description: "Readline from lexical filehandle (not glob).",
        tags: &["readline", "io", "disambiguation", "filehandle"],
        source: r#"open my $fh, "<", "file.txt" or die $!;
while (<$fh>) {
    print;
}
"#,
    },
    GlobExpressionCase {
        id: "glob.readline.named.handle",
        description: "Readline from named filehandle (not glob).",
        tags: &["readline", "io", "disambiguation", "filehandle"],
        source: r#"open FH, "<", "file.txt" or die $!;
while (<FH>) {
    print;
}
close FH;
"#,
    },
    GlobExpressionCase {
        id: "glob.disambiguation.mixed",
        description: "Glob vs readline disambiguation in same scope.",
        tags: &["glob", "readline", "disambiguation"],
        source: r#"while (<STDIN>) { chomp; }
my @f = <*.txt>;
open my $fh, "<", "data.txt" or die $!;
while (<$fh>) { print; }
"#,
    },
];

/// Return the static glob expression fixtures.
pub fn glob_expression_cases() -> &'static [GlobExpressionCase] {
    GLOB_EXPRESSION_CASES
}

/// Find a glob expression fixture by ID.
pub fn find_glob_case(id: &str) -> Option<&'static GlobExpressionCase> {
    glob_expression_cases().iter().find(|case| case.id == id)
}

/// Convenience helper for working with glob expression fixtures.
pub struct GlobExpressionGenerator;

impl GlobExpressionGenerator {
    /// Return all available glob expression cases.
    pub fn all_cases() -> &'static [GlobExpressionCase] {
        glob_expression_cases()
    }

    /// Return glob expression cases with a matching tag.
    pub fn by_tag(tag: &str) -> Vec<&'static GlobExpressionCase> {
        glob_expression_cases().iter().filter(|case| case.tags.contains(&tag)).collect()
    }

    /// Return glob expression cases that match any of the provided tags.
    pub fn by_tags_any(tags: &[&str]) -> Vec<&'static GlobExpressionCase> {
        if tags.is_empty() {
            return glob_expression_cases().iter().collect();
        }

        glob_expression_cases()
            .iter()
            .filter(|case| case.tags.iter().any(|tag| tags.contains(tag)))
            .collect()
    }

    /// Return glob expression cases that match all of the provided tags.
    pub fn by_tags_all(tags: &[&str]) -> Vec<&'static GlobExpressionCase> {
        if tags.is_empty() {
            return glob_expression_cases().iter().collect();
        }

        glob_expression_cases()
            .iter()
            .filter(|case| tags.iter().all(|tag| case.tags.iter().any(|t| t == tag)))
            .collect()
    }

    /// Find a single glob expression case by ID.
    pub fn find(id: &str) -> Option<&'static GlobExpressionCase> {
        find_glob_case(id)
    }

    /// Sample a deterministic glob expression case by seed.
    pub fn sample(seed: u64) -> Option<&'static GlobExpressionCase> {
        let cases = glob_expression_cases();
        if cases.is_empty() {
            return None;
        }
        let idx = (seed % cases.len() as u64) as usize;
        cases.get(idx)
    }

    /// Sample a deterministic glob expression case by tag.
    pub fn sample_by_tag(tag: &str, seed: u64) -> Option<&'static GlobExpressionCase> {
        let matches = Self::by_tag(tag);
        if matches.is_empty() {
            return None;
        }
        let idx = (seed % matches.len() as u64) as usize;
        matches.get(idx).copied()
    }

    /// Return sorted unique glob expression tags.
    pub fn tags() -> Vec<&'static str> {
        let mut tags: Vec<&'static str> =
            glob_expression_cases().iter().flat_map(|case| case.tags.iter().copied()).collect();
        tags.sort();
        tags.dedup();
        tags
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use perl_tdd_support::must_some;
    use std::collections::HashSet;

    #[test]
    fn glob_cases_have_ids() {
        assert!(glob_expression_cases().iter().all(|case| !case.id.is_empty()));
    }

    #[test]
    fn glob_cases_have_descriptions() {
        assert!(glob_expression_cases().iter().all(|case| !case.description.is_empty()));
    }

    #[test]
    fn glob_cases_have_source() {
        assert!(glob_expression_cases().iter().all(|case| !case.source.is_empty()));
    }

    #[test]
    fn glob_case_ids_are_unique() {
        let mut seen = HashSet::new();
        for case in glob_expression_cases() {
            assert!(seen.insert(case.id), "Duplicate glob case id: {}", case.id);
        }
    }

    #[test]
    fn glob_cases_can_filter_by_tag() {
        let diamond = GlobExpressionGenerator::by_tag("diamond");
        assert!(!diamond.is_empty());
        assert!(diamond.iter().all(|case| case.tags.contains(&"diamond")));
    }

    #[test]
    fn glob_cases_can_filter_by_any_tag() {
        let matches = GlobExpressionGenerator::by_tags_any(&["wildcard", "brace-expansion"]);
        assert!(!matches.is_empty());
    }

    #[test]
    fn glob_cases_can_filter_by_all_tags() {
        let matches = GlobExpressionGenerator::by_tags_all(&["glob", "file"]);
        assert!(!matches.is_empty());
        assert!(
            matches
                .iter()
                .all(|case| { case.tags.contains(&"glob") && case.tags.contains(&"file") })
        );
    }

    #[test]
    fn glob_case_lookup_by_id() {
        let case = find_glob_case("glob.function.basic");
        must_some(case);
        assert_eq!(must_some(case).id, "glob.function.basic");
    }

    #[test]
    fn glob_case_sample_is_stable() {
        let first = GlobExpressionGenerator::sample(42);
        let second = GlobExpressionGenerator::sample(42);
        assert_eq!(must_some(first).id, must_some(second).id);
    }

    #[test]
    fn glob_case_sample_by_tag_matches() {
        let case = GlobExpressionGenerator::sample_by_tag("diamond", 7);
        must_some(case);
        assert!(must_some(case).tags.contains(&"diamond"));
    }

    #[test]
    fn glob_case_tags_are_unique() {
        let tags = GlobExpressionGenerator::tags();
        let mut deduped = tags.clone();
        deduped.sort();
        deduped.dedup();
        assert_eq!(tags, deduped);
    }

    #[test]
    fn glob_cases_cover_basic_patterns() {
        assert!(find_glob_case("glob.function.basic").is_some());
        assert!(find_glob_case("glob.diamond.basic").is_some());
        assert!(find_glob_case("glob.wildcard.question").is_some());
        assert!(find_glob_case("glob.wildcard.brackets").is_some());
        assert!(find_glob_case("glob.brace.expansion").is_some());
    }

    #[test]
    fn glob_cases_cover_advanced_patterns() {
        assert!(find_glob_case("glob.directory.pattern").is_some());
        assert!(find_glob_case("glob.variable.interpolation").is_some());
        assert!(find_glob_case("glob.list.context").is_some());
        assert!(find_glob_case("glob.scalar.context").is_some());
    }

    #[test]
    fn glob_cases_cover_disambiguation() {
        assert!(find_glob_case("glob.function.bare").is_some());
        assert!(find_glob_case("glob.function.multi.pattern").is_some());
        assert!(find_glob_case("glob.diamond.dir.variable").is_some());
        assert!(find_glob_case("glob.readline.stdin").is_some());
        assert!(find_glob_case("glob.readline.filehandle").is_some());
        assert!(find_glob_case("glob.readline.named.handle").is_some());
        assert!(find_glob_case("glob.disambiguation.mixed").is_some());
    }

    #[test]
    fn glob_cases_have_disambiguation_tag() {
        let cases = GlobExpressionGenerator::by_tag("disambiguation");
        assert!(cases.len() >= 4, "Expected at least 4 disambiguation cases, got {}", cases.len());
    }

    #[test]
    fn glob_generator_all_cases_returns_all() {
        assert_eq!(GlobExpressionGenerator::all_cases().len(), glob_expression_cases().len());
    }

    #[test]
    fn glob_generator_find_works() {
        let case = GlobExpressionGenerator::find("glob.diamond.variable");
        must_some(case);
        assert_eq!(must_some(case).id, "glob.diamond.variable");
    }
}