Skip to content

Commit

Permalink
🔍 icon theme: add tests for icon theme
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Zhang <kweizh@gmail.com>
  • Loading branch information
zwpaper committed Aug 18, 2022
1 parent b4952fa commit 8feb18d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 29 deletions.
76 changes: 57 additions & 19 deletions src/icon.rs
Expand Up @@ -86,20 +86,70 @@ mod test {
use tempfile::tempdir;

#[test]
fn get_no_icon() {
fn get_no_icon_never_tty() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();

let icon = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icon.get(&meta.name);
let icons = Icons::new(true, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);

assert_eq!(icon, "");
}
#[test]
fn get_no_icon_never_not_tty() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();

let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);

assert_eq!(icon, "");
}

#[test]
fn get_default_file_icon() {
fn get_no_icon_auto() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();

let icons = Icons::new(false, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);

assert_eq!(icon, "");
}
#[test]
fn get_icon_auto_tty() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();

let icons = Icons::new(true, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);

assert_eq!(icon, "\u{f15c} ");
}

#[test]
fn get_icon_always_tty_default_file() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false).unwrap();

let icon = Icons::new(true, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);

assert_eq!(icon_str, "\u{f016} "); // 
}

#[test]
fn get_icon_always_not_tty_default_file() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
Expand All @@ -112,7 +162,7 @@ mod test {
}

#[test]
fn get_default_file_icon_unicode() {
fn get_icon_default_file_icon_unicode() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
Expand All @@ -130,7 +180,7 @@ mod test {
}

#[test]
fn get_directory_icon() {
fn get_icon_default_directory() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false).unwrap();
Expand All @@ -142,7 +192,7 @@ mod test {
}

#[test]
fn get_directory_icon_unicode() {
fn get_icon_default_directory_unicode() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false).unwrap();
Expand All @@ -158,18 +208,6 @@ mod test {
assert_eq!(icon_str, format!("{}{}", "\u{1f4c2}", icon.icon_separator));
}

#[test]
fn get_directory_icon_with_ext() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false).unwrap();

let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);

assert_eq!(icon_str, "\u{f115} "); // 
}

#[test]
fn get_icon_by_name() {
let tmp_dir = tempdir().expect("failed to create temp dir");
Expand Down
19 changes: 9 additions & 10 deletions src/theme/color.rs
Expand Up @@ -345,9 +345,14 @@ impl ColorTheme {
tree_edge: Color::AnsiValue(245), // Grey
}
}
}

#[cfg(test)]
mod tests {
use super::ColorTheme;
use crate::theme::Theme;

#[cfg(test)]
pub fn default_yaml() -> &'static str {
fn default_yaml() -> &'static str {
r#"---
user: 230
group: 187
Expand Down Expand Up @@ -375,18 +380,12 @@ links:
tree-edge: 245
"#
}
}

#[cfg(test)]
mod tests {
use super::ColorTheme;
use crate::theme::Theme;

#[test]
fn test_default_theme() {
assert_eq!(
ColorTheme::default_dark(),
Theme::with_yaml(ColorTheme::default_yaml()).unwrap()
Theme::with_yaml(default_yaml()).unwrap()
);
}

Expand All @@ -397,7 +396,7 @@ 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, "{}", ColorTheme::default_yaml()).unwrap();
writeln!(file, "{}", default_yaml()).unwrap();

assert_eq!(
ColorTheme::default_dark(),
Expand Down
72 changes: 72 additions & 0 deletions src/theme/icon.rs
Expand Up @@ -473,3 +473,75 @@ impl IconTheme {
.collect::<HashMap<_, _>>()
}
}

#[cfg(test)]
mod tests {
use super::IconTheme;
use crate::theme::Theme;

fn partial_default_yaml() -> &'static str {
r#"---
icons-by-name:
.trash: 
.cargo: 
.emacs.d: 
a.out: 
icons-by-extension:
go: 
hs: 
rs: 
icons-by-filetype:
dir: 
file: 
pipe: 
socket: 
executable: 
symlink-dir: 
symlink-file: 
device-char: 
device-block: ﰩ
special: 
"#
}

fn check_partial_yaml(def: &IconTheme, yaml: &IconTheme) {
assert_eq!(def.icons_by_filetype.dir, yaml.icons_by_filetype.dir,);
}

#[test]
fn test_default_theme() {
let def = IconTheme::default();
let yaml = Theme::with_yaml(partial_default_yaml()).unwrap();
check_partial_yaml(&def, &yaml);
}

#[test]
fn test_tmp_partial_default_theme_file() {
use std::fs::File;
use std::io::Write;
let dir = assert_fs::TempDir::new().unwrap();
let theme = dir.path().join("icon.yaml");
let mut file = File::create(&theme).unwrap();
writeln!(file, "{}", partial_default_yaml()).unwrap();
let def = IconTheme::default();
let decoded = Theme::from_path(theme.to_str().unwrap()).unwrap();
check_partial_yaml(&def, &decoded);
}

#[test]
fn test_empty_theme_return_default() {
// Must contain one field at least
// ref https://github.com/dtolnay/serde-yaml/issues/86
let empty: IconTheme = Theme::with_yaml("icons-by-filetype:\n dir: ").unwrap(); //  is the default value
let default = IconTheme::default();
check_partial_yaml(&empty, &default);
}

#[test]
fn test_serde_dir_from_yaml() {
// Must contain one field at least
// ref https://github.com/dtolnay/serde-yaml/issues/86
let empty: IconTheme = Theme::with_yaml("icons-by-filetype:\n dir: ").unwrap();
assert_eq!(empty.icons_by_filetype.dir, "");
}
}

0 comments on commit 8feb18d

Please sign in to comment.