use ordered_float::OrderedFloat;
use skyscraper::{
html,
xpath::{
self,
grammar::data_model::{AnyAtomicType, XpathItem},
},
};
#[test]
fn cast_integer_to_string() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse("42 cast as string").unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::String(String::from("42")))
);
}
#[test]
fn cast_string_to_integer() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse(r#""123" cast as integer"#).unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Integer(123))
);
}
#[test]
fn cast_integer_to_double() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse("42 cast as double").unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Double(OrderedFloat(42.0)))
);
}
#[test]
fn cast_boolean_to_integer() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse("(1 > 0) cast as integer").unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Integer(1))
);
}
#[test]
fn cast_string_to_boolean() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse(r#""true" cast as boolean"#).unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Boolean(true))
);
}
#[test]
fn cast_empty_with_question_mark() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse("//nonexistent cast as integer?").unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 0);
}
#[test]
fn cast_invalid_string_to_integer_fails() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse(r#""abc" cast as integer"#).unwrap();
let result = xpath.apply(&document);
assert!(result.is_err());
}
#[test]
fn cast_no_clause_returns_base() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath = xpath::parse("42").unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Integer(42))
);
}
#[test]
fn cast_uri_qualified_string() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath =
xpath::parse(r#"42 cast as Q{http://www.w3.org/2001/XMLSchema}string"#).unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::String(String::from("42")))
);
}
#[test]
fn cast_uri_qualified_integer() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath =
xpath::parse(r#""99" cast as Q{http://www.w3.org/2001/XMLSchema}integer"#).unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Integer(99))
);
}
#[test]
fn cast_uri_qualified_double() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath =
xpath::parse(r#"42 cast as Q{http://www.w3.org/2001/XMLSchema}double"#).unwrap();
let items = xpath.apply(&document).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(
items[0],
XpathItem::AnyAtomicType(AnyAtomicType::Double(OrderedFloat(42.0)))
);
}
#[test]
fn cast_uri_qualified_unsupported_namespace_fails() {
let text = r#"<html><body></body></html>"#;
let document = html::parse(text).unwrap();
let xpath =
xpath::parse(r#"42 cast as Q{http://example.com/types}string"#).unwrap();
let result = xpath.apply(&document);
assert!(result.is_err());
}