From 191c0e5b90febabac807ad2438afc092419b55c9 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Sun, 25 Jan 2015 13:04:15 +0000 Subject: [PATCH] Test highlighting of usernames --- src/column.rs | 1 + src/file.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/src/column.rs b/src/column.rs index d842a86..881712d 100644 --- a/src/column.rs +++ b/src/column.rs @@ -82,6 +82,7 @@ impl Alignment { } } +#[derive(PartialEq, Debug)] pub struct Cell { pub length: usize, pub text: String, diff --git a/src/file.rs b/src/file.rs index 6395630..bcfd55d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -377,3 +377,93 @@ impl<'a> File<'a> { fn ext<'a>(name: &'a str) -> Option { name.rfind('.').map(|p| name[p+1..].to_string()) } + + +#[cfg(test)] +mod test { + use super::*; + use column::{Cell, Column}; + use std::io; + use users::User; + use users::mock::MockUsers; + + use ansi_term::Style::Plain; + use ansi_term::Colour::Yellow; + + fn dummy_stat() -> io::FileStat { + io::FileStat { + size: 0, + kind: io::FileType::RegularFile, + created: 0, + modified: 0, + accessed: 0, + perm: io::USER_READ, + unstable: io::UnstableFileStat { + inode: 0, + device: 0, + rdev: 0, + nlink: 0, + uid: 0, + gid: 0, + blksize: 0, + blocks: 0, + flags: 0, + gen: 0, + } + } + } + + #[test] + fn user_with_name() { + let mut stat = dummy_stat(); + stat.unstable.uid = 1000; + + let file = File::with_stat(stat, &Path::new("/hi"), None); + + let mut users = MockUsers::with_current_uid(1000); + users.add_user(User { uid: 1000, name: "enoch".to_string(), primary_group: 100 }); + + let cell = Cell::paint(Yellow.bold(), "enoch"); + assert_eq!(cell, file.display(&Column::User, &mut users)) + } + + #[test] + fn user_with_no_name() { + let mut stat = dummy_stat(); + stat.unstable.uid = 1000; + + let file = File::with_stat(stat, &Path::new("/hi"), None); + + let mut users = MockUsers::with_current_uid(1000); + + let cell = Cell::paint(Yellow.bold(), "1000"); + assert_eq!(cell, file.display(&Column::User, &mut users)) + } + + #[test] + fn a_different_user() { + let mut stat = dummy_stat(); + stat.unstable.uid = 1000; + + let file = File::with_stat(stat, &Path::new("/hi"), None); + + let mut users = MockUsers::with_current_uid(3); + users.add_user(User { uid: 1000, name: "enoch".to_string(), primary_group: 100 }); + + let cell = Cell::paint(Plain, "enoch"); + assert_eq!(cell, file.display(&Column::User, &mut users)) + } + + #[test] + fn a_different_yet_unnamed_user() { + let mut stat = dummy_stat(); + stat.unstable.uid = 1000; + + let file = File::with_stat(stat, &Path::new("/hi"), None); + + let mut users = MockUsers::with_current_uid(3); + + let cell = Cell::paint(Plain, "1000"); + assert_eq!(cell, file.display(&Column::User, &mut users)) + } +}