mirror of
https://github.com/lsd-rs/lsd
synced 2024-12-14 14:12:31 +00:00
fix tests for flags as ref
This commit is contained in:
parent
dd5e8733f2
commit
1e6b2c6e67
6 changed files with 59 additions and 66 deletions
15
src/app.rs
15
src/app.rs
|
@ -169,18 +169,11 @@ pub fn build() -> App<'static, 'static> {
|
|||
Arg::with_name("blocks")
|
||||
.long("blocks")
|
||||
.multiple(true)
|
||||
.possible_values(&[
|
||||
"type",
|
||||
"permission",
|
||||
"user",
|
||||
"group",
|
||||
"size",
|
||||
"date",
|
||||
"name",
|
||||
])
|
||||
.require_delimiter(true)
|
||||
.number_of_values(1)
|
||||
.help("Specify the blocks that will be displayed"),
|
||||
.require_delimiter(true)
|
||||
.possible_values(&["permission", "user", "group", "size", "date", "name"])
|
||||
.default_value("permission,user,group,size,date,name")
|
||||
.help("Specify the blocks that will be displayed and in what order"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("classic")
|
||||
|
|
|
@ -110,7 +110,7 @@ impl Core {
|
|||
}
|
||||
|
||||
fn sort(&self, metas: &mut Vec<Meta>) {
|
||||
metas.sort_unstable_by(|a, b| sort::by_meta(a, b, self.flags.clone()));
|
||||
metas.sort_unstable_by(|a, b| sort::by_meta(a, b, &self.flags));
|
||||
|
||||
for meta in metas {
|
||||
if let Some(ref mut content) = meta.content {
|
||||
|
|
|
@ -119,7 +119,7 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Colour::Fixed(40).paint(creation_date.ctime().to_string()),
|
||||
date.render(&colors, creation_date.ctime().to_string().len(), flags)
|
||||
date.render(&colors, creation_date.ctime().to_string().len(), &flags)
|
||||
);
|
||||
|
||||
fs::remove_file(file_path).unwrap();
|
||||
|
@ -143,7 +143,7 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Colour::Fixed(42).paint(creation_date.ctime().to_string()),
|
||||
date.render(&colors, creation_date.ctime().to_string().len(), flags)
|
||||
date.render(&colors, creation_date.ctime().to_string().len(), &flags)
|
||||
);
|
||||
|
||||
fs::remove_file(file_path).unwrap();
|
||||
|
@ -167,7 +167,7 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Colour::Fixed(36).paint(creation_date.ctime().to_string()),
|
||||
date.render(&colors, creation_date.ctime().to_string().len(), flags)
|
||||
date.render(&colors, creation_date.ctime().to_string().len(), &flags)
|
||||
);
|
||||
|
||||
fs::remove_file(file_path).unwrap();
|
||||
|
@ -193,7 +193,7 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Colour::Fixed(36).paint("2 days ago "),
|
||||
date.render(&colors, 12, flags)
|
||||
date.render(&colors, 12, &flags)
|
||||
);
|
||||
|
||||
fs::remove_file(file_path).unwrap();
|
||||
|
@ -229,7 +229,7 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
Colour::Fixed(40).paint("now "),
|
||||
date.render(&colors, 5, flags)
|
||||
date.render(&colors, 5, &flags)
|
||||
);
|
||||
|
||||
fs::remove_file(file_path).unwrap();
|
||||
|
|
|
@ -44,7 +44,7 @@ mod test {
|
|||
|
||||
let file_type = Indicator::from(FileType::Directory { uid: false });
|
||||
|
||||
assert_eq!("/", file_type.render(flags).to_string().as_str());
|
||||
assert_eq!("/", file_type.render(&flags).to_string().as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -57,7 +57,7 @@ mod test {
|
|||
exec: true,
|
||||
});
|
||||
|
||||
assert_eq!("*", file_type.render(flags).to_string().as_str());
|
||||
assert_eq!("*", file_type.render(&flags).to_string().as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -67,7 +67,7 @@ mod test {
|
|||
|
||||
let file_type = Indicator::from(FileType::Socket);
|
||||
|
||||
assert_eq!("=", file_type.render(flags).to_string().as_str());
|
||||
assert_eq!("=", file_type.render(&flags).to_string().as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -77,7 +77,7 @@ mod test {
|
|||
|
||||
let file_type = Indicator::from(FileType::SymLink);
|
||||
|
||||
assert_eq!("@", file_type.render(flags).to_string().as_str());
|
||||
assert_eq!("@", file_type.render(&flags).to_string().as_str());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -91,6 +91,6 @@ mod test {
|
|||
uid: false,
|
||||
});
|
||||
|
||||
assert_eq!("", file_type.render(flags).to_string().as_str());
|
||||
assert_eq!("", file_type.render(&flags).to_string().as_str());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -162,9 +162,9 @@ mod test {
|
|||
|
||||
assert_eq!(size.render_value().as_str(), "42");
|
||||
|
||||
assert_eq!(size.render_unit(flags).as_str(), "B");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "B");
|
||||
flags.size = SizeFlag::Short;
|
||||
assert_eq!(size.render_unit(flags).as_str(), "B");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "B");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -173,9 +173,9 @@ mod test {
|
|||
let mut flags = Flags::default();
|
||||
|
||||
assert_eq!(size.render_value().as_str(), "42");
|
||||
assert_eq!(size.render_unit(flags).as_str(), "KB");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "KB");
|
||||
flags.size = SizeFlag::Short;
|
||||
assert_eq!(size.render_unit(flags).as_str(), "K");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "K");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -184,9 +184,9 @@ mod test {
|
|||
let mut flags = Flags::default();
|
||||
|
||||
assert_eq!(size.render_value().as_str(), "42");
|
||||
assert_eq!(size.render_unit(flags).as_str(), "MB");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "MB");
|
||||
flags.size = SizeFlag::Short;
|
||||
assert_eq!(size.render_unit(flags).as_str(), "M");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "M");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -195,9 +195,9 @@ mod test {
|
|||
let mut flags = Flags::default();
|
||||
|
||||
assert_eq!(size.render_value().as_str(), "42");
|
||||
assert_eq!(size.render_unit(flags).as_str(), "GB");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "GB");
|
||||
flags.size = SizeFlag::Short;
|
||||
assert_eq!(size.render_unit(flags).as_str(), "G");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "G");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -206,9 +206,9 @@ mod test {
|
|||
let mut flags = Flags::default();
|
||||
|
||||
assert_eq!(size.render_value().as_str(), "42");
|
||||
assert_eq!(size.render_unit(flags).as_str(), "TB");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "TB");
|
||||
flags.size = SizeFlag::Short;
|
||||
assert_eq!(size.render_unit(flags).as_str(), "T");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "T");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -217,7 +217,7 @@ mod test {
|
|||
let flags = Flags::default();
|
||||
|
||||
assert_eq!(size.render_value().as_str(), "42.1");
|
||||
assert_eq!(size.render_unit(flags).as_str(), "KB");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "KB");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -226,6 +226,6 @@ mod test {
|
|||
let flags = Flags::default();
|
||||
|
||||
assert_eq!(size.render_value().as_str(), "42");
|
||||
assert_eq!(size.render_unit(flags).as_str(), "KB");
|
||||
assert_eq!(size.render_unit(&flags).as_str(), "KB");
|
||||
}
|
||||
}
|
||||
|
|
64
src/sort.rs
64
src/sort.rs
|
@ -2,12 +2,12 @@ use crate::flags::{DirOrderFlag, Flags, SortFlag, SortOrder};
|
|||
use crate::meta::{FileType, Meta};
|
||||
use std::cmp::Ordering;
|
||||
|
||||
pub fn by_meta(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
pub fn by_meta(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
match flags.sort_by {
|
||||
SortFlag::Name => match flags.directory_order {
|
||||
DirOrderFlag::First => by_name_with_dirs_first(a, b, flags),
|
||||
DirOrderFlag::None => by_name(a, b, flags),
|
||||
DirOrderFlag::Last => by_name_with_files_first(a, b, flags),
|
||||
DirOrderFlag::First => by_name_with_dirs_first(a, b, &flags),
|
||||
DirOrderFlag::None => by_name(a, b, &flags),
|
||||
DirOrderFlag::Last => by_name_with_files_first(a, b, &flags),
|
||||
},
|
||||
SortFlag::Size => match flags.directory_order {
|
||||
DirOrderFlag::First => by_size(a, b, flags),
|
||||
|
@ -15,14 +15,14 @@ pub fn by_meta(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
|||
DirOrderFlag::Last => by_size(a, b, flags),
|
||||
},
|
||||
SortFlag::Time => match flags.directory_order {
|
||||
DirOrderFlag::First => by_date_with_dirs_first(a, b, flags),
|
||||
DirOrderFlag::None => by_date(a, b, flags),
|
||||
DirOrderFlag::Last => by_date_with_files_first(a, b, flags),
|
||||
DirOrderFlag::First => by_date_with_dirs_first(a, b, &flags),
|
||||
DirOrderFlag::None => by_date(a, b, &flags),
|
||||
DirOrderFlag::Last => by_date_with_files_first(a, b, &flags),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn by_size(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_size(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
|
||||
if flags.sort_order == SortOrder::Default {
|
||||
b.size.get_bytes().cmp(&a.size.get_bytes())
|
||||
|
@ -33,7 +33,7 @@ fn by_size(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
|||
|
||||
|
||||
|
||||
fn by_name(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_name(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
if flags.sort_order == SortOrder::Default {
|
||||
a.name.cmp(&b.name)
|
||||
} else {
|
||||
|
@ -41,25 +41,25 @@ fn by_name(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
|||
}
|
||||
}
|
||||
|
||||
fn by_name_with_dirs_first(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_name_with_dirs_first(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
match (a.file_type, b.file_type) {
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_name(a, b, flags),
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_name(a, b, &flags),
|
||||
(FileType::Directory { .. }, _) => Ordering::Less,
|
||||
(_, FileType::Directory { .. }) => Ordering::Greater,
|
||||
_ => by_name(a, b, flags),
|
||||
_ => by_name(a, b, &flags),
|
||||
}
|
||||
}
|
||||
|
||||
fn by_name_with_files_first(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_name_with_files_first(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
match (a.file_type, b.file_type) {
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_name(a, b, flags),
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_name(a, b, &flags),
|
||||
(FileType::Directory { .. }, _) => Ordering::Greater,
|
||||
(_, FileType::Directory { .. }) => Ordering::Less,
|
||||
_ => by_name(a, b, flags),
|
||||
_ => by_name(a, b, &flags),
|
||||
}
|
||||
}
|
||||
|
||||
fn by_date(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_date(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
if flags.sort_order == SortOrder::Default {
|
||||
b.date.cmp(&a.date).then(a.name.cmp(&b.name))
|
||||
} else {
|
||||
|
@ -67,21 +67,21 @@ fn by_date(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
|||
}
|
||||
}
|
||||
|
||||
fn by_date_with_dirs_first(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_date_with_dirs_first(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
match (a.file_type, b.file_type) {
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_date(a, b, flags),
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_date(a, b, &flags),
|
||||
(FileType::Directory { .. }, _) => Ordering::Less,
|
||||
(_, FileType::Directory { .. }) => Ordering::Greater,
|
||||
_ => by_date(a, b, flags),
|
||||
_ => by_date(a, b, &flags),
|
||||
}
|
||||
}
|
||||
|
||||
fn by_date_with_files_first(a: &Meta, b: &Meta, flags: Flags) -> Ordering {
|
||||
fn by_date_with_files_first(a: &Meta, b: &Meta, flags: &Flags) -> Ordering {
|
||||
match (a.file_type, b.file_type) {
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_date(a, b, flags),
|
||||
(FileType::Directory { .. }, FileType::Directory { .. }) => by_date(a, b, &flags),
|
||||
(FileType::Directory { .. }, _) => Ordering::Greater,
|
||||
(_, FileType::Directory { .. }) => Ordering::Less,
|
||||
_ => by_date(a, b, flags),
|
||||
_ => by_date(a, b, &flags),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,11 +111,11 @@ mod tests {
|
|||
flags.directory_order = DirOrderFlag::First;
|
||||
|
||||
// Sort with the dirs first
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Greater);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Greater);
|
||||
|
||||
// Sort with the dirs first (the dirs stay first)
|
||||
flags.sort_order = SortOrder::Reverse;
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Greater);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -136,10 +136,10 @@ mod tests {
|
|||
flags.directory_order = DirOrderFlag::Last;
|
||||
|
||||
// Sort with file first
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Less);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Less);
|
||||
|
||||
// Sort with file first reversed (thie files stay first)
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Less);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Less);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -160,11 +160,11 @@ mod tests {
|
|||
flags.directory_order = DirOrderFlag::None;
|
||||
|
||||
// Sort by name unordered
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Less);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Less);
|
||||
|
||||
// Sort by name unordered
|
||||
flags.sort_order = SortOrder::Reverse;
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Greater);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -185,11 +185,11 @@ mod tests {
|
|||
flags.directory_order = DirOrderFlag::None;
|
||||
|
||||
// Sort by name unordered
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Greater);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Greater);
|
||||
|
||||
// Sort by name unordered reversed
|
||||
flags.sort_order = SortOrder::Reverse;
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Less);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Less);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -231,10 +231,10 @@ mod tests {
|
|||
flags.sort_by = SortFlag::Time;
|
||||
|
||||
// Sort by time
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Less);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Less);
|
||||
|
||||
// Sort by time reversed
|
||||
flags.sort_order = SortOrder::Reverse;
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, flags), Ordering::Greater);
|
||||
assert_eq!(by_meta(&meta_a, &meta_z, &flags), Ordering::Greater);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue