use super::*;
#[test]
fn round_trip_preserves_a_label_this_build_has_never_seen() {
for label in ["A", "F", "A+", "A+++", "CLASS-1"] {
let json = serde_json::to_string(&CarbonFootprintClass::new(label).unwrap()).unwrap();
assert_eq!(json, format!("\"{label}\""));
let back: CarbonFootprintClass = serde_json::from_str(&json).unwrap();
assert_eq!(back.as_str(), label, "label must survive the round trip");
}
}
#[test]
fn deserialization_validates_rather_than_silently_accepting() {
for bad in ["\"\"", "\" \"", "\"TOOLONGLABEL\""] {
assert!(
serde_json::from_str::<CarbonFootprintClass>(bad).is_err(),
"should reject {bad}"
);
}
}
#[test]
fn rejects_empty_blank_overlong_and_control_characters() {
assert!(matches!(
CarbonFootprintClass::new(""),
Err(CarbonFootprintClassError::Empty)
));
assert!(matches!(
CarbonFootprintClass::new(" \t "),
Err(CarbonFootprintClassError::Empty)
));
assert!(matches!(
CarbonFootprintClass::new("A\u{7}"),
Err(CarbonFootprintClassError::ControlCharacter(_))
));
let err = CarbonFootprintClass::new("ABCDEFGHI").unwrap_err();
assert!(matches!(
err,
CarbonFootprintClassError::TooLong { len: 9, max: 8, .. }
));
}
#[test]
fn length_bound_matches_the_schema_and_counts_characters() {
assert_eq!(CarbonFootprintClass::MAX_LEN, 8);
assert!(CarbonFootprintClass::new("ABCDEFGH").is_ok());
assert!(CarbonFootprintClass::new("Ä+++").is_ok());
}
#[test]
fn label_is_stored_verbatim_without_case_folding() {
let c = CarbonFootprintClass::new("a+").unwrap();
assert_eq!(c.as_str(), "a+");
assert_eq!(c.to_string(), "a+");
}