Skip to content

Commit

Permalink
CYA tests for AnsiFlags. bumping version to 0.1.5 for first GH release
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyb983 committed Apr 12, 2022
1 parent b75a72a commit e406914
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 97 deletions.
7 changes: 7 additions & 0 deletions src/ansi/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,11 @@ mod tests {
all_but_color
);
}

#[test]
fn color_inputs() {
let _red = Ansi::from_fg(crate::Colors::Red);
let _green = Ansi::from_fg((0, 255, 0));
let _blue = Ansi::from_fg(Color::from_hex("#0000ff").unwrap());
}
}
137 changes: 137 additions & 0 deletions src/ansi/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,140 @@ impl BoolFlags for AnsiFlags {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn all_the_things() {
assert_eq!(
AnsiFlags::all(),
AnsiFlags::BOLD
| AnsiFlags::UNDERLINE
| AnsiFlags::ITALIC
| AnsiFlags::BLINK
| AnsiFlags::REVERSE
| AnsiFlags::STRIKE
);
assert!(AnsiFlags::is_all(&AnsiFlags::all()));
assert_eq!(
format!("{:?}", AnsiFlags::all()),
"BOLD | UNDERLINE | ITALIC | BLINK | REVERSE | STRIKE"
);
assert!(!AnsiFlags::is_empty(&AnsiFlags::all()));
assert!(AnsiFlags::all().contains(AnsiFlags::UNDERLINE));
assert_eq!(AnsiFlags::from_bits(0x0), Some(AnsiFlags::empty()));
assert_eq!(AnsiFlags::from_bits(0x1), Some(AnsiFlags::BOLD));
assert_eq!(AnsiFlags::from_bits(0x64), None);
assert_eq!(AnsiFlags::from_bits_truncate(200), AnsiFlags::BLINK);
unsafe {
assert_eq!(AnsiFlags::from_bits_unchecked(1), AnsiFlags::BOLD);
}
assert!((AnsiFlags::BOLD | AnsiFlags::ITALIC).intersects(AnsiFlags::BOLD));
assert_eq!(
(AnsiFlags::BOLD | AnsiFlags::ITALIC)
.intersection(AnsiFlags::BOLD | AnsiFlags::UNDERLINE | AnsiFlags::STRIKE),
AnsiFlags::BOLD
);
assert_eq!(
(AnsiFlags::BOLD | AnsiFlags::ITALIC)
.union(AnsiFlags::STRIKE | AnsiFlags::UNDERLINE | AnsiFlags::ITALIC),
AnsiFlags::BOLD | AnsiFlags::ITALIC | AnsiFlags::STRIKE | AnsiFlags::UNDERLINE
);
assert_eq!(
(AnsiFlags::BOLD | AnsiFlags::ITALIC)
.difference(AnsiFlags::STRIKE | AnsiFlags::UNDERLINE | AnsiFlags::ITALIC),
AnsiFlags::BOLD
);
assert_eq!(
(AnsiFlags::BOLD | AnsiFlags::ITALIC)
.symmetric_difference(AnsiFlags::STRIKE | AnsiFlags::UNDERLINE | AnsiFlags::ITALIC),
AnsiFlags::BOLD | AnsiFlags::STRIKE | AnsiFlags::UNDERLINE
);
assert_eq!(
(AnsiFlags::STRIKE | AnsiFlags::UNDERLINE | AnsiFlags::ITALIC).complement(),
AnsiFlags::all() - AnsiFlags::STRIKE - AnsiFlags::UNDERLINE - AnsiFlags::ITALIC
);
let mut strike = AnsiFlags::STRIKE;
strike.insert(AnsiFlags::UNDERLINE);
assert_eq!(strike, AnsiFlags::STRIKE | AnsiFlags::UNDERLINE);
assert_eq!(strike, AnsiFlags::STRIKE.insert_to(AnsiFlags::UNDERLINE));
strike.remove(AnsiFlags::UNDERLINE);
assert_eq!(strike, AnsiFlags::STRIKE);
assert_eq!(
strike,
(AnsiFlags::STRIKE | AnsiFlags::UNDERLINE).remove_to(AnsiFlags::UNDERLINE)
);
let mut bold = AnsiFlags::BOLD;
bold.toggle(AnsiFlags::all());
assert_eq!(bold, AnsiFlags::all() - AnsiFlags::BOLD);
bold.set(AnsiFlags::BOLD, true);
assert_eq!(bold, AnsiFlags::all());
bold.set(AnsiFlags::all(), false);
assert_eq!(bold, AnsiFlags::empty());
assert!(bold.is_empty());
}

#[test]
fn ops_binary() {
use std::ops::*;
let mut bold = AnsiFlags::BOLD;
let out = bold.bitand(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(out, AnsiFlags::BOLD);
bold.bitand_assign(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(out, bold);
let italic = AnsiFlags::ITALIC;
let out = italic.bitand(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(out, AnsiFlags::empty());

let mut italic = AnsiFlags::ITALIC;
let out = italic.bitor(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(
out,
AnsiFlags::BOLD | AnsiFlags::UNDERLINE | AnsiFlags::ITALIC
);
italic.bitor_assign(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(out, italic);

let mut blink = AnsiFlags::BLINK;
let out = blink.bitxor(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(
out,
AnsiFlags::BOLD | AnsiFlags::UNDERLINE | AnsiFlags::BLINK
);
blink.bitxor_assign(AnsiFlags::BOLD | AnsiFlags::UNDERLINE);
assert_eq!(out, blink);

let blink = AnsiFlags::BLINK;
let out = blink.not();
assert_eq!(out, AnsiFlags::all() - AnsiFlags::BLINK);
}

#[test]
fn other_traits() {
let mut flags = AnsiFlags::REVERSE;
flags.extend([AnsiFlags::BOLD]);
assert_eq!(flags, AnsiFlags::REVERSE | AnsiFlags::BOLD);
// flags.extend_one(AnsiFlags::ITALIC);
// assert_eq!(
// flags,
// AnsiFlags::REVERSE | AnsiFlags::BOLD | AnsiFlags::ITALIC
// );
assert!(!AnsiFlags::ITALIC.BOLD());
assert!(!AnsiFlags::ITALIC.BLINK());
assert!(!AnsiFlags::ITALIC.STRIKE());
assert!(!AnsiFlags::ITALIC.UNDERLINE());
assert!(!AnsiFlags::ITALIC.REVERSE());
assert!(AnsiFlags::ITALIC.ITALIC());
}

#[test]
fn format() {
assert_eq!(format!("{:02x}", AnsiFlags::all()), "3f");
assert_eq!(format!("{:02X}", AnsiFlags::all()), "3F");
assert_eq!(format!("{:02o}", AnsiFlags::all()), "77");
assert_eq!(format!("{:02b}", AnsiFlags::all()), "111111");
}
}
97 changes: 0 additions & 97 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,100 +21,3 @@ pub mod iter {
pub use ansi::*;
pub use color::*;
pub use styled::*;

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

const DISPLAY_PRE: &str = "\u{1b}[";
const DISPLAY_SUF: &str = "m";

fn empty_style_function() -> Ansi {
Ansi::new()
}

#[test]
fn storing_styles() {
let style1 = Ansi::new().fg((100, 200, 100)).underline();
let style2 = Ansi::new().bg((0, 0, 75)).italic().strike();

// println!("{}Here is text styled by #1.{}", style1, Ansi::reset());
// println!("{}Here is text styled by #2.{}", style2, Ansi::reset());
// println!("{}Here is more text styled by #1.{}", style1, Ansi::reset());

assert_eq!(
style1.to_string(),
format!("{}4;38;2;100;200;100{}", DISPLAY_PRE, DISPLAY_SUF)
);
assert_eq!(
style2.to_string(),
format!("{}3;9;48;2;0;0;75{}", DISPLAY_PRE, DISPLAY_SUF)
);
assert_eq!(
style1.to_string(),
format!("{}4;38;2;100;200;100{}", DISPLAY_PRE, DISPLAY_SUF)
);
assert_eq!(
style2.to_string(),
format!("{}3;9;48;2;0;0;75{}", DISPLAY_PRE, DISPLAY_SUF)
);
}

#[test]
fn style_text_basic() {
let first = "first".to_string();
let unstyled_val = style_text(&first, Ansi::new());
assert_eq!(unstyled_val, first);
let unstyled_fn = style_text(&first, empty_style_function);
assert_eq!(unstyled_fn, first);

let manual_prefix = format!("{}{}{}", DISPLAY_PRE, "4;38;2;255;0;0", DISPLAY_SUF);
let manual_suffix = format!("{}{}{}", DISPLAY_PRE, "0", DISPLAY_SUF);
let manual = format!("{}{}{}", manual_prefix, first, manual_suffix);

let styled_value = style_text(&first, Ansi::red().underline());

assert_eq!(styled_value, manual);
}

#[test]
fn style_text_inputs() {
let first = "first".to_string();

let st = style_text(&first, Ansi::new());
let sf = style_text(&first, empty_style_function);
let sc = style_text(&first, || {
let style = Ansi::new()
.underline()
.italic()
.fg((200, 100, 200))
.bg((255, 255, 255));

style.strike()
});
// Why the fuck cant i get this to work in another project.
let _scols = style_text(&first, Colors::Yellow);
let _scols = style_text(&first, Colors::Yellow.into_ansi());
let yellow = Colors::Yellow.into_color();
let _scol = style_text(&first, yellow.into_ansi());

let manual_prefix = format!(
"{}{}{}",
DISPLAY_PRE, "3;4;9;38;2;200;100;200;48;2;255;255;255", DISPLAY_SUF
);
let manual_suffix = format!("{}{}{}", DISPLAY_PRE, "0", DISPLAY_SUF);
let third = format!("{}{}{}", manual_prefix, first, manual_suffix);

assert_eq!(&st, &first);
assert_eq!(&sf, &first);
assert_eq!(&sc, &third);
}

#[test]
fn color_inputs() {
let _red = Ansi::from_fg(Colors::Red);
let _green = Ansi::from_fg((0, 255, 0));
let _blue = Ansi::from_fg(Color::from_hex("#0000ff").unwrap());
}
}
86 changes: 86 additions & 0 deletions src/styled/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,89 @@ where
style_text(self.to_string(), style)
}
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;

const DISPLAY_PRE: &str = "\u{1b}[";
const DISPLAY_SUF: &str = "m";

fn empty_style_function() -> Ansi {
Ansi::new()
}

#[test]
fn storing_styles() {
let style1 = Ansi::new().fg((100, 200, 100)).underline();
let style2 = Ansi::new().bg((0, 0, 75)).italic().strike();

assert_eq!(
style1.to_string(),
format!("{}4;38;2;100;200;100{}", DISPLAY_PRE, DISPLAY_SUF)
);
assert_eq!(
style2.to_string(),
format!("{}3;9;48;2;0;0;75{}", DISPLAY_PRE, DISPLAY_SUF)
);
assert_eq!(
style1.to_string(),
format!("{}4;38;2;100;200;100{}", DISPLAY_PRE, DISPLAY_SUF)
);
assert_eq!(
style2.to_string(),
format!("{}3;9;48;2;0;0;75{}", DISPLAY_PRE, DISPLAY_SUF)
);
}

#[test]
fn style_text_basic() {
let first = "first".to_string();
let unstyled_val = style_text(&first, Ansi::new());
assert_eq!(unstyled_val, first);
let unstyled_fn = style_text(&first, empty_style_function);
assert_eq!(unstyled_fn, first);

let manual_prefix = format!("{}{}{}", DISPLAY_PRE, "4;38;2;255;0;0", DISPLAY_SUF);
let manual_suffix = format!("{}{}{}", DISPLAY_PRE, "0", DISPLAY_SUF);
let manual = format!("{}{}{}", manual_prefix, first, manual_suffix);

let styled_value = style_text(&first, Ansi::red().underline());

assert_eq!(styled_value, manual);
}

#[test]
fn style_text_inputs() {
let first = "first".to_string();

let st = style_text(&first, Ansi::new());
let sf = style_text(&first, empty_style_function);
let sc = style_text(&first, || {
let style = Ansi::new()
.underline()
.italic()
.fg((200, 100, 200))
.bg((255, 255, 255));

style.strike()
});
// Why the fuck cant i get this to work in another project.
let _scols = style_text(&first, crate::Colors::Yellow);
let _scols = style_text(&first, crate::Colors::Yellow.into_ansi());
let yellow = crate::Colors::Yellow.into_color();
let _scol = style_text(&first, yellow.into_ansi());

let manual_prefix = format!(
"{}{}{}",
DISPLAY_PRE, "3;4;9;38;2;200;100;200;48;2;255;255;255", DISPLAY_SUF
);
let manual_suffix = format!("{}{}{}", DISPLAY_PRE, "0", DISPLAY_SUF);
let third = format!("{}{}{}", manual_prefix, first, manual_suffix);

assert_eq!(&st, &first);
assert_eq!(&sf, &first);
assert_eq!(&sc, &third);
}
}

0 comments on commit e406914

Please sign in to comment.