use super::dom_with;
use crate::{TuiAccessors, TuiAccessorsMut};
#[test]
fn set_property_writes_inline_style_field() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property("color", "red")
.unwrap();
assert_eq!(
dom.node(div).style().unwrap().get_property_value("color"),
"red"
);
}
#[test]
fn set_property_writes_style_attribute_atomically() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property("color", "red")
.unwrap();
assert_eq!(
dom.node(div).style().unwrap().get_property_value("color"),
"red"
);
let attr = dom.node(div).get_attribute("style").unwrap_or("");
assert!(
attr.contains("color: red"),
"style attribute should contain `color: red`, got {attr:?}"
);
}
#[test]
fn set_property_invalid_value_is_silent_no_op() {
let (mut dom, div) = dom_with("div");
let r = dom
.node_mut(div)
.style_mut()
.unwrap()
.set_property("color", "not-a-color");
assert!(r.is_ok());
assert_eq!(
dom.node(div).style().unwrap().get_property_value("color"),
"",
"inline_style.fg should be unset after invalid value"
);
assert_eq!(dom.node(div).get_attribute("style"), None);
}
#[test]
fn set_property_unknown_name_is_silent_no_op() {
let (mut dom, div) = dom_with("div");
let r = dom
.node_mut(div)
.style_mut()
.unwrap()
.set_property("not-a-property", "x");
assert!(r.is_ok());
assert_eq!(dom.node(div).get_attribute("style"), None);
}
#[test]
fn set_property_clears_prior_important_bit() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property_important("color", "red")
.unwrap();
assert_eq!(
dom.node(div)
.style()
.unwrap()
.get_property_priority("color"),
"important"
);
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property("color", "blue")
.unwrap();
assert_eq!(
dom.node(div)
.style()
.unwrap()
.get_property_priority("color"),
""
);
}
#[test]
fn set_property_important_raises_priority() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property_important("color", "red")
.unwrap();
let style = dom.node(div).style().unwrap();
assert_eq!(style.get_property_value("color"), "red");
assert_eq!(style.get_property_priority("color"), "important");
assert!(style.css_text().contains("!important"));
}
#[test]
fn set_property_important_writes_attribute_with_marker() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property_important("color", "red")
.unwrap();
let attr = dom.node(div).get_attribute("style").unwrap_or("");
assert!(
attr.contains("!important"),
"style attribute should include !important, got {attr:?}"
);
}
#[test]
fn remove_property_returns_previous_value() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_property("color", "red")
.unwrap();
let prev = dom
.node_mut(div)
.style_mut()
.unwrap()
.remove_property("color")
.unwrap();
assert_eq!(prev, "red");
assert_eq!(
dom.node(div)
.style()
.unwrap()
.get_property_priority("color"),
""
);
}
#[test]
fn remove_property_clears_field_and_important_bit() {
let (mut dom, div) = dom_with("div");
{
let mut nm = dom.node_mut(div);
let mut sd = nm.style_mut().unwrap();
sd.set_property_important("color", "red").unwrap();
sd.remove_property("color").unwrap();
}
let style = dom.node(div).style().unwrap();
assert_eq!(style.get_property_value("color"), "");
assert_eq!(style.get_property_priority("color"), "");
}
#[test]
fn remove_property_unset_returns_empty_string() {
let (mut dom, div) = dom_with("div");
let prev = dom
.node_mut(div)
.style_mut()
.unwrap()
.remove_property("color")
.unwrap();
assert_eq!(prev, "");
}
#[test]
fn remove_property_unknown_returns_empty_string() {
let (mut dom, div) = dom_with("div");
let prev = dom
.node_mut(div)
.style_mut()
.unwrap()
.remove_property("bogus")
.unwrap();
assert_eq!(prev, "");
}
#[test]
fn set_css_text_replaces_entire_declaration() {
let (mut dom, div) = dom_with("div");
{
let mut nm = dom.node_mut(div);
let mut sd = nm.style_mut().unwrap();
sd.set_property("color", "red").unwrap();
sd.set_css_text("display: block; gap: 2").unwrap();
}
let style = dom.node(div).style().unwrap();
assert_eq!(style.get_property_value("color"), "");
assert_eq!(style.get_property_value("display"), "block");
assert_eq!(style.get_property_value("gap"), "2");
}
#[test]
fn set_css_text_serializes_back_to_attribute() {
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.set_css_text("color: red; gap: 3")
.unwrap();
let attr = dom.node(div).get_attribute("style").unwrap_or("");
assert!(attr.contains("color: red"));
assert!(attr.contains("gap: 3"));
}
#[test]
fn try_set_property_returns_unknown_property() {
use crate::cssom::{DispatchError, SetPropertyError};
let (mut dom, div) = dom_with("div");
let mut nm = dom.node_mut(div);
let r = nm.style_mut().unwrap().try_set_property("colour", "red");
assert!(matches!(
r,
Err(SetPropertyError::Parse(DispatchError::UnknownProperty))
));
}
#[test]
fn try_set_property_returns_invalid_value() {
use crate::cssom::{DispatchError, SetPropertyError};
let (mut dom, div) = dom_with("div");
let mut nm = dom.node_mut(div);
let r = nm
.style_mut()
.unwrap()
.try_set_property("color", "not-a-color");
assert!(matches!(
r,
Err(SetPropertyError::Parse(DispatchError::InvalidValue))
));
}
#[test]
fn try_set_property_succeeds_on_valid_input() {
let (mut dom, div) = dom_with("div");
{
let mut nm = dom.node_mut(div);
nm.style_mut()
.unwrap()
.try_set_property("color", "red")
.unwrap();
}
assert_eq!(
dom.node(div).style().unwrap().get_property_value("color"),
"red"
);
}
#[test]
fn try_set_property_important_raises_priority_and_surfaces_errors() {
use crate::cssom::{DispatchError, SetPropertyError};
let (mut dom, div) = dom_with("div");
dom.node_mut(div)
.style_mut()
.unwrap()
.try_set_property_important("color", "red")
.unwrap();
assert_eq!(
dom.node(div)
.style()
.unwrap()
.get_property_priority("color"),
"important"
);
let r = dom
.node_mut(div)
.style_mut()
.unwrap()
.try_set_property_important("colour", "red");
assert!(matches!(
r,
Err(SetPropertyError::Parse(DispatchError::UnknownProperty))
));
}
#[test]
fn set_property_remains_silent_on_parse_failure() {
let (mut dom, div) = dom_with("div");
let r = dom
.node_mut(div)
.style_mut()
.unwrap()
.set_property("colour", "red");
assert!(r.is_ok(), "set_property must silently swallow parse errors");
assert_eq!(
dom.node(div).style().unwrap().get_property_value("color"),
""
);
}