perl-module 0.16.0

Perl module resolution, import analysis, and refactoring — unified facade
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
634
635
636
637
638
639
640
641
//! Comprehensive unit tests for perl-module-token-core public API.

use perl_module::token_core::{
    ModuleTokenSpan, has_standalone_module_token_boundaries, is_module_identifier_char,
    is_module_token_char, parse_module_token,
};

// ---------------------------------------------------------------------------
// parse_module_token — basic parsing
// ---------------------------------------------------------------------------

#[test]
fn parse_single_bare_identifier() -> Result<(), String> {
    let span = parse_module_token("Foo", 0).ok_or("expected Some for single bare identifier")?;
    if span != (ModuleTokenSpan { start: 0, end: 3 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_canonical_two_segments() -> Result<(), String> {
    let span =
        parse_module_token("Foo::Bar", 0).ok_or("expected Some for canonical two-segment")?;
    if span != (ModuleTokenSpan { start: 0, end: 8 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_canonical_three_segments() -> Result<(), String> {
    let span = parse_module_token("Foo::Bar::Baz", 0)
        .ok_or("expected Some for canonical three-segment")?;
    if span != (ModuleTokenSpan { start: 0, end: 13 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_legacy_two_segments() -> Result<(), String> {
    let span = parse_module_token("Foo'Bar", 0).ok_or("expected Some for legacy two-segment")?;
    if span != (ModuleTokenSpan { start: 0, end: 7 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_mixed_separators() -> Result<(), String> {
    let span = parse_module_token("Foo::Bar'Baz", 0).ok_or("expected Some for mixed separators")?;
    if span != (ModuleTokenSpan { start: 0, end: 12 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_mixed_separators_reversed() -> Result<(), String> {
    let span = parse_module_token("Foo'Bar::Baz", 0)
        .ok_or("expected Some for reversed mixed separators")?;
    if span != (ModuleTokenSpan { start: 0, end: 12 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// parse_module_token — offsets
// ---------------------------------------------------------------------------

#[test]
fn parse_at_nonzero_offset() -> Result<(), String> {
    let span = parse_module_token("use Foo::Bar;", 4).ok_or("expected Some at offset 4")?;
    if span != (ModuleTokenSpan { start: 4, end: 12 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_token_stops_at_semicolon() -> Result<(), String> {
    let span =
        parse_module_token("Foo::Bar;more", 0).ok_or("expected Some stopping at semicolon")?;
    if span != (ModuleTokenSpan { start: 0, end: 8 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_token_stops_at_whitespace() -> Result<(), String> {
    let span =
        parse_module_token("Foo::Bar rest", 0).ok_or("expected Some stopping at whitespace")?;
    if span != (ModuleTokenSpan { start: 0, end: 8 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_token_stops_at_paren() -> Result<(), String> {
    let span = parse_module_token("Foo::Bar()", 0).ok_or("expected Some stopping at paren")?;
    if span != (ModuleTokenSpan { start: 0, end: 8 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// parse_module_token — underscore and digit in identifiers
// ---------------------------------------------------------------------------

#[test]
fn parse_underscore_identifier() -> Result<(), String> {
    let span = parse_module_token("_Foo::_Bar", 0)
        .ok_or("expected Some for underscore-prefixed identifiers")?;
    if span != (ModuleTokenSpan { start: 0, end: 10 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_underscore_only_segment() -> Result<(), String> {
    let span = parse_module_token("_::_", 0).ok_or("expected Some for underscore-only segments")?;
    if span != (ModuleTokenSpan { start: 0, end: 4 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_identifier_with_digits() -> Result<(), String> {
    let span =
        parse_module_token("Foo2::Bar3", 0).ok_or("expected Some for identifiers with digits")?;
    if span != (ModuleTokenSpan { start: 0, end: 10 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_single_char_segments() -> Result<(), String> {
    let span = parse_module_token("A::B::C", 0).ok_or("expected Some for single-char segments")?;
    if span != (ModuleTokenSpan { start: 0, end: 7 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// parse_module_token — None cases
// ---------------------------------------------------------------------------

#[test]
fn parse_empty_string_returns_none() {
    assert!(parse_module_token("", 0).is_none());
}

#[test]
fn parse_start_beyond_length_returns_none() {
    assert!(parse_module_token("Foo", 10).is_none());
}

#[test]
fn parse_start_at_exact_length_returns_none() {
    assert!(parse_module_token("Foo", 3).is_none());
}

#[test]
fn parse_start_at_digit_returns_none() {
    assert!(parse_module_token("123", 0).is_none());
}

#[test]
fn parse_start_at_space_returns_none() {
    assert!(parse_module_token(" Foo", 0).is_none());
}

#[test]
fn parse_start_at_colon_returns_none() {
    assert!(parse_module_token("::Foo", 0).is_none());
}

#[test]
fn parse_start_at_semicolon_returns_none() {
    assert!(parse_module_token(";Foo", 0).is_none());
}

#[test]
fn parse_start_at_apostrophe_returns_none() {
    assert!(parse_module_token("'Foo", 0).is_none());
}

// ---------------------------------------------------------------------------
// parse_module_token — trailing separator edge cases
// ---------------------------------------------------------------------------

#[test]
fn parse_trailing_canonical_separator_returns_none() {
    // "Foo::" — separator found but no valid segment follows, so returns None
    assert!(parse_module_token("Foo::", 0).is_none());
}

#[test]
fn parse_trailing_legacy_separator_returns_none() {
    // "Foo'" — separator found but no valid segment follows, so returns None
    assert!(parse_module_token("Foo'", 0).is_none());
}

#[test]
fn parse_separator_followed_by_digit_returns_none() {
    // "Foo::2bar" — digit is not a valid identifier start after separator, returns None
    assert!(parse_module_token("Foo::2bar", 0).is_none());
}

#[test]
fn parse_legacy_separator_followed_by_digit_returns_none() {
    // "Foo'2bar" — digit after legacy separator is not valid, returns None
    assert!(parse_module_token("Foo'2bar", 0).is_none());
}

// ---------------------------------------------------------------------------
// parse_module_token — deeply nested
// ---------------------------------------------------------------------------

#[test]
fn parse_four_segment_canonical() -> Result<(), String> {
    let span =
        parse_module_token("A::B::C::D", 0).ok_or("expected Some for four-segment canonical")?;
    if span != (ModuleTokenSpan { start: 0, end: 10 }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

#[test]
fn parse_long_real_world_module() -> Result<(), String> {
    let input = "Moose::Meta::Role::Application::ToClass";
    let span = parse_module_token(input, 0).ok_or("expected Some for long module")?;
    if span != (ModuleTokenSpan { start: 0, end: input.len() }) {
        return Err(format!("unexpected span: {span:?}"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// parse_module_token — extracted substring matches
// ---------------------------------------------------------------------------

#[test]
fn extracted_text_matches_expected() -> Result<(), String> {
    let input = "use Foo::Bar::Baz;";
    let span = parse_module_token(input, 4).ok_or("expected Some")?;
    let extracted = &input[span.start..span.end];
    if extracted != "Foo::Bar::Baz" {
        return Err(format!("unexpected extracted text: {extracted}"));
    }
    Ok(())
}

#[test]
fn extracted_legacy_text_matches() -> Result<(), String> {
    let input = "use Foo'Bar;";
    let span = parse_module_token(input, 4).ok_or("expected Some")?;
    let extracted = &input[span.start..span.end];
    if extracted != "Foo'Bar" {
        return Err(format!("unexpected extracted text: {extracted}"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// has_standalone_module_token_boundaries
// ---------------------------------------------------------------------------

#[test]
fn standalone_at_line_start_and_end() {
    assert!(has_standalone_module_token_boundaries("Foo::Bar", 0, 8));
}

#[test]
fn standalone_surrounded_by_non_module_chars() {
    assert!(has_standalone_module_token_boundaries("(Foo::Bar)", 1, 9));
}

#[test]
fn standalone_after_space() {
    assert!(has_standalone_module_token_boundaries("use Foo::Bar;", 4, 12));
}

#[test]
fn not_standalone_when_preceded_by_alpha() {
    // "xFoo" at position 1..4 — 'x' is a module token char
    assert!(!has_standalone_module_token_boundaries("xFoo", 1, 4));
}

#[test]
fn not_standalone_when_followed_by_alpha() {
    assert!(!has_standalone_module_token_boundaries("Foox", 0, 3));
}

#[test]
fn not_standalone_when_preceded_by_colon() {
    assert!(!has_standalone_module_token_boundaries(":Foo", 1, 4));
}

#[test]
fn not_standalone_when_followed_by_colon() {
    assert!(!has_standalone_module_token_boundaries("Foo:", 0, 3));
}

#[test]
fn not_standalone_when_preceded_by_underscore() {
    assert!(!has_standalone_module_token_boundaries("_Foo", 1, 4));
}

#[test]
fn not_standalone_when_followed_by_underscore() {
    assert!(!has_standalone_module_token_boundaries("Foo_", 0, 3));
}

#[test]
fn not_standalone_when_followed_by_digit() {
    assert!(!has_standalone_module_token_boundaries("Foo1", 0, 3));
}

#[test]
fn not_standalone_when_preceded_by_digit() {
    assert!(!has_standalone_module_token_boundaries("1Foo", 1, 4));
}

#[test]
fn standalone_when_preceded_by_double_colon_beyond_span() {
    // "::Foo" — the left context at position 2 has ':' which is a module_token_char
    assert!(!has_standalone_module_token_boundaries("::Foo", 2, 5));
}

// ---------------------------------------------------------------------------
// has_standalone_module_token_boundaries — apostrophe edge cases
// ---------------------------------------------------------------------------

#[test]
fn not_standalone_when_left_context_has_apostrophe_with_identifier() {
    // "X'Foo" — apostrophe at position 1, preceded by 'X' (identifier char)
    // so left context of Foo (position 2) sees apostrophe + identifier = module char
    assert!(!has_standalone_module_token_boundaries("X'Foo", 2, 5));
}

#[test]
fn standalone_when_left_context_has_lone_apostrophe() {
    // "'Foo" — apostrophe at position 0 with nothing before it
    assert!(has_standalone_module_token_boundaries("'Foo", 1, 4));
}

#[test]
fn not_standalone_when_right_context_has_apostrophe_with_identifier() {
    // "Foo'X" — Foo at 0..3, right context sees ' then X
    assert!(!has_standalone_module_token_boundaries("Foo'X", 0, 3));
}

#[test]
fn standalone_when_right_context_has_apostrophe_without_identifier() {
    // "Foo'" — Foo at 0..3, right context sees ' then end-of-string
    assert!(has_standalone_module_token_boundaries("Foo'", 0, 3));
}

#[test]
fn standalone_when_right_apostrophe_followed_by_non_identifier() {
    // "Foo' " — Foo at 0..3, right context sees ' then space (not identifier)
    assert!(has_standalone_module_token_boundaries("Foo' ", 0, 3));
}

// ---------------------------------------------------------------------------
// has_standalone_module_token_boundaries — edge positions
// ---------------------------------------------------------------------------

#[test]
fn standalone_empty_string_start_eq_end() {
    // Degenerate case: start == 0, end == 0 on an empty-ish line
    assert!(has_standalone_module_token_boundaries("", 0, 0));
}

#[test]
fn standalone_end_at_line_length() {
    assert!(has_standalone_module_token_boundaries("Foo", 0, 3));
}

#[test]
fn standalone_end_beyond_line_length() {
    // end > line.len() is treated as no right context
    assert!(has_standalone_module_token_boundaries("Foo", 0, 10));
}

#[test]
fn standalone_after_semicolon() {
    assert!(has_standalone_module_token_boundaries(";Foo;", 1, 4));
}

#[test]
fn standalone_after_open_paren() {
    assert!(has_standalone_module_token_boundaries("(Foo)", 1, 4));
}

#[test]
fn standalone_after_newline_char() {
    assert!(has_standalone_module_token_boundaries("\nFoo\n", 1, 4));
}

// ---------------------------------------------------------------------------
// is_module_token_char — comprehensive character class
// ---------------------------------------------------------------------------

#[test]
fn module_token_char_accepts_ascii_letters() {
    for ch in 'a'..='z' {
        assert!(is_module_token_char(ch), "expected true for '{ch}'");
    }
    for ch in 'A'..='Z' {
        assert!(is_module_token_char(ch), "expected true for '{ch}'");
    }
}

#[test]
fn module_token_char_accepts_digits() {
    for ch in '0'..='9' {
        assert!(is_module_token_char(ch), "expected true for '{ch}'");
    }
}

#[test]
fn module_token_char_accepts_underscore() {
    assert!(is_module_token_char('_'));
}

#[test]
fn module_token_char_accepts_colon() {
    assert!(is_module_token_char(':'));
}

#[test]
fn module_token_char_rejects_common_non_module_chars() {
    let rejects = [
        ' ', '\t', '\n', ';', '(', ')', '[', ']', '{', '}', ',', '.', '!', '@', '#', '$', '%', '^',
        '&', '*', '-', '+', '=', '<', '>', '/', '\\', '|', '~', '`', '"', '\'', '?',
    ];
    for ch in rejects {
        assert!(!is_module_token_char(ch), "expected false for '{ch}'");
    }
}

// ---------------------------------------------------------------------------
// is_module_identifier_char — comprehensive character class
// ---------------------------------------------------------------------------

#[test]
fn module_identifier_char_accepts_ascii_letters() {
    for ch in 'a'..='z' {
        assert!(is_module_identifier_char(ch), "expected true for '{ch}'");
    }
    for ch in 'A'..='Z' {
        assert!(is_module_identifier_char(ch), "expected true for '{ch}'");
    }
}

#[test]
fn module_identifier_char_accepts_digits() {
    for ch in '0'..='9' {
        assert!(is_module_identifier_char(ch), "expected true for '{ch}'");
    }
}

#[test]
fn module_identifier_char_accepts_underscore() {
    assert!(is_module_identifier_char('_'));
}

#[test]
fn module_identifier_char_rejects_colon() {
    assert!(!is_module_identifier_char(':'));
}

#[test]
fn module_identifier_char_rejects_apostrophe() {
    assert!(!is_module_identifier_char('\''));
}

#[test]
fn module_identifier_char_rejects_special_chars() {
    let rejects = [' ', '\t', ';', '(', ')', '-', '.', '@', '$', '%'];
    for ch in rejects {
        assert!(!is_module_identifier_char(ch), "expected false for '{ch}'");
    }
}

// ---------------------------------------------------------------------------
// ModuleTokenSpan — struct behavior
// ---------------------------------------------------------------------------

#[test]
fn module_token_span_debug_impl() {
    let span = ModuleTokenSpan { start: 1, end: 5 };
    let debug = format!("{span:?}");
    assert!(!debug.is_empty());
}

#[test]
fn module_token_span_clone() {
    let span = ModuleTokenSpan { start: 0, end: 3 };
    let cloned = span;
    assert_eq!(span, cloned);
}

#[test]
fn module_token_span_copy() {
    let span = ModuleTokenSpan { start: 2, end: 7 };
    let copied = span;
    // Both should be usable since Copy
    assert_eq!(span.start, copied.start);
    assert_eq!(span.end, copied.end);
}

#[test]
fn module_token_span_eq() {
    let a = ModuleTokenSpan { start: 0, end: 5 };
    let b = ModuleTokenSpan { start: 0, end: 5 };
    let c = ModuleTokenSpan { start: 0, end: 6 };
    assert_eq!(a, b);
    assert_ne!(a, c);
}

// ---------------------------------------------------------------------------
// parse_module_token — real-world Perl patterns
// ---------------------------------------------------------------------------

#[test]
fn parse_use_statement() -> Result<(), String> {
    let input = "use strict;";
    let span = parse_module_token(input, 4).ok_or("expected Some")?;
    let extracted = &input[span.start..span.end];
    if extracted != "strict" {
        return Err(format!("unexpected: {extracted}"));
    }
    Ok(())
}

#[test]
fn parse_require_statement() -> Result<(), String> {
    let input = "require Carp::Heavy;";
    let span = parse_module_token(input, 8).ok_or("expected Some")?;
    let extracted = &input[span.start..span.end];
    if extracted != "Carp::Heavy" {
        return Err(format!("unexpected: {extracted}"));
    }
    Ok(())
}

#[test]
fn parse_method_call_context() -> Result<(), String> {
    let input = "Foo::Bar->new()";
    let span = parse_module_token(input, 0).ok_or("expected Some")?;
    let extracted = &input[span.start..span.end];
    if extracted != "Foo::Bar" {
        return Err(format!("unexpected: {extracted}"));
    }
    Ok(())
}

#[test]
fn parse_isa_check_context_with_quotes() {
    // "$obj->isa('Some::Class')" — the closing ' is seen as a legacy separator
    // followed by ')' which is not a valid identifier start, so returns None
    let input = "$obj->isa('Some::Class')";
    assert!(parse_module_token(input, 11).is_none());
}

#[test]
fn parse_isa_check_without_trailing_quote() -> Result<(), String> {
    // Without surrounding quotes, module parses fine
    let input = "$obj->isa(Some::Class)";
    let span = parse_module_token(input, 10).ok_or("expected Some")?;
    let extracted = &input[span.start..span.end];
    if extracted != "Some::Class" {
        return Err(format!("unexpected: {extracted}"));
    }
    Ok(())
}

#[test]
fn parse_function_call_context() -> Result<(), String> {
    let input = "Foo::Bar::baz()";
    let span = parse_module_token(input, 0).ok_or("expected Some")?;
    // Should parse entire qualified name including function
    let extracted = &input[span.start..span.end];
    if extracted != "Foo::Bar::baz" {
        return Err(format!("unexpected: {extracted}"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// parse_module_token — boundary combinations with standalone check
// ---------------------------------------------------------------------------

#[test]
fn parsed_token_is_standalone_in_use_statement() -> Result<(), String> {
    let input = "use Moose::Role;";
    let span = parse_module_token(input, 4).ok_or("expected Some")?;
    if !has_standalone_module_token_boundaries(input, span.start, span.end) {
        return Err("expected standalone boundaries".to_string());
    }
    Ok(())
}

#[test]
fn parsed_token_not_standalone_when_part_of_longer() {
    let input = "use Foo::Bar::Baz;";
    // Parse from offset 4 gets the full "Foo::Bar::Baz"
    // Check if just "Foo::Bar" (4..12) is standalone — it shouldn't be
    assert!(!has_standalone_module_token_boundaries(input, 4, 12));
}

#[test]
fn parse_at_each_segment_start() -> Result<(), String> {
    let input = "Foo::Bar::Baz";
    // Parse starting at "Bar::Baz" (offset 5)
    let span = parse_module_token(input, 5).ok_or("expected Some at Bar")?;
    let extracted = &input[span.start..span.end];
    if extracted != "Bar::Baz" {
        return Err(format!("unexpected: {extracted}"));
    }
    // Parse starting at "Baz" (offset 10)
    let span2 = parse_module_token(input, 10).ok_or("expected Some at Baz")?;
    let extracted2 = &input[span2.start..span2.end];
    if extracted2 != "Baz" {
        return Err(format!("unexpected: {extracted2}"));
    }
    Ok(())
}