use crate::test_support::{render_block, render_style};
use crate::{BlockStyle, Color, ScrollbarRole, TerminalBackground, TextStyle};
use super::*;
const TOKENS: SemanticTokens = SemanticTokens {
text: Color::Ansi(1),
text_muted: Color::Ansi(2),
background: Color::Ansi(3),
surface: Color::Ansi(4),
accent: Color::Ansi(5),
accent_text: Color::Ansi(6),
success: Color::Ansi(7),
warning: Color::Ansi(8),
error: Color::Ansi(9),
border: Color::Ansi(10),
};
#[test]
fn component_theme_follows_the_token_mapping() {
let components = ComponentTheme::from_tokens(&TOKENS);
let expected = [
(ComponentRole::Body, "\x1b[31mx\x1b[0m"),
(ComponentRole::Muted, "\x1b[2;32mx\x1b[0m"),
(ComponentRole::Accent, "\x1b[1;35mx\x1b[0m"),
(ComponentRole::Success, "\x1b[37mx\x1b[0m"),
(ComponentRole::Error, "\x1b[1;91mx\x1b[0m"),
(ComponentRole::PromptQuestion, "\x1b[1;35mx\x1b[0m"),
(ComponentRole::PromptAnswer, "\x1b[31mx\x1b[0m"),
(ComponentRole::PromptPlaceholder, "\x1b[3;32mx\x1b[0m"),
(ComponentRole::PromptCursor, "\x1b[1;35mx\x1b[0m"),
(ComponentRole::PromptOption, "\x1b[31mx\x1b[0m"),
(ComponentRole::PromptOptionSelected, "\x1b[1;36;45mx\x1b[0m"),
(ComponentRole::PromptButton, "\x1b[31;44mx\x1b[0m"),
(ComponentRole::PromptButtonFocused, "\x1b[1;36;45mx\x1b[0m"),
(ComponentRole::PromptHelp, "\x1b[2;32mx\x1b[0m"),
(ComponentRole::PromptError, "\x1b[91mx\x1b[0m"),
];
for (role, painted) in expected {
assert_eq!(render_style(components.get_text_style(role), "x"), painted);
}
assert_eq!(
render_block(components.get_panel(), "x"),
"\x1b[92m╭───╮\x1b[0m\n\x1b[92m│\x1b[0m\x1b[31;44m x \x1b[0m\x1b[92m│\x1b[0m\n\x1b[92m╰───╯\x1b[0m"
);
assert_eq!(
render_block(components.get_panel_focused(), "x"),
"\x1b[35m╭───╮\x1b[0m\n\x1b[35m│\x1b[0m\x1b[31;44m x \x1b[0m\x1b[35m│\x1b[0m\n\x1b[35m╰───╯\x1b[0m"
);
assert_eq!(
render_style(components.get_list_style(ListRole::Item), "x"),
"\x1b[31mx\x1b[0m"
);
assert_eq!(
render_style(components.get_list_style(ListRole::Enumerator), "x"),
"\x1b[32mx\x1b[0m"
);
assert_eq!(
render_style(components.get_tree_style(TreeRole::Root), "x"),
"\x1b[1;31mx\x1b[0m"
);
assert_eq!(
render_style(components.get_tree_style(TreeRole::Item), "x"),
"\x1b[31mx\x1b[0m"
);
assert_eq!(
render_style(components.get_tree_style(TreeRole::Connector), "x"),
"\x1b[32mx\x1b[0m"
);
assert_eq!(
render_style(components.get_scrollbar_style(ScrollbarRole::Thumb), "x"),
"\x1b[35mx\x1b[0m"
);
for role in [
ScrollbarRole::Track,
ScrollbarRole::Begin,
ScrollbarRole::End,
] {
assert_eq!(
render_style(components.get_scrollbar_style(role), "x"),
"\x1b[92mx\x1b[0m"
);
}
assert_eq!(
render_block(components.get_table_style(TableRole::Header), "x"),
"\x1b[1;31mx\x1b[0m"
);
assert_eq!(
render_block(components.get_table_style(TableRole::Cell), "x"),
"\x1b[31mx\x1b[0m"
);
assert_eq!(
render_style(components.table().get_border_style(), "x"),
"\x1b[92mx\x1b[0m"
);
}
#[test]
fn custom_tree_style_replaces_the_default() {
let components = ComponentTheme::from_tokens(&TOKENS)
.tree_style(TreeRole::Connector, TextStyle::new().underlined());
assert_eq!(
render_style(components.get_tree_style(TreeRole::Connector), "branch"),
"\x1b[4mbranch\x1b[0m"
);
}
#[test]
fn list_roles_are_independent_from_tree_roles() {
let components = ComponentTheme::from_tokens(&TOKENS)
.list_style(ListRole::Enumerator, TextStyle::new().underlined());
assert_eq!(
render_style(components.get_list_style(ListRole::Enumerator), "marker"),
"\x1b[4mmarker\x1b[0m"
);
assert_eq!(
render_style(components.get_tree_style(TreeRole::Connector), "branch"),
"\x1b[32mbranch\x1b[0m"
);
}
#[test]
fn list_roles_resolve_through_the_theme_contract() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(
&theme.text_style(ListRole::Item),
theme.components().get_list_style(ListRole::Item)
);
}
#[test]
fn theme_list_shortcut_delegates_to_the_canonical_presentation() {
let theme = Theme::from_tokens(TOKENS);
let list = crate::List::new().item("item");
assert_eq!(theme.list(&list), theme.components().list().compose(&list));
}
#[test]
fn tree_roles_resolve_through_the_theme_contract() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(
&theme.text_style(TreeRole::Root),
theme.components().get_tree_style(TreeRole::Root)
);
}
#[test]
fn theme_tree_shortcut_delegates_to_the_canonical_presentation() {
let theme = Theme::from_tokens(TOKENS);
let tree = crate::Tree::new().child("item");
assert_eq!(theme.tree(&tree), theme.components().tree().compose(&tree));
}
#[test]
fn theme_and_component_theme_keep_value_equality_after_cloning() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(theme.clone(), theme);
assert_eq!(theme.components().clone(), *theme.components());
}
#[test]
fn custom_table_style_replaces_the_default() {
let components = ComponentTheme::from_tokens(&TOKENS)
.table_style(TableRole::Header, BlockStyle::new().underlined());
assert_eq!(
render_block(components.get_table_style(TableRole::Header), "head"),
"\x1b[4mhead\x1b[0m"
);
assert_eq!(
render_block(components.get_table_style(TableRole::Cell), "cell"),
"\x1b[31mcell\x1b[0m"
);
}
#[test]
fn table_roles_resolve_through_the_theme_contract() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(
&theme.block_style(TableRole::Header),
theme.components().get_table_style(TableRole::Header)
);
}
#[test]
fn panel_roles_resolve_through_the_theme_contract() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(
&theme.block_style(PanelRole::Panel),
theme.components().get_panel()
);
assert_eq!(
&theme.block_style(PanelRole::PanelFocused),
theme.components().get_panel_focused()
);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AppRole {
ReportTitle,
}
impl TextThemeRole for AppRole {
fn resolve(self, theme: &Theme) -> TextStyle {
match self {
Self::ReportTitle => theme
.text_style(ComponentRole::Body)
.foreground(theme.tokens().accent)
.bold(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Heading(u8);
impl TextThemeRole for Heading {
fn resolve(self, theme: &Theme) -> TextStyle {
let style = theme.text_style(ComponentRole::Body);
match self.0 {
1 => style.foreground(theme.tokens().accent).bold(),
_ => style.foreground(theme.tokens().text_muted),
}
}
}
#[test]
fn application_roles_resolve_from_a_token_and_a_built_in_role() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(
render_style(&theme.text_style(AppRole::ReportTitle), "report"),
"\x1b[1;35mreport\x1b[0m"
);
assert_eq!(
render_style(&theme.text_style(ComponentRole::Body), "body"),
"\x1b[31mbody\x1b[0m"
);
}
#[test]
fn application_roles_follow_an_overridden_built_in_role() {
let components = ComponentTheme::from_tokens(&TOKENS)
.text_style(ComponentRole::Body, TextStyle::new().italic());
let theme = Theme::new(TOKENS, components);
assert_eq!(
render_style(&theme.text_style(AppRole::ReportTitle), "report"),
"\x1b[1;3;35mreport\x1b[0m"
);
}
#[test]
fn parameterized_application_roles_resolve_per_value() {
let theme = Theme::from_tokens(TOKENS);
assert_eq!(
render_style(&theme.text_style(Heading(1)), "h1"),
"\x1b[1;35mh1\x1b[0m"
);
assert_eq!(
render_style(&theme.text_style(Heading(2)), "h2"),
"\x1b[32mh2\x1b[0m"
);
}
#[test]
fn theme_set_selects_between_a_light_and_dark_theme() {
let light = Theme::from_tokens(TOKENS);
let dark = Theme::from_tokens(SemanticTokens {
text: Color::Ansi(9),
..TOKENS
});
let themes = ThemeSet::new(light, dark);
assert_eq!(
themes
.select(ColorScheme::Light)
.text_style(ComponentRole::Body),
themes.light().text_style(ComponentRole::Body)
);
assert_eq!(
render_style(
&themes
.select(ColorScheme::Dark)
.text_style(ComponentRole::Body),
"x",
),
"\x1b[91mx\x1b[0m"
);
}
#[test]
fn explicit_theme_modes_ignore_an_observed_background() {
let white = TerminalBackground::new(u16::MAX, u16::MAX, u16::MAX);
let black = TerminalBackground::new(0, 0, 0);
assert_eq!(ThemeMode::Light.resolve(Some(black)), ColorScheme::Light);
assert_eq!(ThemeMode::Dark.resolve(Some(white)), ColorScheme::Dark);
}
#[test]
fn automatic_theme_mode_uses_its_explicit_fallback() {
assert_eq!(
ThemeMode::Auto {
fallback: ColorScheme::Light,
}
.resolve(None),
ColorScheme::Light
);
assert_eq!(
ThemeMode::Auto {
fallback: ColorScheme::Dark,
}
.resolve(None),
ColorScheme::Dark
);
}
#[test]
fn automatic_theme_mode_classifies_across_the_luminance_boundary() {
assert_eq!(
ThemeMode::Auto {
fallback: ColorScheme::Light,
}
.resolve(Some(TerminalBackground::new(48_191, 48_191, 48_191))),
ColorScheme::Dark
);
assert_eq!(
ThemeMode::Auto {
fallback: ColorScheme::Dark,
}
.resolve(Some(TerminalBackground::new(48_192, 48_192, 48_192))),
ColorScheme::Light
);
}