tldr-core 0.1.2

Core analysis engine for TLDR code analysis tool
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
//! Program Slicing
//!
//! Computes program slices using PDG traversal.
//!
//! # Slice Types
//!
//! ## Backward Slice
//! Given a slicing criterion (line, optional variable), find all statements
//! that could affect the computation at that point.
//!
//! Algorithm:
//! 1. Start at the criterion node in PDG
//! 2. Follow edges backward (from target to source)
//! 3. Collect all visited nodes
//!
//! ## Forward Slice
//! Given a slicing criterion, find all statements that could be affected
//! by the computation at that point.
//!
//! Algorithm:
//! 1. Start at the criterion node in PDG
//! 2. Follow edges forward (from source to target)
//! 3. Collect all visited nodes
//!
//! # Variable Filtering
//! If a variable is specified, only follow edges related to that variable.
//! For data dependencies, this filters by the variable name.
//! For control dependencies, all are followed (they affect all variables).

use std::collections::{HashMap, HashSet};
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::pdg::get_pdg_context;
use crate::types::{DependenceType, Language, PdgInfo, SliceDirection};
use crate::TldrResult;

// =============================================================================
// Rich Slice Types
// =============================================================================

/// A single node in a rich program slice, containing source code and metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SliceNode {
    /// Source line number
    pub line: u32,
    /// Trimmed source line content
    pub code: String,
    /// PDG node type (e.g., "assignment", "return", "call")
    pub node_type: String,
    /// Variables defined at this line
    pub definitions: Vec<String>,
    /// Variables used at this line
    pub uses: Vec<String>,
    /// How this node connects to the dependency chain: "data" or "control"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dep_type: Option<String>,
    /// Variable name for data dependencies
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dep_label: Option<String>,
}

/// An edge in the rich slice representing a dependency relationship
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SliceEdge {
    /// Source line number
    pub from_line: u32,
    /// Target line number
    pub to_line: u32,
    /// Dependency type: "data" or "control"
    pub dep_type: String,
    /// Variable name for data dependencies, empty for control
    pub label: String,
}

/// Rich slice result containing code content and dependency chains
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RichSlice {
    /// Slice nodes sorted by line number
    pub nodes: Vec<SliceNode>,
    /// Dependency chain edges within the slice
    pub edges: Vec<SliceEdge>,
}

/// Compute program slice
///
/// # Arguments
/// * `source_or_path` - Either source code string or path to a file
/// * `function_name` - Name of the function to slice
/// * `line` - Line number to slice from
/// * `direction` - Backward or forward slice
/// * `variable` - Optional variable to filter by
/// * `language` - Programming language
///
/// # Returns
/// * `Ok(HashSet<u32>)` - Set of line numbers in the slice
/// * Empty set if line is not in the function
///
/// # Example
/// ```ignore
/// use tldr_core::pdg::get_slice;
/// use tldr_core::{Language, SliceDirection};
///
/// let slice = get_slice(
///     "def foo(): x = 1; return x",
///     "foo",
///     2,  // return line
///     SliceDirection::Backward,
///     None,
///     Language::Python
/// )?;
/// // slice should include line 1 (x = 1)
/// ```
pub fn get_slice(
    source_or_path: &str,
    function_name: &str,
    line: u32,
    direction: SliceDirection,
    variable: Option<&str>,
    language: Language,
) -> TldrResult<HashSet<u32>> {
    // Get PDG for the function
    let pdg = get_pdg_context(source_or_path, function_name, language)?;

    // Find the node(s) containing the target line
    let start_nodes = find_nodes_for_line(&pdg, line);

    if start_nodes.is_empty() {
        // Line not in function - return empty set per spec
        return Ok(HashSet::new());
    }

    // Perform slice traversal
    let slice = compute_slice(&pdg, &start_nodes, direction, variable);

    // Convert node IDs to line numbers
    let lines = nodes_to_lines(&pdg, &slice);

    Ok(lines)
}

/// Compute a rich program slice with source code and dependency chains
///
/// Like `get_slice()` but returns `RichSlice` with code content, node metadata,
/// and filtered dependency edges instead of bare line numbers.
///
/// # Arguments
/// * `source_or_path` - Either source code string or path to a file
/// * `function_name` - Name of the function to slice
/// * `line` - Line number to slice from
/// * `direction` - Backward or forward slice
/// * `variable` - Optional variable to filter by
/// * `language` - Programming language
///
/// # Returns
/// * `Ok(RichSlice)` - Rich slice with code, metadata, and edges
/// * Empty RichSlice if line is not in the function
pub fn get_slice_rich(
    source_or_path: &str,
    function_name: &str,
    line: u32,
    direction: SliceDirection,
    variable: Option<&str>,
    language: Language,
) -> TldrResult<RichSlice> {
    // Get PDG for the function
    let pdg = get_pdg_context(source_or_path, function_name, language)?;

    // Find the node(s) containing the target line
    let start_nodes = find_nodes_for_line(&pdg, line);

    if start_nodes.is_empty() {
        return Ok(RichSlice {
            nodes: Vec::new(),
            edges: Vec::new(),
        });
    }

    // Perform slice traversal -- get set of visited node IDs
    let visited = compute_slice(&pdg, &start_nodes, direction, variable);

    // Read source lines for code content
    let source_lines = read_source_lines(source_or_path);

    // Build a map from node_id -> PdgNode for visited nodes
    let visited_nodes: Vec<&crate::types::PdgNode> = pdg
        .nodes
        .iter()
        .filter(|n| visited.contains(&n.id))
        .collect();

    // Collect all lines covered by visited nodes, with their metadata
    // Multiple nodes can cover the same line; we merge definitions/uses
    let mut line_map: HashMap<u32, SliceNode> = HashMap::new();

    for node in &visited_nodes {
        for l in node.lines.0..=node.lines.1 {
            if l == 0 {
                continue;
            }
            let code = source_lines
                .get((l as usize).wrapping_sub(1))
                .map(|s| s.trim_end().to_string())
                .unwrap_or_default();

            let entry = line_map.entry(l).or_insert_with(|| SliceNode {
                line: l,
                code,
                node_type: node.node_type.clone(),
                definitions: Vec::new(),
                uses: Vec::new(),
                dep_type: None,
                dep_label: None,
            });

            // Merge definitions and uses from multiple nodes covering same line
            for d in &node.definitions {
                if !entry.definitions.contains(d) {
                    entry.definitions.push(d.clone());
                }
            }
            for u in &node.uses {
                if !entry.uses.contains(u) {
                    entry.uses.push(u.clone());
                }
            }
        }
    }

    // Filter PDG edges to only those within the slice (both endpoints visited)
    let mut edges: Vec<SliceEdge> = Vec::new();
    for edge in &pdg.edges {
        if visited.contains(&edge.source_id) && visited.contains(&edge.target_id) {
            // Map node IDs to their representative line numbers
            let from_line = node_id_to_line(&pdg, edge.source_id);
            let to_line = node_id_to_line(&pdg, edge.target_id);
            if let (Some(from), Some(to)) = (from_line, to_line) {
                let dep_str = match edge.dep_type {
                    DependenceType::Data => "data",
                    DependenceType::Control => "control",
                };
                edges.push(SliceEdge {
                    from_line: from,
                    to_line: to,
                    dep_type: dep_str.to_string(),
                    label: edge.label.clone(),
                });

                // Annotate the target node with dep info (how it connects)
                if let Some(node) = line_map.get_mut(&to) {
                    if node.dep_type.is_none() {
                        node.dep_type = Some(dep_str.to_string());
                        if !edge.label.is_empty() {
                            node.dep_label = Some(edge.label.clone());
                        }
                    }
                }
            }
        }
    }

    // Sort edges by from_line, then to_line
    edges.sort_by_key(|e| (e.from_line, e.to_line));
    // Deduplicate edges (same from/to/type/label)
    edges.dedup_by(|a, b| {
        a.from_line == b.from_line
            && a.to_line == b.to_line
            && a.dep_type == b.dep_type
            && a.label == b.label
    });

    // Collect and sort nodes by line number
    let mut nodes: Vec<SliceNode> = line_map.into_values().collect();
    nodes.sort_by_key(|n| n.line);

    Ok(RichSlice { nodes, edges })
}

/// Read source lines from a path or inline source string
fn read_source_lines(source_or_path: &str) -> Vec<String> {
    let path = Path::new(source_or_path);
    if path.exists() && path.is_file() {
        match std::fs::read_to_string(path) {
            Ok(content) => content.lines().map(|l| l.to_string()).collect(),
            Err(_) => source_or_path.lines().map(|l| l.to_string()).collect(),
        }
    } else {
        source_or_path.lines().map(|l| l.to_string()).collect()
    }
}

/// Map a PDG node ID to its first (representative) line number
fn node_id_to_line(pdg: &PdgInfo, node_id: usize) -> Option<u32> {
    pdg.nodes
        .iter()
        .find(|n| n.id == node_id)
        .map(|n| n.lines.0)
        .filter(|&l| l > 0)
}

/// Find PDG nodes that contain a specific line
fn find_nodes_for_line(pdg: &PdgInfo, line: u32) -> Vec<usize> {
    pdg.nodes
        .iter()
        .filter(|n| line >= n.lines.0 && line <= n.lines.1)
        .map(|n| n.id)
        .collect()
}

/// Compute slice using BFS/DFS traversal
fn compute_slice(
    pdg: &PdgInfo,
    start_nodes: &[usize],
    direction: SliceDirection,
    variable: Option<&str>,
) -> HashSet<usize> {
    let mut visited = HashSet::new();
    let mut worklist: Vec<usize> = start_nodes.to_vec();

    while let Some(node_id) = worklist.pop() {
        if visited.contains(&node_id) {
            continue;
        }
        visited.insert(node_id);

        // Find adjacent nodes based on direction
        let adjacent = match direction {
            SliceDirection::Backward => {
                // Follow edges TO this node (find sources)
                pdg.edges
                    .iter()
                    .filter(|e| e.target_id == node_id)
                    .filter(|e| should_follow_edge(e, variable))
                    .map(|e| e.source_id)
                    .collect::<Vec<_>>()
            }
            SliceDirection::Forward => {
                // Follow edges FROM this node (find targets)
                pdg.edges
                    .iter()
                    .filter(|e| e.source_id == node_id)
                    .filter(|e| should_follow_edge(e, variable))
                    .map(|e| e.target_id)
                    .collect::<Vec<_>>()
            }
        };

        for adj in adjacent {
            if !visited.contains(&adj) {
                worklist.push(adj);
            }
        }
    }

    visited
}

/// Check if an edge should be followed based on variable filter
fn should_follow_edge(edge: &crate::types::PdgEdge, variable: Option<&str>) -> bool {
    match variable {
        None => true, // No filter, follow all edges
        Some(var) => {
            match edge.dep_type {
                DependenceType::Control => true, // Always follow control deps
                DependenceType::Data => edge.label == var, // Only follow if variable matches
            }
        }
    }
}

/// Convert set of node IDs to set of line numbers
fn nodes_to_lines(pdg: &PdgInfo, node_ids: &HashSet<usize>) -> HashSet<u32> {
    let mut lines = HashSet::new();

    for &node_id in node_ids {
        if let Some(node) = pdg.nodes.iter().find(|n| n.id == node_id) {
            // Include all lines covered by this node
            for line in node.lines.0..=node.lines.1 {
                if line > 0 {
                    lines.insert(line);
                }
            }
        }
    }

    lines
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_backward_slice_simple() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let slice = get_slice(
            source,
            "foo",
            4,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Backward slice from "return y" should include y = x + 2 and x = 1
        assert!(!slice.is_empty(), "slice should not be empty");
    }

    #[test]
    fn test_forward_slice_simple() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        // Line 3 is "x = 1" (line 1 is blank, line 2 is "def foo():")
        let slice = get_slice(
            source,
            "foo",
            3,
            SliceDirection::Forward,
            None,
            Language::Python,
        )
        .unwrap();

        // Forward slice from "x = 1" should include the starting line at minimum
        // Note: forward slice traversal starts from the starting node
        assert!(slice.contains(&3), "slice should include the starting line");
    }

    #[test]
    fn test_slice_with_variable_filter() {
        let source = r#"
def foo():
    x = 1
    y = 2
    z = x + y
    return z
"#;
        let slice = get_slice(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            Some("x"),
            Language::Python,
        )
        .unwrap();

        // Backward slice for 'x' from "z = x + y" should include x = 1 but not y = 2
        // Note: the line numbers in this test are approximate
        assert!(!slice.is_empty(), "slice should not be empty");
    }

    #[test]
    fn test_slice_line_not_in_function() {
        let source = "def foo(): pass";
        let slice = get_slice(
            source,
            "foo",
            999,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Line 999 is not in the function - should return empty set
        assert!(
            slice.is_empty(),
            "slice for non-existent line should be empty"
        );
    }

    #[test]
    fn test_slice_returns_line_numbers() {
        let source = r#"
def foo():
    x = 1
    return x
"#;
        let slice = get_slice(
            source,
            "foo",
            3,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Result should be line numbers (positive integers)
        for &line in &slice {
            assert!(line > 0, "line numbers should be positive");
        }
    }

    #[test]
    fn test_backward_slice_with_control_deps() {
        let source = r#"
def foo(cond):
    if cond:
        x = 1
    else:
        x = 2
    return x
"#;
        let slice = get_slice(
            source,
            "foo",
            6,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Backward slice should include the if condition due to control dependency
        assert!(
            !slice.is_empty(),
            "slice should include control dependencies"
        );
    }

    #[test]
    fn test_forward_slice_traces_all_vars() {
        let source = r#"
def foo():
    x = 1
    y = x
    z = y
    return z
"#;
        // Line 3 is "x = 1" (line 1 is blank, line 2 is "def foo():")
        let slice = get_slice(
            source,
            "foo",
            3,
            SliceDirection::Forward,
            None,
            Language::Python,
        )
        .unwrap();

        // Forward slice from x=1 should include the starting line
        // The slice starts at the given line and follows forward dependencies
        assert!(
            slice.contains(&3),
            "forward slice should include the starting line"
        );
    }

    // =========================================================================
    // Tests for get_slice_rich()
    // =========================================================================

    #[test]
    fn test_rich_slice_returns_nodes_with_code() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            4,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Should have nodes with actual code content
        assert!(!rich.nodes.is_empty(), "rich slice should have nodes");
        for node in &rich.nodes {
            assert!(!node.code.is_empty(), "each node should have code content");
            assert!(node.line > 0, "line numbers should be positive");
        }
    }

    #[test]
    fn test_rich_slice_nodes_sorted_by_line() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Nodes must be sorted by line number
        let lines: Vec<u32> = rich.nodes.iter().map(|n| n.line).collect();
        let mut sorted = lines.clone();
        sorted.sort();
        assert_eq!(lines, sorted, "nodes should be sorted by line number");
    }

    #[test]
    fn test_rich_slice_code_is_trimmed() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        for node in &rich.nodes {
            assert_eq!(
                node.code,
                node.code.trim_end(),
                "code should have trailing whitespace trimmed"
            );
        }
    }

    #[test]
    fn test_rich_slice_preserves_definitions_and_uses() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // At least some nodes should have definitions or uses
        let has_defs = rich.nodes.iter().any(|n| !n.definitions.is_empty());
        let has_uses = rich.nodes.iter().any(|n| !n.uses.is_empty());
        assert!(
            has_defs || has_uses,
            "rich slice should preserve definition/use info from PDG"
        );
    }

    #[test]
    fn test_rich_slice_has_node_types() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        for node in &rich.nodes {
            assert!(
                !node.node_type.is_empty(),
                "each node should have a node_type"
            );
        }
    }

    #[test]
    fn test_rich_slice_edges_within_slice() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        let slice_lines: std::collections::HashSet<u32> =
            rich.nodes.iter().map(|n| n.line).collect();
        // All edges should reference lines that are in the slice
        for edge in &rich.edges {
            assert!(
                slice_lines.contains(&edge.from_line),
                "edge from_line {} should be in slice",
                edge.from_line
            );
            assert!(
                slice_lines.contains(&edge.to_line),
                "edge to_line {} should be in slice",
                edge.to_line
            );
        }
    }

    #[test]
    fn test_rich_slice_edge_dep_types() {
        let source = r#"
def foo(cond):
    if cond:
        x = 1
    else:
        x = 2
    return x
"#;
        let rich = get_slice_rich(
            source,
            "foo",
            7,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // Should have edges with valid dep_type strings
        for edge in &rich.edges {
            assert!(
                edge.dep_type == "data" || edge.dep_type == "control",
                "edge dep_type should be 'data' or 'control', got '{}'",
                edge.dep_type
            );
        }
    }

    #[test]
    fn test_rich_slice_empty_for_invalid_line() {
        let source = "def foo(): pass";
        let rich = get_slice_rich(
            source,
            "foo",
            999,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        assert!(
            rich.nodes.is_empty(),
            "rich slice for non-existent line should have no nodes"
        );
        assert!(
            rich.edges.is_empty(),
            "rich slice for non-existent line should have no edges"
        );
    }

    #[test]
    fn test_rich_slice_from_file_path() {
        // Create a temp file to test file-based slicing
        use std::io::Write;
        let dir = std::env::temp_dir();
        let path = dir.join("test_slice_rich.py");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, "def bar():").unwrap();
        writeln!(f, "    a = 10").unwrap();
        writeln!(f, "    b = a + 1").unwrap();
        writeln!(f, "    return b").unwrap();

        let rich = get_slice_rich(
            path.to_str().unwrap(),
            "bar",
            4,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        assert!(!rich.nodes.is_empty(), "should work with file path input");
        // Code should come from the file
        let has_return = rich.nodes.iter().any(|n| n.code.contains("return"));
        assert!(has_return, "should contain the criterion line code");

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn test_rich_slice_backward_compat_with_get_slice() {
        let source = r#"
def foo():
    x = 1
    y = x + 2
    return y
"#;
        let plain = get_slice(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();
        let rich = get_slice_rich(
            source,
            "foo",
            5,
            SliceDirection::Backward,
            None,
            Language::Python,
        )
        .unwrap();

        // The rich slice line set should match the plain slice line set
        let rich_lines: HashSet<u32> = rich.nodes.iter().map(|n| n.line).collect();
        assert_eq!(
            plain, rich_lines,
            "rich slice lines should match plain slice lines"
        );
    }
}