aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor/tests/test.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-12 16:50:16 +0100
committerAleksey Kladov <[email protected]>2018-08-12 16:50:16 +0100
commit66be735aa98c32fb062d1c756fa9303ff2d13002 (patch)
treed679bef9b4005f969cfa5a369c6804195de6c779 /crates/libeditor/tests/test.rs
parent56aa6e20e0279c69e0130905573b1607056cfaf9 (diff)
flip comma
Diffstat (limited to 'crates/libeditor/tests/test.rs')
-rw-r--r--crates/libeditor/tests/test.rs52
1 files changed, 45 insertions, 7 deletions
diff --git a/crates/libeditor/tests/test.rs b/crates/libeditor/tests/test.rs
index dedca49a4..369854fed 100644
--- a/crates/libeditor/tests/test.rs
+++ b/crates/libeditor/tests/test.rs
@@ -1,9 +1,16 @@
1extern crate libeditor; 1extern crate libeditor;
2extern crate libsyntax2;
2extern crate itertools; 3extern crate itertools;
4#[macro_use]
5extern crate assert_eq_text;
3 6
4use std::fmt; 7use std::fmt;
5use itertools::Itertools; 8use itertools::Itertools;
6use libeditor::{File, highlight, runnables, extend_selection, TextRange, file_symbols}; 9use libsyntax2::AstNode;
10use libeditor::{
11 File, TextUnit, TextRange,
12 highlight, runnables, extend_selection, file_symbols, flip_comma,
13};
7 14
8#[test] 15#[test]
9fn test_extend_selection() { 16fn test_extend_selection() {
@@ -27,13 +34,13 @@ fn main() {}
27"#); 34"#);
28 let hls = highlight(&file); 35 let hls = highlight(&file);
29 dbg_eq( 36 dbg_eq(
30 &hls,
31 r#"[HighlightedRange { range: [1; 11), tag: "comment" }, 37 r#"[HighlightedRange { range: [1; 11), tag: "comment" },
32 HighlightedRange { range: [12; 14), tag: "keyword" }, 38 HighlightedRange { range: [12; 14), tag: "keyword" },
33 HighlightedRange { range: [15; 19), tag: "function" }, 39 HighlightedRange { range: [15; 19), tag: "function" },
34 HighlightedRange { range: [29; 36), tag: "text" }, 40 HighlightedRange { range: [29; 36), tag: "text" },
35 HighlightedRange { range: [38; 50), tag: "string" }, 41 HighlightedRange { range: [38; 50), tag: "string" },
36 HighlightedRange { range: [52; 54), tag: "literal" }]"# 42 HighlightedRange { range: [52; 54), tag: "literal" }]"#,
43 &hls,
37 ); 44 );
38} 45}
39 46
@@ -51,10 +58,10 @@ fn test_foo() {}
51"#); 58"#);
52 let runnables = runnables(&file); 59 let runnables = runnables(&file);
53 dbg_eq( 60 dbg_eq(
54 &runnables,
55 r#"[Runnable { range: [1; 13), kind: Bin }, 61 r#"[Runnable { range: [1; 13), kind: Bin },
56 Runnable { range: [15; 39), kind: Test { name: "test_foo" } }, 62 Runnable { range: [15; 39), kind: Test { name: "test_foo" } },
57 Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#, 63 Runnable { range: [41; 75), kind: Test { name: "test_foo" } }]"#,
64 &runnables,
58 ) 65 )
59} 66}
60 67
@@ -76,7 +83,6 @@ const C: i32 = 92;
76"#); 83"#);
77 let symbols = file_symbols(&file); 84 let symbols = file_symbols(&file);
78 dbg_eq( 85 dbg_eq(
79 &symbols,
80 r#"[FileSymbol { parent: None, name: "Foo", name_range: [8; 11), node_range: [1; 26), kind: STRUCT }, 86 r#"[FileSymbol { parent: None, name: "Foo", name_range: [8; 11), node_range: [1; 26), kind: STRUCT },
81 FileSymbol { parent: None, name: "m", name_range: [32; 33), node_range: [28; 53), kind: MODULE }, 87 FileSymbol { parent: None, name: "m", name_range: [32; 33), node_range: [28; 53), kind: MODULE },
82 FileSymbol { parent: Some(1), name: "bar", name_range: [43; 46), node_range: [40; 51), kind: FUNCTION }, 88 FileSymbol { parent: Some(1), name: "bar", name_range: [43; 46), node_range: [40; 51), kind: FUNCTION },
@@ -84,6 +90,19 @@ const C: i32 = 92;
84 FileSymbol { parent: None, name: "T", name_range: [81; 82), node_range: [76; 88), kind: TYPE_ITEM }, 90 FileSymbol { parent: None, name: "T", name_range: [81; 82), node_range: [76; 88), kind: TYPE_ITEM },
85 FileSymbol { parent: None, name: "S", name_range: [96; 97), node_range: [89; 108), kind: STATIC_ITEM }, 91 FileSymbol { parent: None, name: "S", name_range: [96; 97), node_range: [89; 108), kind: STATIC_ITEM },
86 FileSymbol { parent: None, name: "C", name_range: [115; 116), node_range: [109; 127), kind: CONST_ITEM }]"#, 92 FileSymbol { parent: None, name: "C", name_range: [115; 116), node_range: [109; 127), kind: CONST_ITEM }]"#,
93 &symbols,
94 )
95}
96
97#[test]
98fn test_swap_comma() {
99 check_modification(
100 "fn foo(x: i32,<|> y: Result<(), ()>) {}",
101 "fn foo(y: Result<(), ()>, x: i32) {}",
102 &|file, offset| {
103 let edit = flip_comma(file, offset).unwrap()();
104 edit.apply(&file.syntax().text())
105 },
87 ) 106 )
88} 107}
89 108
@@ -91,8 +110,27 @@ fn file(text: &str) -> File {
91 File::parse(text) 110 File::parse(text)
92} 111}
93 112
94fn dbg_eq(actual: &impl fmt::Debug, expected: &str) { 113fn dbg_eq(expected: &str, actual: &impl fmt::Debug) {
95 let actual = format!("{:?}", actual); 114 let actual = format!("{:?}", actual);
96 let expected = expected.lines().map(|l| l.trim()).join(" "); 115 let expected = expected.lines().map(|l| l.trim()).join(" ");
97 assert_eq!(actual, expected); 116 assert_eq!(expected, actual);
117}
118
119fn check_modification(
120 before: &str,
121 after: &str,
122 f: &impl Fn(&File, TextUnit) -> String,
123) {
124 let cursor = "<|>";
125 let cursor_pos = match before.find(cursor) {
126 None => panic!("before text should contain cursor marker"),
127 Some(pos) => pos,
128 };
129 let mut text = String::with_capacity(before.len() - cursor.len());
130 text.push_str(&before[..cursor_pos]);
131 text.push_str(&before[cursor_pos + cursor.len()..]);
132 let cursor_pos = TextUnit::from(cursor_pos as u32);
133 let file = file(&text);
134 let actual = f(&file, cursor_pos);
135 assert_eq_text!(after, &actual);
98} 136}