From e847a1b7a6b53d5663aae7396ea0f65485f1283e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Aug 2018 12:47:34 +0300 Subject: hihglihgitng tests --- libeditor/Cargo.toml | 1 + libeditor/src/lib.rs | 20 +------------------- libeditor/tests/test.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 libeditor/tests/test.rs diff --git a/libeditor/Cargo.toml b/libeditor/Cargo.toml index 92abd3289..750d80038 100644 --- a/libeditor/Cargo.toml +++ b/libeditor/Cargo.toml @@ -6,3 +6,4 @@ publish = false [dependencies] libsyntax2 = { path = "../" } +itertools = "0.7.8" diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs index a0c003fb5..ef31efe35 100644 --- a/libeditor/src/lib.rs +++ b/libeditor/src/lib.rs @@ -13,6 +13,7 @@ pub struct File { inner: libsyntax2::File } +#[derive(Debug)] pub struct HighlightedRange { pub range: TextRange, pub tag: &'static str, @@ -102,22 +103,3 @@ impl<'f> Declaration<'f> { self.0.range() } } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_extend_selection() { - let text = r#"fn foo() { - 1 + 1 -} -"#; - let file = File::new(text); - let range = TextRange::offset_len(18.into(), 0.into()); - let range = file.extend_selection(range).unwrap(); - assert_eq!(range, TextRange::from_to(17.into(), 18.into())); - let range = file.extend_selection(range).unwrap(); - assert_eq!(range, TextRange::from_to(15.into(), 20.into())); - } -} diff --git a/libeditor/tests/test.rs b/libeditor/tests/test.rs new file mode 100644 index 000000000..e1c8aee4b --- /dev/null +++ b/libeditor/tests/test.rs @@ -0,0 +1,47 @@ +extern crate libeditor; +extern crate itertools; + +use std::fmt; +use itertools::Itertools; +use libeditor::{File, TextRange}; + +#[test] +fn test_extend_selection() { + let file = file(r#"fn foo() { + 1 + 1 +} +"#); + let range = TextRange::offset_len(18.into(), 0.into()); + let range = file.extend_selection(range).unwrap(); + assert_eq!(range, TextRange::from_to(17.into(), 18.into())); + let range = file.extend_selection(range).unwrap(); + assert_eq!(range, TextRange::from_to(15.into(), 20.into())); +} + +#[test] +fn test_highlighting() { + let file = file(r#" +// comment +fn main() {} + println!("Hello, {}!", 92); +"#); + let hls = file.highlight(); + dbg_eq( + &hls, + r#"[HighlightedRange { range: [1; 11), tag: "comment" }, + HighlightedRange { range: [12; 14), tag: "keyword" }, + HighlightedRange { range: [15; 19), tag: "function" }, + HighlightedRange { range: [29; 36), tag: "text" }, + HighlightedRange { range: [38; 50), tag: "string" }, + HighlightedRange { range: [52; 54), tag: "literal" }]"# + ); +} + +fn file(text: &str) -> File { + File::new(text) +} + +fn dbg_eq(actual: &impl fmt::Debug, expected: &str) { + let expected = expected.lines().map(|l| l.trim()).join(" "); + assert_eq!(actual, expected); +} -- cgit v1.2.3