🔍 icon theme: add tests for icon theme

Signed-off-by: Wei Zhang <kweizh@gmail.com>
This commit is contained in:
Wei Zhang 2022-08-19 01:04:53 +08:00 committed by Abin Simon
parent 7e9a07a97d
commit 2c312f56d7
3 changed files with 138 additions and 29 deletions

View file

@ -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");
@ -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");
@ -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();
@ -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();
@ -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");

View file

@ -345,9 +345,14 @@ impl ColorTheme {
tree_edge: Color::AnsiValue(245), // Grey
}
}
}
#[cfg(test)]
pub fn default_yaml() -> &'static str {
#[cfg(test)]
mod tests {
use super::ColorTheme;
use crate::theme::Theme;
fn default_yaml() -> &'static str {
r#"---
user: 230
group: 187
@ -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()
);
}
@ -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(),

View file

@ -474,3 +474,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, "");
}
}