Merge branch 'actual-working-tests'

This commit is contained in:
Ben S 2015-05-16 21:02:38 +01:00
commit 9775aea8bb
3 changed files with 221 additions and 202 deletions

View File

@ -494,207 +494,23 @@ pub mod fields {
} }
} }
#[cfg(broken_test)]
pub mod test {
pub use super::*;
pub use column::{Cell, Column}; #[cfg(test)]
pub use output::details::UserLocale; mod test {
use super::ext;
pub use users::{User, Group};
pub use users::mock::MockUsers;
pub use ansi_term::Style::Plain;
pub use ansi_term::Colour::Yellow;
#[test] #[test]
fn extension() { fn extension() {
assert_eq!(Some("dat".to_string()), super::ext("fester.dat")) assert_eq!(Some("dat".to_string()), ext("fester.dat"))
} }
#[test] #[test]
fn dotfile() { fn dotfile() {
assert_eq!(Some("vimrc".to_string()), super::ext(".vimrc")) assert_eq!(Some("vimrc".to_string()), ext(".vimrc"))
} }
#[test] #[test]
fn no_extension() { fn no_extension() {
assert_eq!(None, super::ext("jarlsberg")) assert_eq!(None, ext("jarlsberg"))
}
pub fn new_file(metadata: io::FileStat, path: &'static str) -> File {
File::with_metadata(metadata, &Path::new(path), None, false)
}
pub 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,
}
}
}
pub fn dummy_locale() -> UserLocale {
UserLocale::default()
}
mod users {
use super::*;
#[test]
fn named() {
let mut metadata = dummy_stat();
metadata.unstable.uid = 1000;
let file = new_file(metadata, "/hi");
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, &dummy_locale()))
}
#[test]
fn unnamed() {
let mut metadata = dummy_stat();
metadata.unstable.uid = 1000;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(1000);
let cell = Cell::paint(Yellow.bold(), "1000");
assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
}
#[test]
fn different_named() {
let mut metadata = dummy_stat();
metadata.unstable.uid = 1000;
let file = new_file(metadata, "/hi");
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, &dummy_locale()))
}
#[test]
fn different_unnamed() {
let mut metadata = dummy_stat();
metadata.unstable.uid = 1000;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
let cell = Cell::paint(Plain, "1000");
assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
}
#[test]
fn overflow() {
let mut metadata = dummy_stat();
metadata.unstable.uid = 2_147_483_648;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
let cell = Cell::paint(Plain, "2147483648");
assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
}
}
mod groups {
use super::*;
#[test]
fn named() {
let mut metadata = dummy_stat();
metadata.unstable.gid = 100;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
let cell = Cell::paint(Plain, "folk");
assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
}
#[test]
fn unnamed() {
let mut metadata = dummy_stat();
metadata.unstable.gid = 100;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
let cell = Cell::paint(Plain, "100");
assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
}
#[test]
fn primary() {
let mut metadata = dummy_stat();
metadata.unstable.gid = 100;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
users.add_user(User { uid: 3, name: "eve".to_string(), primary_group: 100 });
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
let cell = Cell::paint(Yellow.bold(), "folk");
assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
}
#[test]
fn secondary() {
let mut metadata = dummy_stat();
metadata.unstable.gid = 100;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
users.add_user(User { uid: 3, name: "eve".to_string(), primary_group: 12 });
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![ "eve".to_string() ] });
let cell = Cell::paint(Yellow.bold(), "folk");
assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
}
#[test]
fn overflow() {
let mut metadata = dummy_stat();
metadata.unstable.gid = 2_147_483_648;
let file = new_file(metadata, "/hi");
let mut users = MockUsers::with_current_uid(3);
let cell = Cell::paint(Plain, "2147483648");
assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
}
} }
} }

View File

@ -94,7 +94,7 @@ impl Options {
let sort_field = match matches.opt_str("sort") { let sort_field = match matches.opt_str("sort") {
Some(word) => try!(SortField::from_word(word)), Some(word) => try!(SortField::from_word(word)),
None => SortField::Name, None => SortField::default(),
}; };
let filter = FileFilter { let filter = FileFilter {
@ -166,6 +166,12 @@ pub enum SortField {
ModifiedDate, AccessedDate, CreatedDate, ModifiedDate, AccessedDate, CreatedDate,
} }
impl Default for SortField {
fn default() -> SortField {
SortField::Name
}
}
impl SortField { impl SortField {
/// Find which field to use based on a user-supplied word. /// Find which field to use based on a user-supplied word.
@ -343,6 +349,12 @@ pub enum SizeFormat {
JustBytes, JustBytes,
} }
impl Default for SizeFormat {
fn default() -> SizeFormat {
SizeFormat::DecimalBytes
}
}
impl SizeFormat { impl SizeFormat {
pub fn deduce(matches: &getopts::Matches) -> Result<SizeFormat, Misfire> { pub fn deduce(matches: &getopts::Matches) -> Result<SizeFormat, Misfire> {
let binary = matches.opt_present("binary"); let binary = matches.opt_present("binary");
@ -381,6 +393,12 @@ pub struct TimeTypes {
created: bool, created: bool,
} }
impl Default for TimeTypes {
fn default() -> TimeTypes {
TimeTypes { accessed: false, modified: true, created: false }
}
}
impl TimeTypes { impl TimeTypes {
/// Find which field to use based on a user-supplied word. /// Find which field to use based on a user-supplied word.
@ -413,7 +431,7 @@ impl TimeTypes {
Ok(TimeTypes { accessed: accessed, modified: modified, created: created }) Ok(TimeTypes { accessed: accessed, modified: modified, created: created })
} }
else { else {
Ok(TimeTypes { accessed: false, modified: true, created: false }) Ok(TimeTypes::default())
} }
} }
} }
@ -504,7 +522,7 @@ impl RecurseOptions {
} }
} }
#[derive(PartialEq, Copy, Clone, Debug)] #[derive(PartialEq, Copy, Clone, Debug, Default)]
pub struct Columns { pub struct Columns {
size_format: SizeFormat, size_format: SizeFormat,
time_types: TimeTypes, time_types: TimeTypes,

View File

@ -5,7 +5,9 @@ use dir::Dir;
use file::File; use file::File;
use file::fields as f; use file::fields as f;
use options::{Columns, FileFilter, RecurseOptions, SizeFormat}; use options::{Columns, FileFilter, RecurseOptions, SizeFormat};
use users::{OSUsers, Users}; use users::{OSUsers, Users};
use users::mock::MockUsers;
use super::filename; use super::filename;
@ -29,7 +31,7 @@ use datetime::format::{DateFormat};
/// ///
/// Almost all the heavy lifting is done in a Table object, which handles the /// Almost all the heavy lifting is done in a Table object, which handles the
/// columns for each row. /// columns for each row.
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone, Default)]
pub struct Details { pub struct Details {
/// A Columns object that says which columns should be included in the /// A Columns object that says which columns should be included in the
@ -67,7 +69,7 @@ impl Details {
/// Adds files to the table - recursively, if the `recurse` option /// Adds files to the table - recursively, if the `recurse` option
/// is present. /// is present.
fn add_files_to_table(&self, table: &mut Table, src: &[File], depth: usize) { fn add_files_to_table<U: Users>(&self, table: &mut Table<U>, src: &[File], depth: usize) {
for (index, file) in src.iter().enumerate() { for (index, file) in src.iter().enumerate() {
table.add_file(file, depth, index == src.len() - 1); table.add_file(file, depth, index == src.len() - 1);
@ -120,22 +122,36 @@ struct Row {
/// A **Table** object gets built up by the view as it lists files and /// A **Table** object gets built up by the view as it lists files and
/// directories. /// directories.
struct Table { pub struct Table<U> {
columns: Vec<Column>, columns: Vec<Column>,
rows: Vec<Row>, rows: Vec<Row>,
time: locale::Time, time: locale::Time,
numeric: locale::Numeric, numeric: locale::Numeric,
users: OSUsers, users: U,
colours: Colours, colours: Colours,
current_year: i64, current_year: i64,
} }
impl Table { impl Default for Table<MockUsers> {
fn default() -> Table<MockUsers> {
Table {
columns: Columns::default().for_dir(None),
rows: Vec::new(),
time: locale::Time::english(),
numeric: locale::Numeric::english(),
users: MockUsers::with_current_uid(0),
colours: Colours::default(),
current_year: 1234,
}
}
}
impl Table<OSUsers> {
/// Create a new, empty Table object, setting the caching fields to their /// Create a new, empty Table object, setting the caching fields to their
/// empty states. /// empty states.
fn with_options(colours: Colours, columns: Vec<Column>) -> Table { fn with_options(colours: Colours, columns: Vec<Column>) -> Table<OSUsers> {
Table { Table {
columns: columns, columns: columns,
rows: Vec::new(), rows: Vec::new(),
@ -147,6 +163,9 @@ impl Table {
current_year: LocalDateTime::now().year(), current_year: LocalDateTime::now().year(),
} }
} }
}
impl<U> Table<U> where U: Users {
/// Add a dummy "header" row to the table, which contains the names of all /// Add a dummy "header" row to the table, which contains the names of all
/// the columns, underlined. This has dummy data for the cases that aren't /// the columns, underlined. This has dummy data for the cases that aren't
@ -429,3 +448,169 @@ impl TreePart {
} }
} }
} }
#[cfg(test)]
pub mod test {
pub use super::Table;
pub use file::File;
pub use file::fields as f;
pub use column::{Cell, Column};
pub use users::{User, Group, uid_t, gid_t};
pub use users::mock::MockUsers;
pub use ansi_term::Style::Plain;
pub use ansi_term::Colour::*;
pub fn newser(uid: uid_t, name: &str, group: gid_t) -> User {
User {
uid: uid,
name: name.to_string(),
primary_group: group,
home_dir: String::new(),
shell: String::new(),
}
}
// These tests create a new, default Table object, then fill in the
// expected style in a certain way. This means we can check that the
// right style is being used, as otherwise, it would just be `Plain`.
//
// Doing things with fields is way easier than having to fake the entire
// Metadata struct, which is what I was doing before!
mod users {
use super::*;
#[test]
fn named() {
let mut table = Table::default();
table.colours.users.user_you = Red.bold();
let mut users = MockUsers::with_current_uid(1000);
users.add_user(newser(1000, "enoch", 100));
table.users = users;
let user = f::User(1000);
let expected = Cell::paint(Red.bold(), "enoch");
assert_eq!(expected, table.render_user(user))
}
#[test]
fn unnamed() {
let mut table = Table::default();
table.colours.users.user_you = Cyan.bold();
let users = MockUsers::with_current_uid(1000);
table.users = users;
let user = f::User(1000);
let expected = Cell::paint(Cyan.bold(), "1000");
assert_eq!(expected, table.render_user(user));
}
#[test]
fn different_named() {
let mut table = Table::default();
table.colours.users.user_someone_else = Green.bold();
table.users.add_user(newser(1000, "enoch", 100));
let user = f::User(1000);
let expected = Cell::paint(Green.bold(), "enoch");
assert_eq!(expected, table.render_user(user));
}
#[test]
fn different_unnamed() {
let mut table = Table::default();
table.colours.users.user_someone_else = Red.normal();
let user = f::User(1000);
let expected = Cell::paint(Red.normal(), "1000");
assert_eq!(expected, table.render_user(user));
}
#[test]
fn overflow() {
let mut table = Table::default();
table.colours.users.user_someone_else = Blue.underline();
let user = f::User(2_147_483_648);
let expected = Cell::paint(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_user(user));
}
}
mod groups {
use super::*;
#[test]
fn named() {
let mut table = Table::default();
table.colours.users.group_not_yours = Fixed(101).normal();
let mut users = MockUsers::with_current_uid(1000);
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
table.users = users;
let group = f::Group(100);
let expected = Cell::paint(Fixed(101).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn unnamed() {
let mut table = Table::default();
table.colours.users.group_not_yours = Fixed(87).normal();
let users = MockUsers::with_current_uid(1000);
table.users = users;
let group = f::Group(100);
let expected = Cell::paint(Fixed(87).normal(), "100");
assert_eq!(expected, table.render_group(group));
}
#[test]
fn primary() {
let mut table = Table::default();
table.colours.users.group_yours = Fixed(64).normal();
let mut users = MockUsers::with_current_uid(2);
users.add_user(newser(2, "eve", 100));
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
table.users = users;
let group = f::Group(100);
let expected = Cell::paint(Fixed(64).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn secondary() {
let mut table = Table::default();
table.colours.users.group_yours = Fixed(31).normal();
let mut users = MockUsers::with_current_uid(2);
users.add_user(newser(2, "eve", 666));
users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![ "eve".to_string() ] });
table.users = users;
let group = f::Group(100);
let expected = Cell::paint(Fixed(31).normal(), "folk");
assert_eq!(expected, table.render_group(group))
}
#[test]
fn overflow() {
let mut table = Table::default();
table.colours.users.group_not_yours = Blue.underline();
let group = f::Group(2_147_483_648);
let expected = Cell::paint(Blue.underline(), "2147483648");
assert_eq!(expected, table.render_group(group));
}
}
}