-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrust_match_struct.rs
95 lines (91 loc) · 2.4 KB
/
rust_match_struct.rs
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
use codesort::*;
static INPUT: &str = r#"
pub fn on_event(
&mut self,
w: &mut W,
timed_event: TimedEvent,
con: &AppContext,
) -> Result<Command, ProgramError> {
let cmd = match timed_event {
// a key event
TimedEvent {
key_combination: Some(key),
..
} => {
self.on_key(timed_event, key, con)
}
// an event
// (with a mouse)
TimedEvent { // a tricky comment
event: Event::Mouse(MouseEvent { kind, column, row }),
..
} => { // inner block
self.on_mouse(timed_event, kind, column, row)
}
// anything else
_ => Command::None,
};
self.input_field.display_on(w)?;
Ok(cmd)
}
"#;
static OUTPUT: &str = r#"
pub fn on_event(
&mut self,
w: &mut W,
timed_event: TimedEvent,
con: &AppContext,
) -> Result<Command, ProgramError> {
let cmd = match timed_event {
// an event
// (with a mouse)
TimedEvent { // a tricky comment
event: Event::Mouse(MouseEvent { kind, column, row }),
..
} => { // inner block
self.on_mouse(timed_event, kind, column, row)
}
// a key event
TimedEvent {
key_combination: Some(key),
..
} => {
self.on_key(timed_event, key, con)
}
// anything else
_ => Command::None,
};
self.input_field.display_on(w)?;
Ok(cmd)
}
"#;
#[test]
fn test_match_struct_ranges() {
let list = LocList::read_str(INPUT, Language::Rust).unwrap();
list.print_debug("TOTAL");
let inner_range = list.range_around_line_index(15).unwrap();
let blocks = list.block_ranges_in_range(inner_range);
assert_eq!(
blocks,
vec![
"9:15".parse().unwrap(),
"16:23".parse().unwrap(),
"24:25".parse().unwrap(),
]
);
}
#[test]
fn test_match_struct() {
let list = LocList::read_str(INPUT, Language::Rust).unwrap();
let focused = list.focus_around_line_index(17).unwrap();
focused.print_debug();
{
let blocks = focused.clone().focus.into_blocks();
for (i, block) in blocks.iter().enumerate() {
block.print_debug(&format!(" BLOCK {i}"));
}
}
let sorted_list = focused.sort();
sorted_list.print_debug(" SORTED ");
assert_eq!(sorted_list.to_string(), OUTPUT);
}