diff options
author | Neil Kistner <neil.kistner@gmail.com> | 2019-09-16 00:03:44 -0500 |
---|---|---|
committer | Kevin Song <chipbuster@users.noreply.github.com> | 2019-09-16 00:03:44 -0500 |
commit | aa26c5bca450f049079a0e85da125b4d20c8ffec (patch) | |
tree | e861f12382c6a22452679371c2f20af5a2dfde88 | |
parent | 121c55bac554bc99868f3e7e70782ceaf068d03c (diff) |
fix: Update Module::is_empty to check value of segments (#332)
Previously, a set of empty segments would cause the module to print. This changes the
logic of Module::is_empty to check that all the segments are empty instead.
-rw-r--r-- | src/module.rs | 39 | ||||
-rw-r--r-- | src/segment.rs | 5 |
2 files changed, 42 insertions, 2 deletions
diff --git a/src/module.rs b/src/module.rs index dde073fb2..6a55a0c8a 100644 --- a/src/module.rs +++ b/src/module.rs @@ -74,9 +74,9 @@ impl<'a> Module<'a> { self.segments.last_mut().unwrap() } - /// Whether a module has any segments + /// Whether a module has non-empty segments pub fn is_empty(&self) -> bool { - self.segments.is_empty() + self.segments.iter().all(|segment| segment.is_empty()) } /// Get the module's prefix @@ -261,3 +261,38 @@ impl fmt::Display for Affix { write!(f, "{}", self.ansi_string()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_module_is_empty_with_no_segments() { + let name = "unit_test"; + let module = Module { + config: None, + _name: name.to_string(), + style: Style::default(), + prefix: Affix::default_prefix(name), + segments: Vec::new(), + suffix: Affix::default_suffix(name), + }; + + assert!(module.is_empty()); + } + + #[test] + fn test_module_is_empty_with_all_empty_segments() { + let name = "unit_test"; + let module = Module { + config: None, + _name: name.to_string(), + style: Style::default(), + prefix: Affix::default_prefix(name), + segments: vec![Segment::new("test_segment")], + suffix: Affix::default_suffix(name), + }; + + assert!(module.is_empty()); + } +} diff --git a/src/segment.rs b/src/segment.rs index ebae80d51..8c0140ac1 100644 --- a/src/segment.rs +++ b/src/segment.rs @@ -52,6 +52,11 @@ impl Segment { None => ANSIString::from(&self.value), } } + + /// Determines if the segment contains a value. + pub fn is_empty(&self) -> bool { + self.value.trim().is_empty() + } } impl fmt::Display for Segment { |