perl-corpus 0.13.3

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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Format statement test fixtures for Perl LSP corpus.
//!
//! Provides comprehensive test cases for Perl's format/formline feature,
//! covering picture lines, field holders, and write() operations.

/// A format statement test fixture.
#[derive(Debug, Clone, Copy)]
pub struct FormatStatementCase {
    /// 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 format statement.
    pub source: &'static str,
}

static FORMAT_STATEMENT_CASES: &[FormatStatementCase] = &[
    FormatStatementCase {
        id: "format.basic.declaration",
        description: "Basic format declaration with name.",
        tags: &["format", "declaration", "basic"],
        source: r#"format MYFORMAT =
@<<<<<<<<<<<
$text
.
"#,
    },
    FormatStatementCase {
        id: "format.stdout",
        description: "Format declaration for STDOUT filehandle.",
        tags: &["format", "stdout", "filehandle"],
        source: r#"my ($name, $age) = ("Ada", 37);
format STDOUT =
Name: @<<<<<<  Age: @>>
$name,       $age
.
write;
"#,
    },
    FormatStatementCase {
        id: "format.picture.left",
        description: "Format with left-justified picture lines.",
        tags: &["format", "picture", "alignment"],
        source: r#"my $text = "hello";
format REPORT =
@<<<<<<<<
$text
.
"#,
    },
    FormatStatementCase {
        id: "format.picture.right",
        description: "Format with right-justified picture lines.",
        tags: &["format", "picture", "alignment"],
        source: r#"my $value = 12345;
format NUMBERS =
@>>>>>>>>>
$value
.
"#,
    },
    FormatStatementCase {
        id: "format.picture.center",
        description: "Format with centered picture lines.",
        tags: &["format", "picture", "alignment"],
        source: r#"my $title = "Report";
format HEADER =
@|||||||||||
$title
.
"#,
    },
    FormatStatementCase {
        id: "format.mixed.alignment",
        description: "Format with mixed alignment picture lines.",
        tags: &["format", "picture", "alignment", "complex"],
        source: r#"my ($left, $center, $right) = ("L", "C", "R");
format MIXED =
@<<<<<  @|||||  @>>>>>
$left,  $center, $right
.
"#,
    },
    FormatStatementCase {
        id: "format.numeric.field",
        description: "Format with numeric field picture.",
        tags: &["format", "picture", "numeric"],
        source: r#"my $amount = 1234.56;
format MONEY =
$@##.##
$amount
.
"#,
    },
    FormatStatementCase {
        id: "format.multiline",
        description: "Format with multiple line specifications.",
        tags: &["format", "multiline", "complex"],
        source: r#"my ($name, $address, $city) = ("Ada Lovelace", "123 Main St", "London");
format ADDRESS =
Name:    @<<<<<<<<<<<<<<
         $name
Address: @<<<<<<<<<<<<<<
         $address
City:    @<<<<<<<<<<<<<<
         $city
.
"#,
    },
    FormatStatementCase {
        id: "format.multivalue.line",
        description: "Format with multiple values on a single line.",
        tags: &["format", "multivalue"],
        source: r#"my ($a, $b, $c) = (1, 2, 3);
format DATA =
@### @### @###
$a,  $b,  $c
.
"#,
    },
    FormatStatementCase {
        id: "format.text.block",
        description: "Format with text block field holder.",
        tags: &["format", "text-block", "multiline"],
        source: r#"my $paragraph = "This is a long paragraph that should be formatted across multiple lines.";
format PARAGRAPH =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$paragraph
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$paragraph
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$paragraph
.
"#,
    },
    FormatStatementCase {
        id: "format.suppressed.blank",
        description: "Format with suppressed blank lines.",
        tags: &["format", "text-block", "suppressed"],
        source: r#"my $text = "Short text";
format SUPPRESS =
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   $text
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   $text
.
"#,
    },
    FormatStatementCase {
        id: "format.write.call",
        description: "Format declaration with write() call.",
        tags: &["format", "write", "execution"],
        source: r#"my $data = "test data";
format OUTPUT =
Data: @<<<<<<<<
      $data
.
write OUTPUT;
"#,
    },
    FormatStatementCase {
        id: "format.filehandle.association",
        description: "Format associated with a filehandle.",
        tags: &["format", "filehandle", "association"],
        source: r#"open my $fh, ">", "output.txt";
my $value = 42;
format $fh =
Result: @>>>
        $value
.
write $fh;
close $fh;
"#,
    },
    FormatStatementCase {
        id: "format.computed.field",
        description: "Format with computed field values.",
        tags: &["format", "computed", "expression"],
        source: r#"my ($x, $y) = (10, 20);
format COMPUTED =
Sum: @>>>
     $x + $y
.
"#,
    },
    FormatStatementCase {
        id: "format.variable.interpolation",
        description: "Format with variable interpolation in fields.",
        tags: &["format", "interpolation", "variable"],
        source: r#"my ($first, $last) = ("Ada", "Lovelace");
format FULLNAME =
@<<<<<< @<<<<<<<<
$first, $last
.
"#,
    },
    FormatStatementCase {
        id: "format.literal.text",
        description: "Format with literal text and field holders.",
        tags: &["format", "literal", "mixed"],
        source: r#"my $count = 5;
format REPORT =
Total items: @>>>
             $count
.
"#,
    },
    FormatStatementCase {
        id: "format.formline.builtin",
        description: "Formline builtin with accumulator and picture.",
        tags: &["format", "formline", "builtin"],
        source: r#"my $picture = "@<<<<<<";
my $value = "test";
formline $picture, $value;
my $formatted = $^A;
$^A = "";
"#,
    },
    FormatStatementCase {
        id: "format.accumulator.variable",
        description: "Format accumulator variable manipulation.",
        tags: &["format", "accumulator", "special-var"],
        source: r#"$^A = "";
formline "@>>", 42;
my $result = $^A;
$^A = "";
"#,
    },
    FormatStatementCase {
        id: "format.top.of.page",
        description: "Format with top-of-page header format.",
        tags: &["format", "header", "top"],
        source: r#"format STDOUT_TOP =
Page Header
-----------
.
format STDOUT =
@<<<<<
$data
.
my $data = "content";
write;
"#,
    },
    FormatStatementCase {
        id: "format.page.length",
        description: "Format with page length control.",
        tags: &["format", "page", "length", "special-var"],
        source: r#"$= = 60;  # Set page length
format STDOUT =
@<<<<<
$line
.
my $line = "text";
write;
"#,
    },
    FormatStatementCase {
        id: "format.lines.left",
        description: "Format with lines-left-on-page tracking.",
        tags: &["format", "page", "special-var"],
        source: r#"format STDOUT =
@<<<<<
$item
.
my $item = "data";
write;
my $remaining = $-;
"#,
    },
    FormatStatementCase {
        id: "format.name.special.var",
        description: "Format name special variable usage.",
        tags: &["format", "special-var", "name"],
        source: r#"format MYFORMAT =
@<<<<<
$text
.
$~ = "MYFORMAT";
my $text = "test";
write;
"#,
    },
    FormatStatementCase {
        id: "format.top.special.var",
        description: "Format top-of-page special variable.",
        tags: &["format", "special-var", "header"],
        source: r#"format MYFORMAT_TOP =
Header Line
.
$^ = "MYFORMAT_TOP";
"#,
    },
    FormatStatementCase {
        id: "format.empty",
        description: "Empty format declaration.",
        tags: &["format", "empty", "edge-case"],
        source: r#"format EMPTY =
.
"#,
    },
    FormatStatementCase {
        id: "format.nested.blocks",
        description: "Format with nested code blocks for field values.",
        tags: &["format", "nested", "complex"],
        source: r#"my @values = (1, 2, 3);
format BLOCKS =
@>>>
do { my $sum = 0; $sum += $_ for @values; $sum }
.
"#,
    },
    FormatStatementCase {
        id: "format.special.characters",
        description: "Format with special characters in picture lines.",
        tags: &["format", "special", "edge-case"],
        source: r#"my $value = "test";
format SPECIAL =
[@<<<<<]
 $value
.
"#,
    },
    FormatStatementCase {
        id: "format.array.iteration",
        description: "Format using array iteration for repeated output.",
        tags: &["format", "array", "iteration"],
        source: r#"my @items = ("one", "two", "three");
format LIST =
@<<<<<
$item
.
for my $item (@items) {
    write;
}
"#,
    },
    FormatStatementCase {
        id: "format.continuation.field",
        description: "Format with field continuation across lines.",
        tags: &["format", "continuation", "multiline"],
        source: r#"my $long_text = "This is a very long text that needs to continue";
format CONTINUE =
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$long_text
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$long_text
.
"#,
    },
    FormatStatementCase {
        id: "format.hash.values",
        description: "Format with hash value fields.",
        tags: &["format", "hash", "variable"],
        source: r#"my %data = (name => "Ada", age => 37);
format HASH =
Name: @<<<<<  Age: @>>
      $data{name}, $data{age}
.
"#,
    },
    FormatStatementCase {
        id: "format.method.result",
        description: "Format with method call results.",
        tags: &["format", "method", "oop"],
        source: r#"my $obj = MyClass->new();
format METHOD =
@<<<<<<<<
$obj->get_name()
.
"#,
    },
    FormatStatementCase {
        id: "format.justified.numeric",
        description: "Format with justified numeric fields.",
        tags: &["format", "numeric", "alignment"],
        source: r#"my ($price, $qty, $total) = (19.99, 5, 99.95);
format INVOICE =
Price: $@##.## Qty: @>> Total: $@###.##
        $price,      $qty,       $total
.
"#,
    },
    FormatStatementCase {
        id: "format.repeated.picture",
        description: "Format with repeated picture line patterns.",
        tags: &["format", "repeated", "pattern"],
        source: r#"my @values = (1, 2, 3, 4, 5);
my $value = shift @values;
format REPEAT =
@###
$value
@###
$value
@###
$value
.
"#,
    },
    FormatStatementCase {
        id: "format.conditional.field",
        description: "Format with conditional field evaluation.",
        tags: &["format", "conditional", "expression"],
        source: r#"my ($flag, $yes, $no) = (1, "YES", "NO");
format CONDITIONAL =
@<<<<<
$flag ? $yes : $no
.
"#,
    },
    FormatStatementCase {
        id: "format.write.to.variable",
        description: "Format output captured to a variable.",
        tags: &["format", "capture", "variable"],
        source: r#"my $output;
open my $fh, ">", \$output;
format $fh =
@<<<<<
$data
.
my $data = "test";
write $fh;
close $fh;
"#,
    },
    FormatStatementCase {
        id: "format.legacy.compatibility",
        description: "Format with legacy Perl 4 style patterns.",
        tags: &["format", "legacy", "perl4"],
        source: r#"format LEGACY =
@<<<<<<<<<<<  @>>>>>>>>>>>>  @||||||||||||||
$left,        $right,        $center
.
my ($left, $right, $center) = ("L", "R", "C");
"#,
    },
];

/// Return all format statement test fixtures.
pub fn format_statement_cases() -> &'static [FormatStatementCase] {
    FORMAT_STATEMENT_CASES
}

/// Find a format statement fixture by ID.
pub fn find_format_case(id: &str) -> Option<&'static FormatStatementCase> {
    format_statement_cases().iter().find(|case| case.id == id)
}

/// Convenience helper for working with format statement fixtures.
pub struct FormatStatementGenerator;

impl FormatStatementGenerator {
    /// Return all available format statement cases.
    pub fn all_cases() -> &'static [FormatStatementCase] {
        format_statement_cases()
    }

    /// Return format statement cases with a matching tag.
    pub fn by_tag(tag: &str) -> Vec<&'static FormatStatementCase> {
        format_statement_cases().iter().filter(|case| case.tags.contains(&tag)).collect()
    }

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

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

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

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

    /// Find a single format statement case by ID.
    pub fn find(id: &str) -> Option<&'static FormatStatementCase> {
        find_format_case(id)
    }

    /// Return sorted unique format statement tags.
    pub fn tags() -> Vec<&'static str> {
        let mut tags: Vec<&'static str> =
            format_statement_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 format_cases_have_ids() {
        assert!(format_statement_cases().iter().all(|case| !case.id.is_empty()));
    }

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

    #[test]
    fn format_cases_have_tags() {
        assert!(format_statement_cases().iter().all(|case| !case.tags.is_empty()));
    }

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

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

    #[test]
    fn format_case_lookup_by_id() {
        let case = find_format_case("format.basic.declaration");
        assert!(case.is_some());
        assert_eq!(must_some(case).id, "format.basic.declaration");
    }

    #[test]
    fn format_cases_can_filter_by_tag() {
        let basic = FormatStatementGenerator::by_tag("format");
        assert!(!basic.is_empty());
    }

    #[test]
    fn format_cases_can_filter_by_any_tag() {
        let matches = FormatStatementGenerator::by_tags_any(&["formline", "picture"]);
        assert!(!matches.is_empty());
    }

    #[test]
    fn format_cases_can_filter_by_all_tags() {
        let matches = FormatStatementGenerator::by_tags_all(&["format", "picture", "alignment"]);
        assert!(!matches.is_empty());
    }

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

    #[test]
    fn format_basic_declaration_exists() {
        let case = FormatStatementGenerator::find("format.basic.declaration");
        assert!(case.is_some());
        assert!(must_some(case).source.contains("format MYFORMAT"));
    }

    #[test]
    fn format_stdout_exists() {
        let case = FormatStatementGenerator::find("format.stdout");
        assert!(case.is_some());
        assert!(must_some(case).source.contains("format STDOUT"));
    }

    #[test]
    fn format_picture_lines_exist() {
        let left = FormatStatementGenerator::find("format.picture.left");
        let right = FormatStatementGenerator::find("format.picture.right");
        let center = FormatStatementGenerator::find("format.picture.center");

        assert!(left.is_some());
        assert!(right.is_some());
        assert!(center.is_some());

        assert!(must_some(left).source.contains("@<<<<<"));
        assert!(must_some(right).source.contains("@>>>>>"));
        assert!(must_some(center).source.contains("@|||||"));
    }

    #[test]
    fn format_formline_exists() {
        let case = FormatStatementGenerator::find("format.formline.builtin");
        assert!(case.is_some());
        assert!(must_some(case).source.contains("formline"));
    }

    #[test]
    fn format_write_call_exists() {
        let case = FormatStatementGenerator::find("format.write.call");
        assert!(case.is_some());
        assert!(must_some(case).source.contains("write"));
    }

    #[test]
    fn format_multiline_exists() {
        let case = FormatStatementGenerator::find("format.multiline");
        assert!(case.is_some());
    }

    #[test]
    fn format_computed_field_exists() {
        let case = FormatStatementGenerator::find("format.computed.field");
        assert!(case.is_some());
    }

    #[test]
    fn format_text_block_exists() {
        let case = FormatStatementGenerator::find("format.text.block");
        assert!(case.is_some());
        assert!(must_some(case).source.contains("^<<<<<"));
    }
}