use blather::{Error, Params, Telegram}; #[test] fn simple() { let mut tg = Telegram::new("SomeTopic"); assert_eq!(tg.get_topic(), "SomeTopic"); tg.add_str("Foo", "bar").unwrap(); assert_eq!(tg.get_str("Foo").unwrap(), "bar"); assert_eq!(tg.get_str("Moo"), None); } #[test] fn exist() { let mut tg = Telegram::new("SomeTopic"); tg.add_str("foo", "bar").unwrap(); assert!(tg.have_param("foo")); assert!(!tg.have_param("nonexistent")); } #[test] fn integer() { let mut tg = Telegram::new("SomeTopic"); assert_eq!(tg.get_topic(), "SomeTopic"); tg.add_str("Num", "64").unwrap(); assert_eq!(tg.get_int::("Num").unwrap(), 64); } #[test] fn size() { let mut tg = Telegram::new("SomeTopic"); tg.add_param("Num", 7_usize).unwrap(); assert_eq!(tg.get_int::("Num").unwrap(), 7); } #[test] fn intoparams() { let mut tg = Telegram::new("SomeTopic"); tg.add_str("Foo", "bar").unwrap(); assert_eq!(tg.get_str("Foo").unwrap(), "bar"); assert_eq!(tg.get_str("Moo"), None); let params = tg.into_params(); let val = params.get_str("Foo"); assert_eq!(val.unwrap(), "bar"); } #[test] fn display() { let mut tg = Telegram::new("hello"); tg.add_param("foo", "bar").unwrap(); let s = format!("{tg}"); assert_eq!(s, "hello:{foo=bar}"); } #[test] fn ser_size() { let mut tg = Telegram::new("hello"); tg.add_str("foo", "bar").unwrap(); tg.add_str("moo", "cow").unwrap(); let sz = tg.calc_buf_size(); assert_eq!(sz, 6 + 8 + 8 + 1); } #[test] fn def_int() { let mut tg = Telegram::new("SomeTopic"); tg.add_str("Num", "11").unwrap(); assert_eq!(tg.get_int_def::("Num", 17).unwrap(), 11); let num = tg.get_int_def::("nonexistent", 42).unwrap(); assert_eq!(num, 42); } #[test] fn bad_topic_leading() { let mut tg = Telegram::new("Hello"); let Err(Error::BadFormat(msg)) = tg.set_topic(" SomeTopic") else { panic!("Unexpectedly not Error::BadFormat"); }; assert_eq!(msg, "Invalid leading topic character"); } #[test] fn bad_topic() { let mut tg = Telegram::new("Hello"); let Err(Error::BadFormat(msg)) = tg.set_topic("Some Topic") else { panic!("Unexpectedly not Error::BadFormat"); }; assert_eq!(msg, "Invalid topic character"); } #[test] fn create_from_tuple() { let mut params = Params::new(); params.add_str("my", "word").unwrap(); let tg = Telegram::try_from(("Hello", params)).unwrap(); assert_eq!(tg.get_str("my"), Some("word")); } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :