Skip to content

Commit

Permalink
feat: Remove the duplicated tests and make them into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed May 17, 2024
1 parent d2adbf9 commit e287bf9
Showing 1 changed file with 36 additions and 165 deletions.
201 changes: 36 additions & 165 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use tui::{

#[test]
fn test_empty_op() {
use ansi_to_tui::IntoText;
let string = b"\x1b[32mGREEN\x1b[mFOO\nFOO";
let output = Text::from(vec![
Line::from(vec![
Expand All @@ -18,33 +17,32 @@ fn test_empty_op() {
]),
Line::from(Span::styled("FOO", Style::reset())),
]);
assert_eq!(string.into_text().unwrap(), output);
test_both(string, output);
}

#[test]
fn test_string() {
use ansi_to_tui::IntoText;
let string: Vec<u8> = "FOO".to_string().bytes().collect();
assert_eq!(string.into_text().unwrap(), Text::raw("FOO"));
test_both(string, Text::raw("FOO"));
}

#[test]
fn test_unicode() {
// these are 8 byte unicode charachters
// first 4 bytes are for the unicode and the last 4 bytes are for the color / variant
let bytes = "AAA🅱️🅱️🅱️".as_bytes().to_vec();
let output = Ok(Text::raw("AAA🅱️🅱️🅱️"));
assert_eq!(bytes.into_text(), output);
let output = Text::raw("AAA🅱️🅱️🅱️");
test_both(bytes, output);
}

#[test]
fn test_ascii_rgb() {
let bytes: Vec<u8> = b"\x1b[38;2;100;100;100mAAABBB".to_vec();
let output = Ok(Text::from(Span::styled(
let output = Text::from(Span::styled(
"AAABBB",
Style::default().fg(Color::Rgb(100, 100, 100)),
)));
assert_eq!(bytes.into_text(), output);
));
test_both(bytes, output);
}

// #[test]
Expand All @@ -56,7 +54,7 @@ fn test_ascii_rgb() {
#[test]
fn test_ascii_newlines() {
let bytes = "LINE_1\n\n\n\n\n\n\nLINE_8".as_bytes().to_vec();
let output = Ok(Text::from(vec![
let output = Text::from(vec![
Line::from("LINE_1"),
Line::from(""),
Line::from(""),
Expand All @@ -65,67 +63,65 @@ fn test_ascii_newlines() {
Line::from(""),
Line::from(""),
Line::from("LINE_8"),
]));
]);

// println!("{:#?}", bytes.into_text());
assert_eq!(bytes.into_text(), output);
test_both(bytes, output);
}

#[test]
fn test_reset() {
let string = "\x1b[33mA\x1b[0mB";
let output = Ok(Text::from(Line::from(vec![
let output = Text::from(Line::from(vec![
Span::styled("A", Style::default().fg(Color::Yellow)),
Span::styled("B", Style::reset()),
])));
assert_eq!(string.into_text(), output);
]));
test_both(string, output);
}

#[test]
fn test_screen_modes() {
let bytes: Vec<u8> = b"\x1b[?25hAAABBB".to_vec();
let output = Ok(Text::styled(
let output = Text::styled(
"AAABBB", // or "AAABBB"
Style::default(),
));
assert_eq!(bytes.into_text(), output);
);
test_both(bytes, output);
}

#[test]
fn test_cursor_shape_and_color() {
// malformed -> malformed -> empty
let bytes: Vec<u8> = b"\x1b[4 q\x1b]12;#fab1ed\x07".to_vec();
let output = Ok(Text::raw(""));
assert_eq!(bytes.into_text(), output);
let output = Text::raw("");
test_both(bytes, output);
}

#[test]
fn test_malformed_simple() {
let bytes: Vec<u8> = b"\x1b[".to_vec();
let output = Ok(Text::raw(""));
assert_eq!(bytes.into_text(), output);
let output = Text::raw("");
test_both(bytes, output);
}

#[test]
fn test_malformed_complex() {
let bytes: Vec<u8> = b"\x1b\x1b[0\x1b[m\x1b".to_vec();
let output = Ok(Text::raw(""));
assert_eq!(bytes.into_text(), output);
let output = Text::raw("");
test_both(bytes, output);
}

#[test]
fn empty_span() {
// Yellow -> Red -> Green -> "Hello" -> Reset -> "World"
let bytes: Vec<u8> = b"\x1b[33m\x1b[31m\x1b[32mHello\x1b[0mWorld".to_vec();
let output = Ok(Text::from(Line::from(vec![
let output = Text::from(Line::from(vec![
// Not sure whether to keep this empty span or remove it somehow
Span::styled("", Style::default().fg(Color::Yellow)),
// Span::styled("", Style::default().fg(Color::Red)),
Span::styled("Hello", Style::default().fg(Color::Green)),
Span::styled("World", Style::reset()),
])));
// dbg!(bytes.clone().into_text().unwrap());
assert_eq!(bytes.into_text(), output);
]));
test_both(bytes, output);
}

#[test]
Expand All @@ -134,7 +130,7 @@ fn test_color_and_style_reset() {
"\u{1b}[32m* \u{1b}[0mRunning before-startup command \u{1b}[1mcommand\u{1b}[0m=make my-simple-package.cabal\n\
\u{1b}[32m* \u{1b}[0m$ make my-simple-package.cabal\n\
Build profile: -w ghc-9.0.2 -O1\n").into_bytes();
let output = Ok(Text::from(vec![
let output = Text::from(vec![
Line::from(vec![
Span::styled("* ", Style::default().fg(Color::Green)),
Span::styled("Running before-startup command ", Style::reset()),
Expand All @@ -149,141 +145,16 @@ fn test_color_and_style_reset() {
"Build profile: -w ghc-9.0.2 -O1",
Style::reset(),
)]),
]));
assert_eq!(bytes.into_text(), output);
]);
test_both(bytes, output);
}

#[cfg(feature = "zero-copy")]
mod zero_copy {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_string() {
use ansi_to_tui::IntoText;
let string: Vec<u8> = "FOO".to_string().bytes().collect();
println!("{:?}", string.to_text().unwrap());
}

#[test]
fn test_unicode() {
// these are 8 byte unicode charachters
// first 4 bytes are for the unicode and the last 4 bytes are for the color / variant
let bytes = "AAA🅱️🅱️🅱️".as_bytes().to_vec();
let output = Ok(Text::raw("AAA🅱️🅱️🅱️"));
assert_eq!(bytes.to_text(), output);
}

#[test]
fn test_ascii_rgb() {
let bytes: Vec<u8> = b"\x1b[38;2;100;100;100mAAABBB".to_vec();
let output = Ok(Text::from(Span::styled(
"AAABBB",
Style::default().fg(Color::Rgb(100, 100, 100)),
)));
assert_eq!(bytes.to_text(), output);
}

// #[test]
// fn test_ascii_multi() {
// let bytes = "\x1b[31m\x1b[4m\x1b[1mHELLO".as_bytes().to_vec();
// println!("{:#?}", ansi_to_text(bytes));
// }

#[test]
fn test_ascii_newlines() {
let bytes = "LINE_1\n\n\n\n\n\n\nLINE_8".as_bytes().to_vec();
let output = Ok(Text::from(vec![
Line::from("LINE_1"),
Line::from(""),
Line::from(""),
Line::from(""),
Line::from(""),
Line::from(""),
Line::from(""),
Line::from("LINE_8"),
]));

assert_eq!(bytes.to_text(), output);
}

#[test]
fn test_reset() {
let string = "\x1b[33mA\x1b[0mB";
let output = Ok(Text::from(Line::from(vec![
Span::styled("A", Style::default().fg(Color::Yellow)),
Span::styled("B", Style::reset()),
])));
assert_eq!(string.to_text(), output);
}

#[test]
fn test_screen_modes() {
let bytes: Vec<u8> = b"\x1b[?25hAAABBB".to_vec();
let output = Ok(Text::styled(
"AAABBB", // or "AAABBB"
Style::default(),
));
assert_eq!(bytes.to_text(), output);
}

#[test]
fn test_cursor_shape_and_color() {
let bytes: Vec<u8> = b"\x1b[4 q\x1b]12;#fab1ed\x07".to_vec();
let output = Ok(Text::raw(""));
assert_eq!(bytes.to_text(), bytes.into_text());
assert_eq!(bytes.to_text(), output);
}

#[test]
fn test_malformed_simple() {
let bytes: Vec<u8> = b"\x1b[".to_vec();
let output = Ok(Text::raw(""));
assert_eq!(bytes.to_text(), output);
}

#[test]
fn test_malformed_complex() {
let bytes: Vec<u8> = b"\x1b\x1b[0\x1b[m\x1b".to_vec();
let output = Ok(Text::raw(""));
assert_eq!(bytes.to_text(), output);
}

#[test]
fn empty_span() {
let bytes: Vec<u8> = b"\x1b[33m\x1b[31m\x1b[32mHello\x1b[0mWorld".to_vec();
let output = Ok(Text::from(Line::from(vec![
// Not sure whether to keep this empty span or remove it somehow
Span::styled("", Style::default().fg(Color::Yellow)),
Span::styled("Hello", Style::default().fg(Color::Green)),
Span::styled("World", Style::reset()),
])));
assert_eq!(bytes.to_text(), output);
assert_eq!(bytes.to_text(), bytes.clone().into_text());
}

#[test]
fn test_color_and_style_reset() {
let bytes: Vec<u8> = String::from(
"\u{1b}[32m* \u{1b}[0mRunning before-startup command \u{1b}[1mcommand\u{1b}[0m=make my-simple-package.cabal\n\
\u{1b}[32m* \u{1b}[0m$ make my-simple-package.cabal\n\
Build profile: -w ghc-9.0.2 -O1\n").into_bytes();
let output = Ok(Text::from(vec![
Line::from(vec![
Span::styled("* ", Style::default().fg(Color::Green)),
Span::styled("Running before-startup command ", Style::reset()),
Span::styled("command", Style::reset().bold()),
Span::styled("=make my-simple-package.cabal", Style::reset()),
]),
Line::from(vec![
Span::styled("* ", Style::reset().fg(Color::Green)),
Span::styled("$ make my-simple-package.cabal", Style::reset()),
]),
Line::from(vec![Span::styled(
"Build profile: -w ghc-9.0.2 -O1",
Style::reset(),
)]),
]));
assert_eq!(bytes.into_text(), output);
}
#[cfg(test)]
pub fn test_both(bytes: impl AsRef<[u8]>, other: Text) {
let bytes = bytes.as_ref();
let zero_copy = bytes.to_text().unwrap();
let owned = bytes.into_text().unwrap();
assert_eq!(zero_copy, owned);
assert_eq!(owned, other);
assert_eq!(zero_copy, other);
}

0 comments on commit e287bf9

Please sign in to comment.