use proptest::prelude::*;
use super::*;
#[test]
fn decimals_are_signed_decimal_text() {
for text in ["0", "-0.0015", "12.5", "-5"] {
assert_eq!(text.parse::<Decimal>().unwrap().as_str(), text);
}
for bad in ["", "-", ".5", "1.", "+1", "1e3", "1,5", "--1"] {
assert_eq!(bad.parse::<Decimal>(), Err(ParseDecimalError), "{bad:?}");
}
assert!(serde_json::from_str::<Decimal>("1.5").is_err(), "a JSON number");
}
#[test]
fn price_bounds() {
assert!("0.89999999".parse::<Price>().is_err());
assert_eq!("0.9".parse::<Price>().unwrap().units(), 90_000_000);
assert_eq!("1.1".parse::<Price>().unwrap().units(), 110_000_000);
assert!("1.10000001".parse::<Price>().is_err());
assert_eq!("1".parse::<Price>().unwrap().units(), 100_000_000);
assert_eq!("1.0005".parse::<Price>().unwrap().units(), 100_050_000);
assert_eq!(Price::from_units(89_999_999), None);
assert_eq!(Price::from_units(110_000_001), None);
}
#[test]
fn price_refuses_off_pattern_text() {
for text in ["1.", ".9", "01", "1.000000001", "0.9x", "+1", "1,0", "", "2", "0.95e0"] {
assert!(text.parse::<Price>().is_err(), "{text:?}");
}
}
#[test]
fn price_wire_form() {
let price: Price = serde_json::from_str("\"0.99950000\"").unwrap();
assert_eq!(price.to_string(), "0.9995");
assert_eq!(serde_json::to_string(&price).unwrap(), "\"0.9995\"");
assert_eq!(serde_json::to_string(&Price::from_units(100_000_000).unwrap()).unwrap(), "\"1\"");
assert!(serde_json::from_str::<Price>("1").is_err());
}
proptest! {
#[test]
fn price_text_round_trips(units in PRICE_MIN..=PRICE_MAX) {
let price = Price::from_units(units).unwrap();
prop_assert_eq!(price.to_string().parse::<Price>().unwrap(), price);
}
}
#[test]
fn bps_bounds() {
assert_eq!(Bps::new(10_000).map(Bps::get), Some(10_000));
assert_eq!(Bps::new(10_001), None);
assert_eq!(serde_json::to_string(&Bps::new(25).unwrap()).unwrap(), "25");
assert_eq!(serde_json::from_str::<Bps>("100").unwrap().get(), 100);
assert!(serde_json::from_str::<Bps>("10001").is_err());
}
#[test]
fn max_parts_bounds() {
assert_eq!(MaxParts::new(0), None);
assert_eq!(MaxParts::new(1).map(MaxParts::get), Some(1));
assert_eq!(MaxParts::new(8).map(MaxParts::get), Some(8));
assert_eq!(MaxParts::new(9), None);
assert_eq!(MaxParts::default().get(), 8);
assert_eq!(serde_json::to_string(&MaxParts::default()).unwrap(), "8");
assert!(serde_json::from_str::<MaxParts>("0").is_err());
assert!(serde_json::from_str::<MaxParts>("9").is_err());
}