pub mod theme;
pub use theme::Theme;
#[cfg(feature = "arrows")]
pub use self::arrows::ArrowsTheme;
#[cfg(feature = "arrows_color")]
pub use self::arrows_color::ArrowsColorTheme;
#[cfg(feature = "signs")]
pub use self::signs::SignsTheme;
#[cfg(feature = "signs_color")]
pub use self::signs_color::SignsColorTheme;
#[cfg(feature = "arrows")]
pub mod arrows;
#[cfg(feature = "arrows_color")]
pub mod arrows_color;
#[cfg(feature = "signs")]
pub mod signs;
#[cfg(feature = "signs_color")]
pub mod signs_color;
#[cfg(test)]
mod feature_tests {
#[test]
#[cfg(feature = "arrows")]
fn test_arrows_theme_available() {
use crate::{diff, ArrowsTheme};
use std::io::Cursor;
let old = "The quick brown fox";
let new = "The quick red fox";
let mut buffer = Cursor::new(Vec::new());
let theme = ArrowsTheme::default();
diff(&mut buffer, old, new, &theme).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("<The quick brown fox"));
assert!(output.contains(">The quick red fox"));
}
#[test]
#[cfg(feature = "arrows_color")]
fn test_arrows_color_theme_available() {
use crate::{diff, ArrowsColorTheme};
use std::io::Cursor;
let old = "The quick brown fox";
let new = "The quick red fox";
let mut buffer = Cursor::new(Vec::new());
let theme = ArrowsColorTheme::default();
diff(&mut buffer, old, new, &theme).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("The quick brown fox"));
assert!(output.contains("The quick red fox"));
}
#[test]
#[cfg(feature = "signs")]
fn test_signs_theme_available() {
use crate::{diff, SignsTheme};
use std::io::Cursor;
let old = "The quick brown fox";
let new = "The quick red fox";
let mut buffer = Cursor::new(Vec::new());
let theme = SignsTheme::default();
diff(&mut buffer, old, new, &theme).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("-The quick brown fox"));
assert!(output.contains("+The quick red fox"));
}
#[test]
#[cfg(feature = "signs_color")]
fn test_signs_color_theme_available() {
use crate::{diff, SignsColorTheme};
use std::io::Cursor;
let old = "The quick brown fox";
let new = "The quick red fox";
let mut buffer = Cursor::new(Vec::new());
let theme = SignsColorTheme::default();
diff(&mut buffer, old, new, &theme).unwrap();
let output = String::from_utf8(buffer.into_inner()).expect("Not valid UTF-8");
assert!(output.contains("The quick brown fox"));
assert!(output.contains("The quick red fox"));
}
}
#[cfg(test)]
mod tests {
use crate::{ArrowsColorTheme, ArrowsTheme, SignsColorTheme, SignsTheme, Theme};
use std::borrow::Cow;
#[test]
fn test_arrows_theme_prefixes() {
let theme = ArrowsTheme::default();
assert_eq!(theme.equal_prefix(), Cow::Borrowed(" "));
assert_eq!(theme.delete_prefix(), Cow::Borrowed("<"));
assert_eq!(theme.insert_prefix(), Cow::Borrowed(">"));
}
#[test]
fn test_arrows_theme_header() {
let theme = ArrowsTheme::default();
assert_eq!(theme.header(), Cow::Borrowed("< left / > right\n"));
}
#[test]
fn test_arrows_theme_content_formatting() {
let theme = ArrowsTheme::default();
let input = "test";
assert_eq!(theme.highlight_insert(input), Cow::Borrowed(input));
assert_eq!(theme.highlight_delete(input), Cow::Borrowed(input));
assert_eq!(theme.equal_content(input), Cow::Borrowed(input));
assert_eq!(theme.delete_content(input), Cow::Borrowed(input));
assert_eq!(theme.insert_line(input), Cow::Borrowed(input));
}
#[test]
fn test_arrows_color_theme_prefixes() {
let theme = ArrowsColorTheme::default();
assert_eq!(theme.equal_prefix(), Cow::Borrowed(" "));
assert!(theme.delete_prefix().contains('<'));
assert!(theme.insert_prefix().contains('>'));
}
#[test]
fn test_arrows_color_theme_highlighting() {
let theme = ArrowsColorTheme::default();
let input = "test";
assert_ne!(theme.highlight_insert(input), Cow::Borrowed(input));
assert_ne!(theme.highlight_delete(input), Cow::Borrowed(input));
assert_ne!(theme.delete_content(input), Cow::Borrowed(input));
assert_ne!(theme.insert_line(input), Cow::Borrowed(input));
}
#[test]
fn test_signs_theme_prefixes() {
let theme = SignsTheme::default();
assert_eq!(theme.equal_prefix(), Cow::Borrowed(" "));
assert_eq!(theme.delete_prefix(), Cow::Borrowed("-"));
assert_eq!(theme.insert_prefix(), Cow::Borrowed("+"));
}
#[test]
fn test_signs_theme_header() {
let theme = SignsTheme::default();
assert_eq!(theme.header(), Cow::Borrowed("--- remove | insert +++\n"));
}
#[test]
fn test_signs_theme_defaults() {
let theme = SignsTheme::default();
assert_eq!(theme.line_end(), Cow::Borrowed("\n"));
assert_eq!(theme.trailing_lf_marker(), Cow::Borrowed("␊"));
}
#[test]
fn test_signs_color_theme_prefixes() {
let theme = SignsColorTheme::default();
assert_eq!(theme.equal_prefix(), Cow::Borrowed(" "));
assert!(theme.delete_prefix().contains('-'));
assert!(theme.insert_prefix().contains('+'));
}
#[test]
fn test_signs_color_theme_highlighting() {
let theme = SignsColorTheme::default();
let input = "test";
assert_ne!(theme.highlight_insert(input), Cow::Borrowed(input));
assert_ne!(theme.highlight_delete(input), Cow::Borrowed(input));
assert_ne!(theme.delete_content(input), Cow::Borrowed(input));
assert_ne!(theme.insert_line(input), Cow::Borrowed(input));
}
#[test]
fn test_custom_theme_minimal() {
#[derive(Debug)]
struct MinimalTheme;
impl Theme for MinimalTheme {
fn equal_prefix<'this>(&self) -> Cow<'this, str> {
"=".into()
}
fn delete_prefix<'this>(&self) -> Cow<'this, str> {
"-".into()
}
fn insert_prefix<'this>(&self) -> Cow<'this, str> {
"+".into()
}
fn header<'this>(&self) -> Cow<'this, str> {
"HEADER\n".into()
}
}
let theme = MinimalTheme;
assert_eq!(theme.equal_prefix(), Cow::Borrowed("="));
assert_eq!(theme.delete_prefix(), Cow::Borrowed("-"));
assert_eq!(theme.insert_prefix(), Cow::Borrowed("+"));
assert_eq!(theme.header(), Cow::Borrowed("HEADER\n"));
let input = "test";
assert_eq!(theme.highlight_insert(input), Cow::Borrowed(input));
assert_eq!(theme.highlight_delete(input), Cow::Borrowed(input));
assert_eq!(theme.equal_content(input), Cow::Borrowed(input));
assert_eq!(theme.delete_content(input), Cow::Borrowed(input));
assert_eq!(theme.insert_line(input), Cow::Borrowed(input));
assert_eq!(theme.line_end(), Cow::Borrowed("\n"));
assert_eq!(theme.trailing_lf_marker(), Cow::Borrowed("␊"));
}
#[test]
fn test_custom_theme_full() {
#[derive(Debug)]
struct FullTheme;
impl Theme for FullTheme {
fn equal_prefix<'this>(&self) -> Cow<'this, str> {
"=".into()
}
fn delete_prefix<'this>(&self) -> Cow<'this, str> {
"-".into()
}
fn insert_prefix<'this>(&self) -> Cow<'this, str> {
"+".into()
}
fn header<'this>(&self) -> Cow<'this, str> {
"HEADER\n".into()
}
fn highlight_insert<'this>(&self, input: &'this str) -> Cow<'this, str> {
format!("*{input}*").into()
}
fn highlight_delete<'this>(&self, input: &'this str) -> Cow<'this, str> {
format!("~{input}~").into()
}
fn equal_content<'this>(&self, input: &'this str) -> Cow<'this, str> {
format!("={input}").into()
}
fn delete_content<'this>(&self, input: &'this str) -> Cow<'this, str> {
format!("-{input}").into()
}
fn insert_line<'this>(&self, input: &'this str) -> Cow<'this, str> {
format!("+{input}").into()
}
fn line_end<'this>(&self) -> Cow<'this, str> {
"\r\n".into()
}
fn trailing_lf_marker<'this>(&self) -> Cow<'this, str> {
"[LF]".into()
}
}
let theme = FullTheme;
let input = "test";
assert_eq!(theme.equal_prefix(), Cow::Borrowed("="));
assert_eq!(theme.delete_prefix(), Cow::Borrowed("-"));
assert_eq!(theme.insert_prefix(), Cow::Borrowed("+"));
assert_eq!(theme.header(), Cow::Borrowed("HEADER\n"));
assert_eq!(
theme.highlight_insert(input),
Cow::<str>::Owned("*test*".to_string())
);
assert_eq!(
theme.highlight_delete(input),
Cow::<str>::Owned("~test~".to_string())
);
assert_eq!(
theme.equal_content(input),
Cow::<str>::Owned("=test".to_string())
);
assert_eq!(
theme.delete_content(input),
Cow::<str>::Owned("-test".to_string())
);
assert_eq!(
theme.insert_line(input),
Cow::<str>::Owned("+test".to_string())
);
assert_eq!(theme.line_end(), Cow::Borrowed("\r\n"));
assert_eq!(theme.trailing_lf_marker(), Cow::Borrowed("[LF]"));
}
}