Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152use luafeed::run_method::run_method;
const SOURCE: &str = r#"local root = ...local items = {}for _, a in ipairs(root:find_all("a")) do local href = a:attr("href") local text = a:text() if href and href ~= "" and text and text ~= "" then table.insert(items, { title = text, link = href }) endendreturn items"#;
const HTML: &str = r#"<html><body> <a href="/one">Post One</a> <a href="/two">Post Two</a> <a href="">Empty href, skipped</a> <span>Not a link</span></body></html>"#;
#[test]fn extracts_items_via_dom_traversal() { let items = run_method(SOURCE, "https://example.com/", HTML).unwrap(); assert_eq!(items.len(), 2); assert_eq!(items[0].title, "Post One"); assert_eq!(items[0].link, "/one"); assert_eq!(items[1].title, "Post Two"); assert_eq!(items[1].link, "/two");}
#[test]fn missing_required_field_is_a_clear_error() { let source = r#" local root = ... return { { title = "No link here" } } "#; let err = run_method(source, "https://example.com/", HTML).unwrap_err(); assert!(err.to_string().contains("link"));}
#[test]fn script_receives_the_fetch_url_as_a_second_argument() { let source = r#" local root, url = ... return { { title = "t", link = url } } "#; let items = run_method(source, "https://example.com/", HTML).unwrap(); assert_eq!(items[0].link, "https://example.com/");}