Skip to content

Commit

Permalink
Merge branch 'master' into custom-configuration-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad authored Dec 12, 2024
2 parents 8b89cbd + 3bc8c07 commit 9cbbdf7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
```rust
let boot = boot_test::<App>().await.unwrap();
```
* implement commands to manage background jobs. [https://github.com/loco-rs/loco/pull/1071](https://github.com/loco-rs/loco/pull/1071)


## v0.13.2

Expand Down
59 changes: 51 additions & 8 deletions src/testing/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ pub fn assert_count(html: &str, selector: &str, expected_count: usize) {
/// </ul>
/// </body>
/// </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"]);
/// ```
///
/// # 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();

Expand All @@ -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#"
/// <html>
/// <body>
/// <div class="item">Item 1</div>
/// <div class="item">Item 2</div>
/// <div class="item">Item 3</div>
/// </body>
/// </html>
/// "#;
/// let items = select(html, ".item");
/// assert_eq!(items, vec!["<div class=\"item\">Item 1</div>", "<div class=\"item\">Item 2</div>", "<div class=\"item\">Item 3</div>"]);
/// ```
///
/// # Panics
///
/// This function will panic when could not pase the selector
#[must_use]
pub fn select(html: &str, selector: &str) -> Vec<String> {
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 {
Expand Down Expand Up @@ -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());
Expand All @@ -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",
&[
Expand All @@ -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());
Expand All @@ -522,4 +555,14 @@ mod tests {
);
}
}

#[test]
fn test_select() {
let html = setup_test_html();
assert_eq!(
select(html, ".some-class"),
vec!["<div class=\"some-class\">Some content here</div>"]
);
assert_eq!(select(html, "ul"), vec!["<ul id=\"posts\">\n <li>Post 1</li>\n <li>Post 2</li>\n <li>Post 3</li>\n </ul>"]);
}
}

0 comments on commit 9cbbdf7

Please sign in to comment.