use 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 }) end end return items "#; const HTML: &str = r#" Post One Post Two Empty href, skipped Not a link "#; #[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/"); }