Skip to content

Commit

Permalink
Replace println! tests with snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserblack committed Dec 12, 2023
1 parent 3a2cadf commit 913e70c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.snap linguist-language=Text
3 changes: 2 additions & 1 deletion src/customcolors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ impl From<(u8, u8, u8)> for CustomColor {
#[cfg(test)]
mod tests {
use crate::*;
#[cfg_attr(feature = "no-color", ignore)]
#[test]
fn main() {
let my_color = CustomColor::new(0, 120, 120);
println!("{}", "Greetings from Ukraine".custom_color(my_color));
insta::assert_display_snapshot!("Greetings from Ukraine".custom_color(my_color));
}

#[test]
Expand Down
85 changes: 45 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ impl From<ColoredString> for Box<dyn Error> {
#[cfg(test)]
mod tests {
use super::*;
use std::{fmt::Write, error::Error};

#[test]
fn formatting() {
Expand All @@ -653,47 +654,51 @@ mod tests {
}

#[test]
fn it_works() {
fn it_works() -> Result<(), Box<dyn Error>> {
let mut buf = String::new();
let toto = "toto";
println!("{}", toto.red());
println!("{}", String::from(toto).red());
println!("{}", toto.blue());

println!("blue style ****");
println!("{}", toto.bold());
println!("{}", "yeah ! Red bold !".red().bold());
println!("{}", "yeah ! Yellow bold !".bold().yellow());
println!("{}", toto.bold().blue());
println!("{}", toto.blue().bold());
println!("{}", toto.blue().bold().underline());
println!("{}", toto.blue().italic());
println!("******");
println!("test clearing");
println!("{}", "red cleared".red().clear());
println!("{}", "bold cyan cleared".bold().cyan().clear());
println!("******");
println!("Bg tests");
println!("{}", toto.green().on_blue());
println!("{}", toto.on_magenta().yellow());
println!("{}", toto.purple().on_yellow());
println!("{}", toto.magenta().on_white());
println!("{}", toto.cyan().on_green());
println!("{}", toto.black().on_white());
println!("******");
println!("{}", toto.green());
println!("{}", toto.yellow());
println!("{}", toto.purple());
println!("{}", toto.magenta());
println!("{}", toto.cyan());
println!("{}", toto.white());
println!("{}", toto.white().red().blue().green());
println!("{}", toto.truecolor(255, 0, 0));
println!("{}", toto.truecolor(255, 255, 0));
println!("{}", toto.on_truecolor(0, 80, 80));
println!("{}", toto.custom_color((255, 255, 0)));
println!("{}", toto.on_custom_color((0, 80, 80)));
// uncomment to see term output
// assert!(false)
writeln!(&mut buf, "{}", toto.red())?;
writeln!(&mut buf, "{}", String::from(toto).red())?;
writeln!(&mut buf, "{}", toto.blue())?;

writeln!(&mut buf, "blue style ****")?;
writeln!(&mut buf, "{}", toto.bold())?;
writeln!(&mut buf, "{}", "yeah ! Red bold !".red().bold())?;
writeln!(&mut buf, "{}", "yeah ! Yellow bold !".bold().yellow())?;
writeln!(&mut buf, "{}", toto.bold().blue())?;
writeln!(&mut buf, "{}", toto.blue().bold())?;
writeln!(&mut buf, "{}", toto.blue().bold().underline())?;
writeln!(&mut buf, "{}", toto.blue().italic())?;
writeln!(&mut buf, "******")?;
writeln!(&mut buf, "test clearing")?;
writeln!(&mut buf, "{}", "red cleared".red().clear())?;
writeln!(&mut buf, "{}", "bold cyan cleared".bold().cyan().clear())?;
writeln!(&mut buf, "******")?;
writeln!(&mut buf, "Bg tests")?;
writeln!(&mut buf, "{}", toto.green().on_blue())?;
writeln!(&mut buf, "{}", toto.on_magenta().yellow())?;
writeln!(&mut buf, "{}", toto.purple().on_yellow())?;
writeln!(&mut buf, "{}", toto.magenta().on_white())?;
writeln!(&mut buf, "{}", toto.cyan().on_green())?;
writeln!(&mut buf, "{}", toto.black().on_white())?;
writeln!(&mut buf, "******")?;
writeln!(&mut buf, "{}", toto.green())?;
writeln!(&mut buf, "{}", toto.yellow())?;
writeln!(&mut buf, "{}", toto.purple())?;
writeln!(&mut buf, "{}", toto.magenta())?;
writeln!(&mut buf, "{}", toto.cyan())?;
writeln!(&mut buf, "{}", toto.white())?;
writeln!(&mut buf, "{}", toto.white().red().blue().green())?;
writeln!(&mut buf, "{}", toto.truecolor(255, 0, 0))?;
writeln!(&mut buf, "{}", toto.truecolor(255, 255, 0))?;
writeln!(&mut buf, "{}", toto.on_truecolor(0, 80, 80))?;
writeln!(&mut buf, "{}", toto.custom_color((255, 255, 0)))?;
writeln!(&mut buf, "{}", toto.on_custom_color((0, 80, 80)))?;
#[cfg(feature = "no-color")]
insta::assert_snapshot!("it_works_no_color", buf);
#[cfg(not(feature = "no-color"))]
insta::assert_snapshot!("it_works", buf);
Ok(())
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions src/snapshots/colored__customcolors__tests__main.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/customcolors.rs
expression: "\"Greetings from Ukraine\".custom_color(my_color)"
---
Greetings from Ukraine
41 changes: 41 additions & 0 deletions src/snapshots/colored__tests__it_works.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: src/lib.rs
expression: buf
---
toto
toto
toto
blue style ****
toto
yeah ! Red bold !
yeah ! Yellow bold !
toto
toto
toto
toto
******
test clearing
red cleared
bold cyan cleared
******
Bg tests
toto
toto
toto
toto
toto
toto
******
toto
toto
toto
toto
toto
toto
toto
toto
toto
toto
toto
toto

41 changes: 41 additions & 0 deletions src/snapshots/colored__tests__it_works_no_color.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: src/lib.rs
expression: buf
---
toto
toto
toto
blue style ****
toto
yeah ! Red bold !
yeah ! Yellow bold !
toto
toto
toto
toto
******
test clearing
red cleared
bold cyan cleared
******
Bg tests
toto
toto
toto
toto
toto
toto
******
toto
toto
toto
toto
toto
toto
toto
toto
toto
toto
toto
toto

0 comments on commit 913e70c

Please sign in to comment.