test(sysctl): add tests for section module

This commit is contained in:
Orhun Parmaksız 2021-12-18 18:50:19 +03:00
parent 5fcbf2ca1c
commit aeef248a69
No known key found for this signature in database
GPG key ID: F83424824B3E4B90

View file

@ -37,8 +37,10 @@ impl From<String> for Section {
impl<'a> From<&'a Path> for Section {
fn from(value: &'a Path) -> Self {
for section in Self::variants() {
if value.file_stem().and_then(|v| v.to_str()) == Some(&section.to_string()) {
return *section;
if let Some(file_stem) = value.file_stem().and_then(|v| v.to_str()) {
if file_stem.starts_with(&section.to_string().to_lowercase()) {
return *section;
}
}
}
Self::Net
@ -65,3 +67,20 @@ impl Section {
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sysctl_section() {
assert_eq!(Section::Net, Section::from(String::from("net.xyz")));
assert_eq!(Section::User, Section::from(String::from("user.aaa.bbb")));
assert_eq!(Section::Unknown, Section::from(String::from("test")));
assert_eq!(Section::Vm, Section::from(Path::new("/etc/vm.txt")));
assert_eq!(
Section::Kernel,
Section::from(Path::new("/etc/kernel.tar.gz"))
);
}
}