theme: 🔨 using default to return dark theme and more tests

Signed-off-by: zwPapEr <zw.paper@gmail.com>
This commit is contained in:
zwPapEr 2020-12-05 15:17:03 +08:00 committed by Abin Simon
parent 80a0a704c6
commit e88ca0feb1
5 changed files with 203 additions and 4 deletions

View file

@ -128,10 +128,10 @@ impl Colors {
pub fn new(t: ThemeOption) -> Self {
let theme = match t {
ThemeOption::NoColor => None,
ThemeOption::Default => Some(Theme::default_dark()),
ThemeOption::NoLscolors => Some(Theme::default_dark()),
ThemeOption::Default => Some(Theme::default()),
ThemeOption::NoLscolors => Some(Theme::default()),
ThemeOption::Custom(ref file) => {
Some(Theme::from_path(file).unwrap_or_else(Theme::default_dark))
Some(Theme::from_path(file).unwrap_or_else(Theme::default))
}
};
let lscolors = match t {
@ -255,8 +255,102 @@ mod tests {
#[test]
fn test_color_new_bad_custom_theme() {
assert_eq!(
Colors::new(ThemeOption::Custom("not_existed".to_string())).theme,
Colors::new(ThemeOption::Custom("not-existed".to_string())).theme,
Some(Theme::default_dark()),
);
}
}
#[cfg(test)]
mod elem {
use super::Elem;
use crate::color::{theme, Theme};
use ansi_term::Colour;
#[cfg(test)]
fn test_theme() -> Theme {
Theme {
user: Colour::Fixed(230), // Cornsilk1
group: Colour::Fixed(187), // LightYellow3
permissions: theme::Permissions {
read: Colour::Green,
write: Colour::Yellow,
exec: Colour::Red,
exec_sticky: Colour::Purple,
no_access: Colour::Fixed(245), // Grey
},
file_type: theme::FileType {
file: theme::File {
exec_uid: Colour::Fixed(40), // Green3
uid_no_exec: Colour::Fixed(184), // Yellow3
exec_no_uid: Colour::Fixed(40), // Green3
no_exec_no_uid: Colour::Fixed(184), // Yellow3
},
dir: theme::Dir {
uid: Colour::Fixed(33), // DodgerBlue1
no_uid: Colour::Fixed(33), // DodgerBlue1
},
pipe: Colour::Fixed(44), // DarkTurquoise
symlink: theme::Symlink {
default: Colour::Fixed(44), // DarkTurquoise
broken: Colour::Fixed(124), // Red3
},
block_device: Colour::Fixed(44), // DarkTurquoise
char_device: Colour::Fixed(172), // Orange3
socket: Colour::Fixed(44), // DarkTurquoise
special: Colour::Fixed(44), // DarkTurquoise
},
modified: theme::Modified {
hour_old: Colour::Fixed(40), // Green3
day_old: Colour::Fixed(42), // SpringGreen2
older: Colour::Fixed(36), // DarkCyan
},
size: theme::Size {
none: Colour::Fixed(245), // Grey
small: Colour::Fixed(229), // Wheat1
medium: Colour::Fixed(216), // LightSalmon1
large: Colour::Fixed(172), // Orange3
},
inode: theme::INode {
valid: Colour::Fixed(13), // Pink
invalid: Colour::Fixed(245), // Grey
},
}
}
#[test]
fn test_default_file() {
assert_eq!(
Elem::File {
exec: true,
uid: true
}
.get_color(&test_theme()),
Colour::Fixed(40),
);
assert_eq!(
Elem::File {
exec: false,
uid: true
}
.get_color(&test_theme()),
Colour::Fixed(184),
);
assert_eq!(
Elem::File {
exec: true,
uid: false
}
.get_color(&test_theme()),
Colour::Fixed(40),
);
assert_eq!(
Elem::File {
exec: false,
uid: false
}
.get_color(&test_theme()),
Colour::Fixed(184),
);
}
}

View file

@ -101,6 +101,13 @@ pub struct INode {
pub invalid: Colour,
}
impl Default for Theme {
fn default() -> Self {
// TODO: check terminal color and return light or dark
Self::default_dark()
}
}
impl Theme {
/// This read theme from file,
/// use the file path if it is absolute

View file

@ -44,6 +44,11 @@ pub enum ThemeOption {
impl ThemeOption {
fn from_config(config: &Config) -> ThemeOption {
if let Some(classic) = config.classic {
if classic {
return ThemeOption::NoColor;
}
}
if let Some(c) = &config.color {
if let Some(t) = &c.theme {
return t.clone();
@ -287,3 +292,72 @@ mod test_color_option {
assert_eq!(Some(ColorOption::Never), ColorOption::from_config(&c));
}
}
#[cfg(test)]
mod test_theme_option {
use super::ThemeOption;
use crate::config_file::{self, Config};
#[test]
fn test_from_config_none_default() {
assert_eq!(
ThemeOption::Default,
ThemeOption::from_config(&Config::with_none())
);
}
#[test]
fn test_from_config_default() {
let mut c = Config::with_none();
c.color = Some(config_file::Color {
when: None,
theme: Some(ThemeOption::Default),
});
assert_eq!(ThemeOption::Default, ThemeOption::from_config(&c));
}
#[test]
fn test_from_config_no_color() {
let mut c = Config::with_none();
c.color = Some(config_file::Color {
when: None,
theme: Some(ThemeOption::NoColor),
});
assert_eq!(ThemeOption::NoColor, ThemeOption::from_config(&c));
}
#[test]
fn test_from_config_no_lscolor() {
let mut c = Config::with_none();
c.color = Some(config_file::Color {
when: None,
theme: Some(ThemeOption::NoLscolors),
});
assert_eq!(ThemeOption::NoLscolors, ThemeOption::from_config(&c));
}
#[test]
fn test_from_config_bad_file_flag() {
let mut c = Config::with_none();
c.color = Some(config_file::Color {
when: None,
theme: Some(ThemeOption::Custom("not-existed".to_string())),
});
assert_eq!(
ThemeOption::Custom("not-existed".to_string()),
ThemeOption::from_config(&c)
);
}
#[test]
fn test_from_config_classic_mode() {
let mut c = Config::with_none();
c.color = Some(config_file::Color {
when: None,
theme: Some(ThemeOption::Default),
});
c.classic = Some(true);
assert_eq!(ThemeOption::NoColor, ThemeOption::from_config(&c));
}
}

View file

@ -82,6 +82,11 @@ mod test {
use crate::config_file::Config;
use crate::flags::Configurable;
#[test]
fn test_default() {
assert_eq!(SizeFlag::Default, SizeFlag::default());
}
#[test]
fn test_from_arg_matches_none() {
let argv = vec!["lsd"];
@ -113,6 +118,12 @@ mod test {
assert_eq!(Some(SizeFlag::Bytes), SizeFlag::from_arg_matches(&matches));
}
#[test]
#[should_panic]
fn test_from_arg_matches_unknonwn() {
let args = vec!["lsd", "--size", "unknown"];
let _ = app::build().get_matches_from_safe(args).unwrap();
}
#[test]
fn test_from_arg_matches_size_multi() {
let args = vec!["lsd", "--size", "bytes", "--size", "short"];

View file

@ -248,3 +248,16 @@ impl Meta {
})
}
}
#[cfg(test)]
mod tests {
use super::Meta;
#[test]
#[cfg(unix)]
fn test_from_path_path() {
let dir = assert_fs::TempDir::new().unwrap();
let meta = Meta::from_path(dir.path(), false).unwrap();
assert_eq!(meta.path, dir.path())
}
}