From 614e5cd80a89cf5b4eb3f9cd3fe9e8fca9c30e97 Mon Sep 17 00:00:00 2001 From: love_hangzhou <421699196@qq.com> Date: Wed, 19 Jul 2023 22:28:05 +0800 Subject: [PATCH] test(condition): add unit test for condition --- coms/service/src/config.rs | 13 +++++++ core/sysmaster/unit/entry/condition.rs | 53 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/coms/service/src/config.rs b/coms/service/src/config.rs index e61340b..ec2ebf4 100644 --- a/coms/service/src/config.rs +++ b/coms/service/src/config.rs @@ -218,4 +218,17 @@ mod tests { assert!(result.is_ok()); } + #[test] + fn test_get_service_type() { + let mut file_path = get_project_root().unwrap(); + file_path.push("tests/test_units/config.service"); + + let paths = vec![file_path]; + + let comm = Rc::new(ServiceUnitComm::new()); + let config = ServiceConfig::new(&comm); + + let result = config.load(paths, false); + assert_eq!(config.service_type(), b"Simple") + } } diff --git a/core/sysmaster/unit/entry/condition.rs b/core/sysmaster/unit/entry/condition.rs index c3836b4..225297e 100644 --- a/core/sysmaster/unit/entry/condition.rs +++ b/core/sysmaster/unit/entry/condition.rs @@ -160,3 +160,56 @@ impl UeCondition { Self::condition_vec_test(assert_conditions) } } + +#[cfg(test)] +mod tests { + use basic::condition::ConditionType; + + use crate::unit::entry::condition::condition_keys::CONDITION_NEEDS_UPDATE; + + use super::{ + assert_keys::ASSERT_PATH_EXISTS, condition_keys::CONDITION_PATH_EXISTS, UeCondition, + }; + + #[test] + fn test_new_condition_trigger() { + let uc = UeCondition::new(); + let c = uc.new_condition(ConditionType::FileNotEmpty, String::from("|!test")); + assert_eq!(c.trigger(), 1, "condition trigger is {}", c.trigger()); + } + #[test] + fn test_new_condition_is_not_trigger() { + let uc = UeCondition::new(); + let c = uc.new_condition(ConditionType::FileNotEmpty, String::from("!test")); + assert_eq!(c.trigger(), 0, "condition trigger is {}", c.trigger()); + } + + #[test] + fn test_new_condition_is_revert() { + let uc = UeCondition::new(); + let c = uc.new_condition(ConditionType::FileNotEmpty, String::from("!test")); + assert_eq!(c.revert(), 1, "condition revert is {}", c.revert()); + } + #[test] + fn test_new_condition_is_not_revert() { + let uc = UeCondition::new(); + let c = uc.new_condition(ConditionType::FileNotEmpty, String::from("test")); + assert_eq!(c.revert(), 0, "condition revert is {}", c.revert()); + } + + #[test] + fn test_add_condition() { + let uc = UeCondition::new(); + uc.add_condition(CONDITION_PATH_EXISTS, String::from("test")); + assert_eq!(uc.conditions.borrow().0.len(), 1); + uc.add_condition(CONDITION_NEEDS_UPDATE, String::from("True")); + assert_eq!(uc.conditions.borrow().0.len(), 2); + } + + #[test] + fn test_add_assert() { + let uc = UeCondition::new(); + uc.add_assert(ASSERT_PATH_EXISTS, String::from("assert")); + assert_eq!(uc.asserts.borrow().0.len(), 1); + } +} -- 2.33.0