-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
feature: debug_string_snapshot
to test pretty printers
#707
Comments
debug_string_snapshot
to test pretty printersdebug_string_snapshot
to test pretty printers
Could you give an example? Would |
For example, you could have a typescript pretty printer that takes this as input function greet(name:string){console.log("Hello, "+name);}const name="Alice";greet(name); and outputs this: function greet(name: string): void {
console.log("Hello, " + name);
}
const name = "Alice";
greet(name); Then it would be nice to see that the formatting stays the same as you are refactoring. I think However, I have used similar libraries to |
Can you give a rust code example? |
I got it working using debug. The idea is that I want to quickly iterate on the quality of my error messages and it is a pain to run my CLI too all the time (and I want to catch regressions) so I dump a bunch of code for my custom DSL into a directory and then have it check each file individually for errors. input file:
output file:
testing code: use ariadne::FileCache;
use graphite_cli::diagnostics_report;
use graphite_core::pipeline;
use std::path::PathBuf;
struct PrintString(String);
impl std::fmt::Debug for PrintString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
/// Regression tests for diagnostics report. Run `cargo test` to check that all the tests pass.
#[test]
fn test_parser_regression() {
let test_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("diagnostics_report_input");
let mut failures = Vec::new();
for entry in std::fs::read_dir(test_dir).unwrap() {
let entry = entry.unwrap();
let file_name = entry.file_name().to_str().unwrap().to_string();
let result = std::panic::catch_unwind(|| {
let file_path = entry.path();
let diagnostics = pipeline::check(file_path);
// Build and render the diagnostic report
let mut buffer = Vec::new();
for diagnostic in diagnostics {
let report = diagnostics_report::build(&diagnostic);
report.write(FileCache::default(), &mut buffer).unwrap();
}
let report_string = String::from_utf8(strip_ansi_escapes::strip(&buffer)).unwrap();
// Assert the snapshot
insta::assert_debug_snapshot!(file_name.clone(), PrintString(report_string));
});
if result.is_err() {
failures.push(file_name.to_string());
}
}
if !failures.is_empty() {
panic!("Diagnostic report tests failed for files: {:?}", failures);
}
} |
I'm writing a DSL and would like to use insta to snapshot test my pretty printer and my diagnostics messages. Is it already possible somehow and I've just missed it or would
debug_string_snapshot
make a nice addition to the library?The text was updated successfully, but these errors were encountered: