Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/outline.rs

2.6 KiB, 1 run

created by r3767465682:77352, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! `outline::outline`: the landmark lines of indented text, as deep as a byte budget allows.
2//!
3//! Written for Daimond's `file_read` (13 Nov 2026), which shows a long file's outline first so
4//! that the daimon reads ranges and not the head.
5
6use oxedyne_fe2o3_text::outline::{
7 outline,
8 Outline,
9};
10
11use std::io::Cursor;
12
13
14fn of(text: &str, budget: usize) -> Option<Outline> {
15 outline(&mut Cursor::new(text.as_bytes().to_vec()), budget).expect("an in-memory read")
16}
17
18const SOURCE: &str = "\
19struct Point {
20 x: f64,
21 y: f64,
22}
23
24impl Point {
25 fn new(x: f64, y: f64) -> Self {
26 Self { x, y }
27 }
28
29 fn norm(&self) -> f64 {
30 (self.x * self.x + self.y * self.y).sqrt()
31 }
32}
33";
34
35#[test]
36fn an_indented_source_file_outlines_its_top_level() {
37 let o = of(SOURCE, 80).expect("an outline");
38 assert_eq!(o.text, "1\tstruct Point {\n6\timpl Point {\n");
39 assert_eq!(o.lines, 14);
40 let o = of(SOURCE, 1000).expect("an outline");
41 assert_eq!(o.text, "1\tstruct Point {\n2\t\tx: f64,\n3\t\ty: f64,\n6\timpl Point {\n\
42 7\t\tfn new(x: f64, y: f64) -> Self {\n8\t\t\tSelf { x, y }\n\
43 11\t\tfn norm(&self) -> f64 {\n12\t\t\t(self.x * self.x + self.y * self.y).sqrt()\n");
44}
45
46#[test]
47fn a_long_run_of_imports_collapses_to_one_entry() {
48 let mut text = String::from("//! A module.\n\n");
49 for i in 0..10 {
50 text.push_str(&format!("import mod_{}\n", i));
51 }
52 text.push('\n');
53 for i in 10..19 {
54 text.push_str(&format!("import mod_{}\n", i));
55 }
56 text.push_str("\ndef main():\n pass\n");
57 let o = of(&text, 1000).expect("an outline");
58 assert_eq!(o.text, "1\t//! A module.\n3-22\timport … (20 lines)\n24\tdef main():\n25\t pass\n");
59}
60
61#[test]
62fn an_outline_drops_depth_until_it_fits() {
63 let mut text = String::new();
64 for b in 0..20 {
65 text.push_str(&format!("impl Block{} {{\n", b));
66 for f in 0..100 {
67 text.push_str(&format!("\tfn f{}(&self) {{\n\t\tlet v = {};\n\t}}\n", f, f));
68 }
69 text.push_str("}\n");
70 }
71 let o = of(&text, 2000).expect("an outline");
72 let rows: Vec<&str> = o.text.lines().collect();
73 assert_eq!(rows.len(), 20, "{}", o.text);
74 let lines: Vec<&str> = text.lines().collect();
75 for row in rows {
76 let (n, t) = row.split_once('\t').expect("a numbered row");
77 let n: usize = n.parse().expect("a line number");
78 assert_eq!(lines[n - 1], t);
79 assert!(t.starts_with("impl Block"));
80 }
81 assert_eq!(o.lines, 20 * 302);
82}
83
84#[test]
85fn a_file_with_no_structure_has_no_outline() {
86 let log: String = (0..5000).map(|i| format!("{:05} event at the gate\n", i)).collect();
87 assert_eq!(of(&log, 4000), None);
88 let rows: String = (0..5000).map(|i| format!("row {}\n", i)).collect();
89 assert_eq!(of(&rows, 4000), None, "one entry is no outline");
90}