#![forbid(unsafe_code)]
#![warn(missing_docs, clippy::pedantic)]
#![allow(clippy::too_many_lines, clippy::match_same_arms)]
cefact_units_macros::cefact_units!("rec20.xlsx");
#[cfg(test)]
mod tests {
use crate::{UnitCode, UnitOfMeasure};
#[test]
fn basic_lookup() {
let kg = UnitOfMeasure::from_code("KGM").unwrap();
assert_eq!(kg.name(), "kilogram");
assert_eq!(kg.symbol(), Some("kg"));
}
#[test]
fn from_str_parses() {
let parsed: UnitOfMeasure = "MTR".parse().unwrap();
assert_eq!(parsed.code(), "MTR");
}
#[test]
fn unknown_code_fails() {
assert!(UnitOfMeasure::from_code("INVALID").is_none());
assert!("INVALID".parse::<UnitOfMeasure>().is_err());
}
#[test]
fn unit_code_roundtrip() {
let code: UnitCode = "KGM".parse().unwrap();
assert_eq!(code.as_str(), "KGM");
let unit = UnitOfMeasure::from_unit_code(code);
assert_eq!(unit.unit_code(), code);
}
#[cfg(feature = "case-insensitive")]
#[test]
fn case_insensitive_lookup() {
let kg = UnitOfMeasure::from_code_ignore_case("kgm").unwrap();
assert_eq!(kg.code(), "KGM");
let parsed: UnitOfMeasure = "mtr".parse().unwrap();
assert_eq!(parsed.code(), "MTR");
}
#[cfg(feature = "serde")]
#[test]
fn serde_roundtrip_for_unit_of_measure() {
let unit = UnitOfMeasure::from_code("KGM").unwrap();
let encoded = serde_json::to_string(&unit).unwrap();
assert_eq!(encoded, "\"KGM\"");
let decoded: UnitOfMeasure = serde_json::from_str(&encoded).unwrap();
assert_eq!(decoded, unit);
}
#[cfg(feature = "serde")]
#[test]
fn serde_roundtrip_for_unit_code() {
let code: UnitCode = "MTR".parse().unwrap();
let encoded = serde_json::to_string(&code).unwrap();
assert_eq!(encoded, "\"MTR\"");
let decoded: UnitCode = serde_json::from_str(&encoded).unwrap();
assert_eq!(decoded, code);
}
}