From c009a68ae54c1a01b0bfc6475bd448fa1c165a3e Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Tue, 5 Apr 2016 18:45:35 +0100 Subject: Add Add impl and various tests for DisplayWidth --- src/output/cell.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'src/output') 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 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); + } +} -- cgit v1.2.3