Skip to content

Commit

Permalink
🔍 🔨 fix tests to fit icon theme
Browse files Browse the repository at this point in the history
Signed-off-by: zwPapEr <zw.paper@gmail.com>
  • Loading branch information
zwpaper committed Aug 9, 2022
1 parent 60c019a commit 51a345d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 65 deletions.
30 changes: 15 additions & 15 deletions src/color.rs
Expand Up @@ -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());
Expand All @@ -311,31 +311,31 @@ mod tests {
fn test_color_new_default_theme() {
assert_eq!(
Colors::new(ThemeOption::Default).theme,
Some(Theme::default_dark()),
Some(ColorTheme::default_dark()),
);
}

#[test]
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()),
);
}
}

#[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,
Expand All @@ -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
Expand All @@ -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
},
Expand Down
24 changes: 12 additions & 12 deletions src/display.rs
Expand Up @@ -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;

Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(), '└');
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/flags/icons.rs
Expand Up @@ -136,7 +136,7 @@ impl Configurable<Self> for IconTheme {
/// this returns its corresponding variant in a [Some].
/// Otherwise this returns [None].
fn from_config(config: &Config) -> Option<Self> {
config.icons.as_ref().and_then(|icon| icon.theme)
config.icons.as_ref().and_then(|icon| icon.theme.clone())
}
}

Expand Down
39 changes: 25 additions & 14 deletions src/icon.rs
Expand Up @@ -21,7 +21,7 @@ impl Icons {
} else {
Some(IconTheme::default())
}
},
}
(_, _, FlagTheme::Unicode) => Some(IconTheme::unicode()),
};

Expand Down Expand Up @@ -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;
Expand All @@ -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, "");
Expand All @@ -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)); // 
Expand All @@ -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]
Expand All @@ -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)); // 
Expand All @@ -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]
Expand All @@ -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)); // 
Expand All @@ -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));
Expand All @@ -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));
Expand Down

0 comments on commit 51a345d

Please sign in to comment.