Add Add impl and various tests for DisplayWidth

This commit is contained in:
Benjamin Sago 2016-04-05 18:45:35 +01:00
parent f6c5c89f55
commit c009a68ae5

View File

@ -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
}
}
}
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);
}
}