mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Add assert_set_eq macro to make test clearer
Co-authored-by: Max Brunsfeld <max@zed.dev>
This commit is contained in:
parent
32d2e5952c
commit
0dce371b3e
@ -4660,7 +4660,7 @@ mod tests {
|
||||
use serde_json::json;
|
||||
use std::{cell::RefCell, os::unix, path::PathBuf, rc::Rc};
|
||||
use unindent::Unindent as _;
|
||||
use util::test::temp_tree;
|
||||
use util::{assert_set_eq, test::temp_tree};
|
||||
use worktree::WorktreeHandle as _;
|
||||
|
||||
#[gpui::test]
|
||||
@ -4948,19 +4948,17 @@ mod tests {
|
||||
let mut fake_json_server = fake_json_servers.next().await.unwrap();
|
||||
|
||||
// Ensure both rust documents are reopened in new rust language server without worrying about order
|
||||
let mut opened_items = vec![
|
||||
fake_rust_server
|
||||
.receive_notification::<lsp::notification::DidOpenTextDocument>()
|
||||
.await
|
||||
.text_document,
|
||||
fake_rust_server
|
||||
.receive_notification::<lsp::notification::DidOpenTextDocument>()
|
||||
.await
|
||||
.text_document,
|
||||
];
|
||||
opened_items.sort_by_key(|item| item.uri.clone());
|
||||
assert_eq!(
|
||||
opened_items,
|
||||
assert_set_eq!(
|
||||
[
|
||||
fake_rust_server
|
||||
.receive_notification::<lsp::notification::DidOpenTextDocument>()
|
||||
.await
|
||||
.text_document,
|
||||
fake_rust_server
|
||||
.receive_notification::<lsp::notification::DidOpenTextDocument>()
|
||||
.await
|
||||
.text_document,
|
||||
],
|
||||
[
|
||||
lsp::TextDocumentItem {
|
||||
uri: lsp::Url::from_file_path("/the-root/test.rs").unwrap(),
|
||||
|
@ -1,10 +1,12 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ops::Range,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
mod assertions;
|
||||
mod marked_text;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use tempdir::TempDir;
|
||||
|
||||
pub use assertions::*;
|
||||
pub use marked_text::*;
|
||||
|
||||
pub fn temp_tree(tree: serde_json::Value) -> TempDir {
|
||||
let dir = TempDir::new("").unwrap();
|
||||
write_tree(dir.path(), tree);
|
||||
@ -52,44 +54,3 @@ pub fn sample_text(rows: usize, cols: usize, start_char: char) -> String {
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
pub fn marked_text_by(
|
||||
marked_text: &str,
|
||||
markers: Vec<char>,
|
||||
) -> (String, HashMap<char, Vec<usize>>) {
|
||||
let mut extracted_markers: HashMap<char, Vec<usize>> = Default::default();
|
||||
let mut unmarked_text = String::new();
|
||||
|
||||
for char in marked_text.chars() {
|
||||
if markers.contains(&char) {
|
||||
let char_offsets = extracted_markers.entry(char).or_insert(Vec::new());
|
||||
char_offsets.push(unmarked_text.len());
|
||||
} else {
|
||||
unmarked_text.push(char);
|
||||
}
|
||||
}
|
||||
|
||||
(unmarked_text, extracted_markers)
|
||||
}
|
||||
|
||||
pub fn marked_text(marked_text: &str) -> (String, Vec<usize>) {
|
||||
let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['|']);
|
||||
(unmarked_text, markers.remove(&'|').unwrap_or_else(Vec::new))
|
||||
}
|
||||
|
||||
pub fn marked_text_ranges(marked_text: &str) -> (String, Vec<Range<usize>>) {
|
||||
let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['[', ']']);
|
||||
let opens = markers.remove(&'[').unwrap_or_default();
|
||||
let closes = markers.remove(&']').unwrap_or_default();
|
||||
assert_eq!(opens.len(), closes.len(), "marked ranges are unbalanced");
|
||||
|
||||
let ranges = opens
|
||||
.into_iter()
|
||||
.zip(closes)
|
||||
.map(|(open, close)| {
|
||||
assert!(close >= open, "marked ranges must be disjoint");
|
||||
open..close
|
||||
})
|
||||
.collect();
|
||||
(unmarked_text, ranges)
|
||||
}
|
||||
|
19
crates/util/src/test/assertions.rs
Normal file
19
crates/util/src/test/assertions.rs
Normal file
@ -0,0 +1,19 @@
|
||||
#[macro_export]
|
||||
macro_rules! assert_set_eq {
|
||||
($left:expr,$right:expr) => {{
|
||||
let left = $left;
|
||||
let right = $right;
|
||||
|
||||
for left_value in left.iter() {
|
||||
if !right.contains(left_value) {
|
||||
panic!("assertion failed: `(left == right)`\n left: {:?}\nright: {:?}\nright does not contain {:?}", left, right, left_value);
|
||||
}
|
||||
}
|
||||
|
||||
for right_value in right.iter() {
|
||||
if !left.contains(right_value) {
|
||||
panic!("assertion failed: `(left == right)`\n left: {:?}\nright: {:?}\nleft does not contain {:?}", left, right, right_value);
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
42
crates/util/src/test/marked_text.rs
Normal file
42
crates/util/src/test/marked_text.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use std::{collections::HashMap, ops::Range};
|
||||
|
||||
pub fn marked_text_by(
|
||||
marked_text: &str,
|
||||
markers: Vec<char>,
|
||||
) -> (String, HashMap<char, Vec<usize>>) {
|
||||
let mut extracted_markers: HashMap<char, Vec<usize>> = Default::default();
|
||||
let mut unmarked_text = String::new();
|
||||
|
||||
for char in marked_text.chars() {
|
||||
if markers.contains(&char) {
|
||||
let char_offsets = extracted_markers.entry(char).or_insert(Vec::new());
|
||||
char_offsets.push(unmarked_text.len());
|
||||
} else {
|
||||
unmarked_text.push(char);
|
||||
}
|
||||
}
|
||||
|
||||
(unmarked_text, extracted_markers)
|
||||
}
|
||||
|
||||
pub fn marked_text(marked_text: &str) -> (String, Vec<usize>) {
|
||||
let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['|']);
|
||||
(unmarked_text, markers.remove(&'|').unwrap_or_else(Vec::new))
|
||||
}
|
||||
|
||||
pub fn marked_text_ranges(marked_text: &str) -> (String, Vec<Range<usize>>) {
|
||||
let (unmarked_text, mut markers) = marked_text_by(marked_text, vec!['[', ']']);
|
||||
let opens = markers.remove(&'[').unwrap_or_default();
|
||||
let closes = markers.remove(&']').unwrap_or_default();
|
||||
assert_eq!(opens.len(), closes.len(), "marked ranges are unbalanced");
|
||||
|
||||
let ranges = opens
|
||||
.into_iter()
|
||||
.zip(closes)
|
||||
.map(|(open, close)| {
|
||||
assert!(close >= open, "marked ranges must be disjoint");
|
||||
open..close
|
||||
})
|
||||
.collect();
|
||||
(unmarked_text, ranges)
|
||||
}
|
Loading…
Reference in New Issue
Block a user