From 51a345d75198cf0406dfebca50a5aa00fac011ba Mon Sep 17 00:00:00 2001 From: zwPapEr Date: Tue, 9 Aug 2022 14:16:57 +0800 Subject: [PATCH] :mag: :hammer: fix tests to fit icon theme Signed-off-by: zwPapEr --- src/color.rs | 30 +++++++++++++++--------------- src/display.rs | 24 ++++++++++++------------ src/flags/icons.rs | 2 +- src/icon.rs | 39 +++++++++++++++++++++++++-------------- src/meta/name.rs | 20 ++++++++++---------- src/theme/color.rs | 23 ++++++++++++----------- src/theme/icon.rs | 6 ++++-- 7 files changed, 79 insertions(+), 65 deletions(-) diff --git a/src/color.rs b/src/color.rs index 5f1e150e3..8dc63a301 100644 --- a/src/color.rs +++ b/src/color.rs @@ -301,7 +301,7 @@ fn to_content_style(ls: &lscolors::Style) -> ContentStyle { mod tests { use super::Colors; use crate::color::ThemeOption; - use crate::theme::color_theme::Theme; + use crate::theme::color::ColorTheme; #[test] fn test_color_new_no_color_theme() { assert!(Colors::new(ThemeOption::NoColor).theme.is_none()); @@ -311,7 +311,7 @@ mod tests { fn test_color_new_default_theme() { assert_eq!( Colors::new(ThemeOption::Default).theme, - Some(Theme::default_dark()), + Some(ColorTheme::default_dark()), ); } @@ -319,7 +319,7 @@ mod tests { fn test_color_new_bad_custom_theme() { assert_eq!( Colors::new(ThemeOption::Custom("not-existed".to_string())).theme, - Some(Theme::default_dark()), + Some(ColorTheme::default_dark()), ); } } @@ -327,15 +327,15 @@ mod tests { #[cfg(test)] mod elem { use super::Elem; - use crate::color::{theme, Theme}; + use crate::theme::{color, color::ColorTheme}; use crossterm::style::Color; #[cfg(test)] - fn test_theme() -> Theme { - Theme { + fn test_theme() -> ColorTheme { + ColorTheme { user: Color::AnsiValue(230), // Cornsilk1 group: Color::AnsiValue(187), // LightYellow3 - permission: theme::Permission { + permission: color::Permission { read: Color::Green, write: Color::Yellow, exec: Color::Red, @@ -345,19 +345,19 @@ mod elem { acl: Color::DarkCyan, context: Color::Cyan, }, - file_type: theme::FileType { - file: theme::File { + file_type: color::FileType { + file: color::File { exec_uid: Color::AnsiValue(40), // Green3 uid_no_exec: Color::AnsiValue(184), // Yellow3 exec_no_uid: Color::AnsiValue(40), // Green3 no_exec_no_uid: Color::AnsiValue(184), // Yellow3 }, - dir: theme::Dir { + dir: color::Dir { uid: Color::AnsiValue(33), // DodgerBlue1 no_uid: Color::AnsiValue(33), // DodgerBlue1 }, pipe: Color::AnsiValue(44), // DarkTurquoise - symlink: theme::Symlink { + symlink: color::Symlink { default: Color::AnsiValue(44), // DarkTurquoise broken: Color::AnsiValue(124), // Red3 missing_target: Color::AnsiValue(124), // Red3 @@ -367,22 +367,22 @@ mod elem { socket: Color::AnsiValue(44), // DarkTurquoise special: Color::AnsiValue(44), // DarkTurquoise }, - date: theme::Date { + date: color::Date { hour_old: Color::AnsiValue(40), // Green3 day_old: Color::AnsiValue(42), // SpringGreen2 older: Color::AnsiValue(36), // DarkCyan }, - size: theme::Size { + size: color::Size { none: Color::AnsiValue(245), // Grey small: Color::AnsiValue(229), // Wheat1 medium: Color::AnsiValue(216), // LightSalmon1 large: Color::AnsiValue(172), // Orange3 }, - inode: theme::INode { + inode: color::INode { valid: Color::AnsiValue(13), // Pink invalid: Color::AnsiValue(245), // Grey }, - links: theme::Links { + links: color::Links { valid: Color::AnsiValue(13), // Pink invalid: Color::AnsiValue(245), // Grey }, diff --git a/src/display.rs b/src/display.rs index 472bf26ba..631ed1a28 100644 --- a/src/display.rs +++ b/src/display.rs @@ -408,11 +408,11 @@ mod tests { use super::*; use crate::color; use crate::color::Colors; - use crate::flags::HyperlinkOption; + use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme}; use crate::icon::Icons; use crate::meta::{FileType, Name}; use crate::Config; - use crate::{app, flags, icon, sort}; + use crate::{app, flags, sort}; use assert_fs::prelude::*; use std::path::Path; @@ -438,7 +438,7 @@ mod tests { let output = name .render( &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, ) @@ -472,7 +472,7 @@ mod tests { let output = name .render( &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::Fancy, " ".to_string()), + &Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, ) @@ -505,7 +505,7 @@ mod tests { let output = name .render( &Colors::new(color::ThemeOption::NoLscolors), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, ) @@ -546,7 +546,7 @@ mod tests { let output = name .render( &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), &DisplayOption::FileName, HyperlinkOption::Never, ) @@ -608,7 +608,7 @@ mod tests { &metas, &flags, &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), ); assert_eq!("one.d\n├── .hidden\n└── two\n", output); @@ -638,7 +638,7 @@ mod tests { &metas, &flags, &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), ); let length_before_b = |i| -> usize { @@ -677,7 +677,7 @@ mod tests { &metas, &flags, &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), ); assert_eq!(output.lines().nth(1).unwrap().chars().next().unwrap(), '└'); @@ -715,7 +715,7 @@ mod tests { &metas, &flags, &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), ); assert!(output.ends_with("└── two\n")); @@ -744,7 +744,7 @@ mod tests { &metas, &flags, &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), ); dir.close().unwrap(); @@ -776,7 +776,7 @@ mod tests { &metas, &flags, &Colors::new(color::ThemeOption::NoColor), - &Icons::new(icon::Theme::NoIcon, " ".to_string()), + &Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()), ); dir.close().unwrap(); diff --git a/src/flags/icons.rs b/src/flags/icons.rs index f10cdb1a3..96f47f5c6 100644 --- a/src/flags/icons.rs +++ b/src/flags/icons.rs @@ -136,7 +136,7 @@ impl Configurable for IconTheme { /// this returns its corresponding variant in a [Some]. /// Otherwise this returns [None]. fn from_config(config: &Config) -> Option { - config.icons.as_ref().and_then(|icon| icon.theme) + config.icons.as_ref().and_then(|icon| icon.theme.clone()) } } diff --git a/src/icon.rs b/src/icon.rs index fa2f7dced..fce3240fe 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -21,7 +21,7 @@ impl Icons { } else { Some(IconTheme::default()) } - }, + } (_, _, FlagTheme::Unicode) => Some(IconTheme::unicode()), }; @@ -79,7 +79,8 @@ impl Icons { #[cfg(test)] mod test { - use super::{Icons, Theme}; + use super::{IconTheme, Icons}; + use crate::flags::{IconOption, IconTheme as FlagTheme}; use crate::meta::Meta; use std::fs::File; use tempfile::tempdir; @@ -91,7 +92,7 @@ mod test { File::create(&file_path).expect("failed to create file"); let meta = Meta::from_path(&file_path, false).unwrap(); - let icon = Icons::new(Theme::NoIcon, " ".to_string()); + let icon = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()); let icon = icon.get(&meta.name); assert_eq!(icon, ""); @@ -104,7 +105,7 @@ mod test { File::create(&file_path).expect("failed to create file"); let meta = Meta::from_path(&file_path, false).unwrap(); - let icon = Icons::new(Theme::Fancy, " ".to_string()); + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); let icon_str = icon.get(&meta.name); assert_eq!(icon_str, format!("{}{}", "\u{f016}", icon.icon_separator)); //  @@ -117,10 +118,15 @@ mod test { File::create(&file_path).expect("failed to create file"); let meta = Meta::from_path(&file_path, false).unwrap(); - let icon = Icons::new(Theme::Unicode, " ".to_string()); + let icon = Icons::new( + false, + IconOption::Always, + FlagTheme::Unicode, + " ".to_string(), + ); let icon_str = icon.get(&meta.name); - assert_eq!(icon_str, format!("{}{}", "\u{1f5cb}", icon.icon_separator)); + assert_eq!(icon_str, format!("{}{}", "\u{1f4c4}", icon.icon_separator)); } #[test] @@ -129,7 +135,7 @@ mod test { let file_path = tmp_dir.path(); let meta = Meta::from_path(file_path, false).unwrap(); - let icon = Icons::new(Theme::Fancy, " ".to_string()); + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); let icon_str = icon.get(&meta.name); assert_eq!(icon_str, format!("{}{}", "\u{f115}", icon.icon_separator)); //  @@ -141,10 +147,15 @@ mod test { let file_path = tmp_dir.path(); let meta = Meta::from_path(file_path, false).unwrap(); - let icon = Icons::new(Theme::Unicode, " ".to_string()); + let icon = Icons::new( + false, + IconOption::Always, + FlagTheme::Unicode, + " ".to_string(), + ); let icon_str = icon.get(&meta.name); - assert_eq!(icon_str, format!("{}{}", "\u{1f5c1}", icon.icon_separator)); + assert_eq!(icon_str, format!("{}{}", "\u{1f4c2}", icon.icon_separator)); } #[test] @@ -153,7 +164,7 @@ mod test { let file_path = tmp_dir.path(); let meta = Meta::from_path(file_path, false).unwrap(); - let icon = Icons::new(Theme::Fancy, " ".to_string()); + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); let icon_str = icon.get(&meta.name); assert_eq!(icon_str, format!("{}{}", "\u{f115}", icon.icon_separator)); //  @@ -163,12 +174,12 @@ mod test { fn get_icon_by_name() { let tmp_dir = tempdir().expect("failed to create temp dir"); - for (file_name, file_icon) in &Icons::get_default_icons_by_name() { + for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() { let file_path = tmp_dir.path().join(file_name); File::create(&file_path).expect("failed to create file"); let meta = Meta::from_path(&file_path, false).unwrap(); - let icon = Icons::new(Theme::Fancy, " ".to_string()); + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); let icon_str = icon.get(&meta.name); assert_eq!(icon_str, format!("{}{}", file_icon, icon.icon_separator)); @@ -179,12 +190,12 @@ mod test { fn get_icon_by_extension() { let tmp_dir = tempdir().expect("failed to create temp dir"); - for (ext, file_icon) in &Icons::get_default_icons_by_extension() { + for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() { let file_path = tmp_dir.path().join(format!("file.{}", ext)); File::create(&file_path).expect("failed to create file"); let meta = Meta::from_path(&file_path, false).unwrap(); - let icon = Icons::new(Theme::Fancy, " ".to_string()); + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); let icon_str = icon.get(&meta.name); assert_eq!(icon_str, format!("{}{}", file_icon, icon.icon_separator)); diff --git a/src/meta/name.rs b/src/meta/name.rs index 4d5052b47..6fc61eacd 100644 --- a/src/meta/name.rs +++ b/src/meta/name.rs @@ -206,8 +206,8 @@ mod test { use super::DisplayOption; use super::Name; use crate::color::{self, Colors}; - use crate::flags::HyperlinkOption; - use crate::icon::{self, Icons}; + use crate::flags::{HyperlinkOption, IconOption, IconTheme as FlagTheme}; + use crate::icon::Icons; use crate::meta::FileType; use crate::meta::Meta; #[cfg(unix)] @@ -227,7 +227,7 @@ mod test { #[cfg(unix)] // Windows uses different default permissions fn test_print_file_name() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::Fancy, " ".to_string()); + let icons = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); // Create the file; let file_path = tmp_dir.path().join("file.txt"); @@ -252,7 +252,7 @@ mod test { #[test] fn test_print_dir_name() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::Fancy, " ".to_string()); + let icons = &Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); // Create the directory let dir_path = tmp_dir.path().join("directory"); @@ -276,7 +276,7 @@ mod test { #[cfg(unix)] // Symlinks are hard on Windows fn test_print_symlink_name_file() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::Fancy, " ".to_string()); + let icons = &Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); // Create the file; let file_path = tmp_dir.path().join("file.tmp"); @@ -309,7 +309,7 @@ mod test { #[cfg(unix)] // Symlinks are hard on Windows fn test_print_symlink_name_dir() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::Fancy, " ".to_string()); + let icons = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); // Create the directory; let dir_path = tmp_dir.path().join("tmp.d"); @@ -342,7 +342,7 @@ mod test { #[cfg(unix)] fn test_print_other_type_name() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::Fancy, " ".to_string()); + let icons = &Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); // Create the pipe; let pipe_path = tmp_dir.path().join("pipe.tmp"); @@ -372,7 +372,7 @@ mod test { #[test] fn test_print_without_icon_or_color() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::NoIcon, " ".to_string()); + let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()); // Create the file; let file_path = tmp_dir.path().join("file.txt"); @@ -397,7 +397,7 @@ mod test { #[test] fn test_print_hyperlink() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::NoIcon, " ".to_string()); + let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string()); // Create the file; let file_path = tmp_dir.path().join("file.txt"); @@ -623,7 +623,7 @@ mod test { #[cfg(unix)] fn test_special_chars_in_filename() { let tmp_dir = tempdir().expect("failed to create temp dir"); - let icons = Icons::new(icon::Theme::Fancy, " ".to_string()); + let icons = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); // Create the file; let file_path = tmp_dir.path().join("file\ttab.txt"); diff --git a/src/theme/color.rs b/src/theme/color.rs index ef94c36f4..33446f676 100644 --- a/src/theme/color.rs +++ b/src/theme/color.rs @@ -379,13 +379,14 @@ tree-edge: 245 #[cfg(test)] mod tests { - use super::Theme; + use super::ColorTheme; + use crate::theme::Theme; #[test] fn test_default_theme() { assert_eq!( - Theme::default_dark(), - Theme::with_yaml(Theme::default_yaml()).unwrap() + ColorTheme::default_dark(), + Theme::with_yaml(ColorTheme::default_yaml()).unwrap() ); } @@ -396,10 +397,10 @@ mod tests { let dir = assert_fs::TempDir::new().unwrap(); let theme = dir.path().join("theme.yaml"); let mut file = File::create(&theme).unwrap(); - writeln!(file, "{}", Theme::default_yaml()).unwrap(); + writeln!(file, "{}", ColorTheme::default_yaml()).unwrap(); assert_eq!( - Theme::default_dark(), + ColorTheme::default_dark(), Theme::from_path(theme.to_str().unwrap()).unwrap() ); } @@ -408,8 +409,8 @@ mod tests { fn test_empty_theme_return_default() { // Must contain one field at least // ref https://github.com/dtolnay/serde-yaml/issues/86 - let empty_theme = Theme::with_yaml("user: 230").unwrap(); // 230 is the default value - let default_theme = Theme::default_dark(); + let empty_theme: ColorTheme = Theme::with_yaml("user: 230").unwrap(); // 230 is the default value + let default_theme = ColorTheme::default_dark(); assert_eq!(empty_theme, default_theme); } @@ -417,8 +418,8 @@ mod tests { fn test_first_level_theme_return_default_but_changed() { // Must contain one field at least // ref https://github.com/dtolnay/serde-yaml/issues/86 - let empty_theme = Theme::with_yaml("user: 130").unwrap(); - let mut theme = Theme::default_dark(); + let empty_theme: ColorTheme = Theme::with_yaml("user: 130").unwrap(); + let mut theme = ColorTheme::default_dark(); use crossterm::style::Color; theme.user = Color::AnsiValue(130); assert_eq!(empty_theme, theme); @@ -428,13 +429,13 @@ mod tests { fn test_second_level_theme_return_default_but_changed() { // Must contain one field at least // ref https://github.com/dtolnay/serde-yaml/issues/86 - let empty_theme = Theme::with_yaml( + let empty_theme: ColorTheme = Theme::with_yaml( r#"--- permission: read: 130"#, ) .unwrap(); - let mut theme = Theme::default_dark(); + let mut theme = ColorTheme::default_dark(); use crossterm::style::Color; theme.permission.read = Color::AnsiValue(130); assert_eq!(empty_theme, theme); diff --git a/src/theme/icon.rs b/src/theme/icon.rs index 5ad3e45f6..b2fde9f52 100644 --- a/src/theme/icon.rs +++ b/src/theme/icon.rs @@ -81,7 +81,8 @@ impl IconTheme { } } - fn get_default_icons_by_name() -> HashMap { + // pub only for testing in icons.rs + pub fn get_default_icons_by_name() -> HashMap { // Note: filenames must be lower-case [ (".trash", "\u{f1f8}"), // "" @@ -208,7 +209,8 @@ impl IconTheme { .collect::>() } - fn get_default_icons_by_extension() -> HashMap { + // pub only for testing in icons.rs + pub fn get_default_icons_by_extension() -> HashMap { // Note: extensions must be lower-case [ ("1", "\u{f02d}"), // ""