From 152224f20adcee0c151ca10d69a24aa7c4580975 Mon Sep 17 00:00:00 2001 From: Krysztal112233 Date: Sat, 13 Apr 2024 02:00:19 +0800 Subject: [PATCH] slabtop: move unit testing into `tests/by_utils` --- src/uu/slabtop/src/parse.rs | 81 +------------------ tests/by-util/test_slabtop.rs | 72 +++++++++++++++++ .../fixtures/slabtop/data.txt | 0 3 files changed, 75 insertions(+), 78 deletions(-) rename src/uu/slabtop/src/data/test_data.txt => tests/fixtures/slabtop/data.txt (100%) diff --git a/src/uu/slabtop/src/parse.rs b/src/uu/slabtop/src/parse.rs index c5fed40..7f0cad5 100644 --- a/src/uu/slabtop/src/parse.rs +++ b/src/uu/slabtop/src/parse.rs @@ -54,16 +54,6 @@ impl SlabInfo { self.data.iter().map(|(k, _)| k).collect() } - #[allow(unused)] // for slabinfo checking - pub fn meta(&self) -> Vec<&String> { - self.meta.iter().collect() - } - - #[allow(unused)] // for slabinfo checking - pub fn version(&self) -> &String { - &self.version - } - pub fn sort(mut self, by: char, ascending_order: bool) -> Self { let mut sort = |by_meta: &str| { if let Some(offset) = self.offset(by_meta) { @@ -276,14 +266,14 @@ impl SlabInfo { } } -fn parse_version(line: &str) -> Option { +pub(crate) fn parse_version(line: &str) -> Option { line.replace(':', " ") .split_whitespace() .last() .map(String::from) } -fn parse_meta(line: &str) -> Vec { +pub(crate) fn parse_meta(line: &str) -> Vec { line.replace(['#', ':'], " ") .split_whitespace() .filter(|it| it.starts_with('<') && it.ends_with('>')) @@ -291,7 +281,7 @@ fn parse_meta(line: &str) -> Vec { .collect() } -fn parse_data(line: &str) -> Option<(String, Vec)> { +pub(crate) fn parse_data(line: &str) -> Option<(String, Vec)> { let split: Vec = line .replace(':', " ") .split_whitespace() @@ -309,68 +299,3 @@ fn parse_data(line: &str) -> Option<(String, Vec)> { ) }) } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_version() { - let test = "slabinfo - version: 2.1"; - assert_eq!("2.1", parse_version(test).unwrap()) - } - - #[test] - fn test_parse_meta() { - let test="# name : tunables : slabdata "; - - let result = parse_meta(test); - - assert_eq!( - result, - [ - "active_objs", - "num_objs", - "objsize", - "objperslab", - "pagesperslab", - "limit", - "batchcount", - "sharedfactor", - "active_slabs", - "num_slabs", - "sharedavail" - ] - ) - } - - #[test] - fn test_parse_data() { - // Success case - - let test = "nf_conntrack_expect 0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0"; - let (name, value) = parse_data(test).unwrap(); - - assert_eq!(name, "nf_conntrack_expect"); - assert_eq!(value, [0, 0, 208, 39, 2, 0, 0, 0, 0, 0, 0]); - - // Fail case - let test = - "0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0"; - let (name, _value) = parse_data(test).unwrap(); - - assert_ne!(name, "nf_conntrack_expect"); - } - - #[test] - fn test_parse() { - let test = include_str!("./data/test_data.txt"); - let result = SlabInfo::parse(test.into()).unwrap(); - - assert_eq!(result.fetch("nf_conntrack_expect", "objsize").unwrap(), 208); - assert_eq!( - result.fetch("dmaengine-unmap-2", "active_slabs").unwrap(), - 16389 - ); - } -} diff --git a/tests/by-util/test_slabtop.rs b/tests/by-util/test_slabtop.rs index 7452bc8..a4b1c4d 100644 --- a/tests/by-util/test_slabtop.rs +++ b/tests/by-util/test_slabtop.rs @@ -1,6 +1,78 @@ use crate::common::util::TestScenario; +use crate::test_slabtop::parse::parse_data; +use crate::test_slabtop::parse::parse_meta; +use crate::test_slabtop::parse::parse_version; +use crate::test_slabtop::parse::SlabInfo; + +#[path = "../../src/uu/slabtop/src/parse.rs"] +mod parse; #[test] fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); } + +#[test] +fn test_slabtop() { + new_ucmd!().arg("--help").succeeds().code_is(0); +} + +#[test] +fn test_parse_version() { + let test = "slabinfo - version: 2.1"; + assert_eq!("2.1", parse_version(test).unwrap()) +} + +#[test] +fn test_parse_meta() { + let test="# name : tunables : slabdata "; + + let result = parse_meta(test); + + assert_eq!( + result, + [ + "active_objs", + "num_objs", + "objsize", + "objperslab", + "pagesperslab", + "limit", + "batchcount", + "sharedfactor", + "active_slabs", + "num_slabs", + "sharedavail" + ] + ) +} + +#[test] +fn test_parse_data() { + // Success case + + let test = "nf_conntrack_expect 0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0"; + let (name, value) = parse_data(test).unwrap(); + + assert_eq!(name, "nf_conntrack_expect"); + assert_eq!(value, [0, 0, 208, 39, 2, 0, 0, 0, 0, 0, 0]); + + // Fail case + let test = + "0 0 208 39 2 : tunables 0 0 0 : slabdata 0 0 0"; + let (name, _value) = parse_data(test).unwrap(); + + assert_ne!(name, "nf_conntrack_expect"); +} + +#[test] +fn test_parse() { + let test = include_str!("../fixtures/slabtop/data.txt"); + let result = SlabInfo::parse(test.into()).unwrap(); + + assert_eq!(result.fetch("nf_conntrack_expect", "objsize").unwrap(), 208); + assert_eq!( + result.fetch("dmaengine-unmap-2", "active_slabs").unwrap(), + 16389 + ); +} diff --git a/src/uu/slabtop/src/data/test_data.txt b/tests/fixtures/slabtop/data.txt similarity index 100% rename from src/uu/slabtop/src/data/test_data.txt rename to tests/fixtures/slabtop/data.txt