From bc68c44b2ff23146de94dd68e2a860a4f6652573 Mon Sep 17 00:00:00 2001 From: Elad Kaplan Date: Thu, 12 Dec 2024 12:31:31 +0200 Subject: [PATCH] rename function + adding selector function (#1080) * rename function + adding selector function * fix test --- src/testing/selector.rs | 59 +++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/src/testing/selector.rs b/src/testing/selector.rs index b5d4178ab..aa1a82350 100644 --- a/src/testing/selector.rs +++ b/src/testing/selector.rs @@ -273,14 +273,14 @@ pub fn assert_count(html: &str, selector: &str, expected_count: usize) { /// /// /// "#; -/// assert_collect_text(html, "ul#posts li", &["Post 1", "Post 2", "Post 3"]); +/// assert_css_eq_list(html, "ul#posts li", &["Post 1", "Post 2", "Post 3"]); /// ``` /// /// # Panics /// /// This function will panic if the text content of the elements does not match /// the expected values. -pub fn assert_collect_text(html: &str, selector: &str, expected_texts: &[&str]) { +pub fn assert_css_eq_list(html: &str, selector: &str, expected_texts: &[&str]) { let document = Html::parse_document(html); let parsed_selector = Selector::parse(selector).unwrap(); @@ -295,6 +295,39 @@ pub fn assert_collect_text(html: &str, selector: &str, expected_texts: &[&str]) ); } +/// Parses the given HTML string and selects the elements matching the specified CSS selector. +/// +/// # Examples +/// +/// ```rust +/// use loco_rs::testing::prelude::*; +/// +/// let html = r#" +/// +/// +///
Item 1
+///
Item 2
+///
Item 3
+/// +/// +/// "#; +/// let items = select(html, ".item"); +/// assert_eq!(items, vec!["
Item 1
", "
Item 2
", "
Item 3
"]); +/// ``` +/// +/// # Panics +/// +/// This function will panic when could not pase the selector +#[must_use] +pub fn select(html: &str, selector: &str) -> Vec { + let document = Html::parse_document(html); + let parsed_selector = Selector::parse(selector).unwrap(); + document + .select(&parsed_selector) + .map(|element| element.html()) + .collect() +} + // Test cases #[cfg(test)] mod tests { @@ -475,12 +508,12 @@ mod tests { } #[test] - fn test_assert_collect_text() { + fn test_assert_css_eq_list() { let html = setup_test_html(); - assert_collect_text(html, "ul#posts li", &["Post 1", "Post 2", "Post 3"]); + assert_css_eq_list(html, "ul#posts li", &["Post 1", "Post 2", "Post 3"]); let result = std::panic::catch_unwind(|| { - assert_collect_text(html, "ul#posts li", &["Post 1", "Post 2", "Wrong Post"]); + assert_css_eq_list(html, "ul#posts li", &["Post 1", "Post 2", "Wrong Post"]); }); assert!(result.is_err()); @@ -497,9 +530,9 @@ mod tests { } #[test] - fn test_assert_collect_text_table() { + fn test_assert_css_eq_list_table() { let html = setup_test_html(); - assert_collect_text( + assert_css_eq_list( html, "table tr td", &[ @@ -508,7 +541,7 @@ mod tests { ); let result = std::panic::catch_unwind(|| { - assert_collect_text(html, "table#posts_t tr td", &["Post 1", "Post 2", "Post 3"]); + assert_css_eq_list(html, "table#posts_t tr td", &["Post 1", "Post 2", "Post 3"]); }); assert!(result.is_err()); @@ -522,4 +555,14 @@ mod tests { ); } } + + #[test] + fn test_select() { + let html = setup_test_html(); + assert_eq!( + select(html, ".some-class"), + vec!["
Some content here
"] + ); + assert_eq!(select(html, "ul"), vec![""]); + } }