use dever_runtime::{ collection::Map, value::{FaultInfo, decimal::Decimal, fault}, }; #[path = "runtime/auth/mod.rs"] mod auth; #[path = "runtime/calendar.rs"] mod calendar; #[path = "runtime/channel.rs"] mod channel; #[path = "runtime/database/mod.rs"] mod database; #[path = "runtime/io.rs"] mod io; #[path = "runtime/job/mod.rs"] mod job; #[path = "runtime/json.rs"] mod json; #[path = "runtime/net.rs"] mod net; #[path = "runtime/process/mod.rs"] mod process; #[path = "runtime/stream.rs"] mod stream; #[path = "runtime/worker/mod.rs"] mod worker; #[test] fn decimal_constants_round_trip_and_integer_conversion_is_exact() { for text in [ "0", "-0", "0.1000000000000000000000000000000001", "1E+6144", "1E-6176", ] { let value = Decimal::parse(text).unwrap(); assert_eq!(value, Decimal::from_be_bytes(value.to_be_bytes())); } for value in [i64::MIN, -1, 0, i32::MAX as i64, u32::MAX as i64, i64::MAX] { assert_eq!( Decimal::from_int(value), Decimal::parse(&value.to_string()).unwrap() ); } assert!(Decimal::parse("1.0000000000000000000000000000000001").is_err()); assert!(Decimal::parse("NaN").is_err()); } #[test] fn decimal_faults_do_not_poison_the_next_operation() { let mut context = Decimal::context(); let one = Decimal::from_int(1); assert!(one.checked_div(Decimal::ZERO, &mut context).is_none()); assert_eq!( one.checked_add(one, &mut context).unwrap(), Decimal::from_int(2) ); assert_eq!(one.checked_sub(one, &mut context).unwrap(), Decimal::ZERO); assert_eq!( one.checked_mul(Decimal::from_int(3), &mut context).unwrap(), Decimal::from_int(3) ); assert_eq!( one.checked_div(Decimal::from_int(3), &mut context) .unwrap() .to_string(), "0.3333333333333333333333333333333333" ); assert_eq!((-one).to_string(), "-1"); } #[test] fn map_keeps_value_semantics_order_and_duplicate_key_detection() { let mut first = Map::with_capacity(2); assert!(first.insert_unique("a".to_string(), 1)); assert!(first.insert_unique("b".to_string(), 2)); assert!(!first.insert_unique("a".to_string(), 3)); assert_eq!(first.get("a"), Some(&1)); let mut second = Map::with_capacity(2); second.insert_unique("b".to_string(), 2); second.insert_unique("a".to_string(), 1); assert_ne!(first, second); let mut copied = first.clone(); copied.insert_unique("c".to_string(), 3); assert_eq!(first.len(), 2); assert!(!first.is_empty()); } #[test] fn entry_unwinding_preserves_dever_location_and_runs_destructors() { static INFO: FaultInfo = FaultInfo { message: "integer overflow", source: "app.dever", line: 4, column: 8, }; let closed = std::cell::Cell::new(false); struct Resource<'a>(&'a std::cell::Cell); impl Drop for Resource<'_> { fn drop(&mut self) { self.0.set(true); } } let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { let _resource = Resource(&closed); fault(&INFO) })); let payload = result.unwrap_err(); let info = payload.downcast_ref::<&'static FaultInfo>().unwrap(); assert_eq!(info.to_string(), "app.dever:4:8: integer overflow"); assert!(closed.get()); } #[test] fn wire_entry_preserves_fault_reasons_and_failure_status() { const CHILD: &str = "DEVER_TEST_WIRE_ENTRY_FAULT"; if let Ok(kind) = std::env::var(CHILD) { static INFO: FaultInfo = FaultInfo { message: "original Dever failure", source: "app.dever", line: 4, column: 8, }; let status = dever_runtime::wire::run(|| match kind.as_str() { "literal" => panic!("original literal failure"), "owned" => std::panic::panic_any(String::from("original owned failure")), "io" => std::panic::panic_any(std::io::Error::other("original I/O failure")), "dever" => fault(&INFO), _ => unreachable!(), }); assert_eq!(status, std::process::ExitCode::FAILURE); return; } for (kind, expected) in [ ("literal", "original literal failure"), ("owned", "original owned failure"), ("io", "original I/O failure"), ("dever", "app.dever:4:8: original Dever failure"), ] { let output = std::process::Command::new(std::env::current_exe().unwrap()) .args([ "--exact", "wire_entry_preserves_fault_reasons_and_failure_status", "--nocapture", ]) .env(CHILD, kind) .output() .unwrap(); assert!(output.status.success(), "{output:?}"); // panic hook 也会打印载荷;核对入口自身最后写出的原因,不能只搜索原文。 let error = String::from_utf8(output.stderr).unwrap(); assert_eq!(error.lines().last(), Some(expected), "{kind}: {error}"); } } #[test] fn wire_counts_during_decode_and_encode_without_a_second_json_parse() { use dever_runtime::wire::{self, serde_json}; let small: Vec = wire::decode("[1,2]", true, false).unwrap(); assert_eq!(small, vec![1, 2]); let text = format!("[{}0]", "0,".repeat(wire::MAX_VALUES)); assert!( wire::decode::>(&text, true, false) .unwrap_err() .contains("value limit") ); let deep = format!("{}0{}", "[".repeat(65), "]".repeat(65)); assert!( wire::decode::(&deep, false, false) .unwrap_err() .contains("nesting") ); assert!( wire::response(&vec![0; wire::MAX_VALUES]) .unwrap_err() .contains("value limit") ); assert!( wire::response(&"x".repeat(wire::MAX_BYTES)) .unwrap_err() .contains("16 MiB") ); let raw = "\"0.1000000000000000000000000000000001\""; let value: Decimal = wire::decode(raw, true, false).unwrap(); assert!(wire::response(&value).unwrap().contains(raw)); } #[path = "runtime/password.rs"] mod password; #[path = "runtime/task.rs"] mod task;