diff options
author | Benjamin Sago <ogham@bsago.me> | 2016-04-05 18:45:35 +0100 |
---|---|---|
committer | Benjamin Sago <ogham@bsago.me> | 2016-04-05 18:45:35 +0100 |
commit | c009a68ae54c1a01b0bfc6475bd448fa1c165a3e (patch) | |
tree | 521ac90f23ca6783b7dfa9a7591ad1dc40b38a78 /src/output | |
parent | f6c5c89f55c694f04f9f0a1269f5c4945450d78b (diff) |
Add Add impl and various tests for DisplayWidth
Diffstat (limited to 'src/output')
-rw-r--r-- | src/output/cell.rs | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/src/output/cell.rs b/src/output/cell.rs index d900f6d..b78f821 100644 --- a/src/output/cell.rs +++ b/src/output/cell.rs @@ -1,6 +1,6 @@ //! The `TextCell` type for the details and lines views. -use std::ops::{Deref, DerefMut}; +use std::ops::{Add, Deref, DerefMut}; use ansi_term::{Style, ANSIString, ANSIStrings}; use unicode_width::UnicodeWidthStr; @@ -202,4 +202,51 @@ impl DerefMut for DisplayWidth { fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target { &mut self.0 } -}
\ No newline at end of file +} + +impl Add for DisplayWidth { + type Output = DisplayWidth; + + fn add(self, rhs: DisplayWidth) -> Self::Output { + DisplayWidth(self.0 + rhs.0) + } +} + +impl Add<usize> for DisplayWidth { + type Output = DisplayWidth; + + fn add(self, rhs: usize) -> Self::Output { + DisplayWidth(self.0 + rhs) + } +} + + +#[cfg(test)] +mod width_unit_test { + use super::DisplayWidth; + + #[test] + fn empty_string() { + let cell = DisplayWidth::from(""); + assert_eq!(*cell, 0); + } + + #[test] + fn test_string() { + let cell = DisplayWidth::from("Diss Playwidth"); + assert_eq!(*cell, 14); + } + + #[test] + fn addition() { + let cell_one = DisplayWidth::from("/usr/bin/"); + let cell_two = DisplayWidth::from("drinking"); + assert_eq!(*(cell_one + cell_two), 17); + } + + #[test] + fn addition_usize() { + let cell = DisplayWidth::from("/usr/bin/"); + assert_eq!(*(cell + 8), 17); + } +} |