Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace println! calls with snapshot tests #158

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"extensions": ["rust-lang.rust-analyzer"]
}
},
"postCreateCommand": "cargo install cargo-insta",
"remoteUser": "vscode"
}
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.snap linguist-language=Text
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Run tests
on: pull_request
env: { CLICOLOR_FORCE: 1 }
env:
CLICOLOR_FORCE: 1
COLORTERM: "truecolor"

jobs:
cargo-fmt:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ features = [

[dev_dependencies]
ansi_term = "0.12"
insta = "1"
rspec = "=1.0.0-beta.3"
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 @@ -736,6 +736,7 @@ impl From<ColoredString> for Box<dyn Error> {
#[cfg(test)]
mod tests {
use super::*;
use std::{error::Error, fmt::Write};

#[test]
fn formatting() {
Expand All @@ -749,47 +750,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