Loading tools/pdl/src/test_utils.rs +51 −6 Original line number Diff line number Diff line Loading @@ -64,7 +64,7 @@ pub fn rustfmt(input: &str) -> String { /// /// Panics if `diff` cannot be found on `$PATH` or if it returns an /// error. pub fn diff(left: &str, right: &str) -> String { pub fn diff(left_label: &str, left: &str, right_label: &str, right: &str) -> String { let mut temp_left = NamedTempFile::new().unwrap(); temp_left.write_all(left.as_bytes()).unwrap(); let mut temp_right = NamedTempFile::new().unwrap(); Loading @@ -75,9 +75,9 @@ pub fn diff(left: &str, right: &str) -> String { .arg("--unified") .arg("--color=always") .arg("--label") .arg("left") .arg(left_label) .arg("--label") .arg("right") .arg(right_label) .arg(temp_left.path()) .arg(temp_right.path()) .output() Loading @@ -94,8 +94,20 @@ pub fn diff(left: &str, right: &str) -> String { /// Compare two strings and output a diff if they are not equal. #[track_caller] pub fn assert_eq_with_diff(left: &str, right: &str) { assert!(left == right, "texts did not match, diff:\n{}\n", diff(left, right)); pub fn assert_eq_with_diff(left_label: &str, left: &str, right_label: &str, right: &str) { assert!( left == right, "texts did not match, diff:\n{}\n", diff(left_label, left, right_label, right) ); } /// Check that `haystack` contains `needle`. /// /// Panic with a nice message if not. #[track_caller] pub fn assert_contains(haystack: &str, needle: &str) { assert!(haystack.contains(needle), "Could not find {:?} in {:?}", needle, haystack); } /// Compare a string with a snapshot file. Loading Loading @@ -126,7 +138,12 @@ pub fn assert_snapshot_eq<P: AsRef<Path>>(snapshot_path: P, actual_content: &str // Normal comparison if UPDATE_SNAPSHOTS is unset. if std::env::var("UPDATE_SNAPSHOTS").is_err() { return assert_eq_with_diff(&snapshot_content, actual_content); return assert_eq_with_diff( snapshot.to_str().unwrap(), &snapshot_content, "actual", actual_content, ); } // Bail out if we are not using Cargo. Loading @@ -146,3 +163,31 @@ pub fn assert_snapshot_eq<P: AsRef<Path>>(snapshot_path: P, actual_content: &str }); } } #[cfg(test)] mod tests { use super::*; #[test] fn test_diff_labels_with_special_chars() { // Check that special characters in labels are passed // correctly to diff. let patch = diff("left 'file'", "foo\nbar\n", "right ~file!", "foo\nnew line\nbar\n"); assert_contains(&patch, "left 'file'"); assert_contains(&patch, "right ~file!"); } #[test] #[should_panic] fn test_assert_eq_with_diff_on_diff() { // We use identical labels to check that we haven't // accidentally mixed up the labels with the file content. assert_eq_with_diff("", "foo\nbar\n", "", "foo\nnew line\nbar\n"); } #[test] fn test_assert_eq_with_diff_on_eq() { // No panic when there is no diff. assert_eq_with_diff("left", "foo\nbar\n", "right", "foo\nbar\n"); } } tools/pdl/test/pdl_tests.rs +6 −1 Original line number Diff line number Diff line Loading @@ -70,7 +70,12 @@ fn bluetooth_packetgen(code: &str) -> String { fn assert_equal_compilation(pdl_code: &str) { let old_rust = rustfmt(&bluetooth_packetgen(pdl_code)); let new_rust = rustfmt(&pdl(pdl_code)); assert_eq_with_diff(&strip_blank_lines(&old_rust), &strip_blank_lines(&new_rust)); assert_eq_with_diff( "bluetooth_packetgen output", &strip_blank_lines(&old_rust), "pdl output", &strip_blank_lines(&new_rust), ); } #[test] Loading Loading
tools/pdl/src/test_utils.rs +51 −6 Original line number Diff line number Diff line Loading @@ -64,7 +64,7 @@ pub fn rustfmt(input: &str) -> String { /// /// Panics if `diff` cannot be found on `$PATH` or if it returns an /// error. pub fn diff(left: &str, right: &str) -> String { pub fn diff(left_label: &str, left: &str, right_label: &str, right: &str) -> String { let mut temp_left = NamedTempFile::new().unwrap(); temp_left.write_all(left.as_bytes()).unwrap(); let mut temp_right = NamedTempFile::new().unwrap(); Loading @@ -75,9 +75,9 @@ pub fn diff(left: &str, right: &str) -> String { .arg("--unified") .arg("--color=always") .arg("--label") .arg("left") .arg(left_label) .arg("--label") .arg("right") .arg(right_label) .arg(temp_left.path()) .arg(temp_right.path()) .output() Loading @@ -94,8 +94,20 @@ pub fn diff(left: &str, right: &str) -> String { /// Compare two strings and output a diff if they are not equal. #[track_caller] pub fn assert_eq_with_diff(left: &str, right: &str) { assert!(left == right, "texts did not match, diff:\n{}\n", diff(left, right)); pub fn assert_eq_with_diff(left_label: &str, left: &str, right_label: &str, right: &str) { assert!( left == right, "texts did not match, diff:\n{}\n", diff(left_label, left, right_label, right) ); } /// Check that `haystack` contains `needle`. /// /// Panic with a nice message if not. #[track_caller] pub fn assert_contains(haystack: &str, needle: &str) { assert!(haystack.contains(needle), "Could not find {:?} in {:?}", needle, haystack); } /// Compare a string with a snapshot file. Loading Loading @@ -126,7 +138,12 @@ pub fn assert_snapshot_eq<P: AsRef<Path>>(snapshot_path: P, actual_content: &str // Normal comparison if UPDATE_SNAPSHOTS is unset. if std::env::var("UPDATE_SNAPSHOTS").is_err() { return assert_eq_with_diff(&snapshot_content, actual_content); return assert_eq_with_diff( snapshot.to_str().unwrap(), &snapshot_content, "actual", actual_content, ); } // Bail out if we are not using Cargo. Loading @@ -146,3 +163,31 @@ pub fn assert_snapshot_eq<P: AsRef<Path>>(snapshot_path: P, actual_content: &str }); } } #[cfg(test)] mod tests { use super::*; #[test] fn test_diff_labels_with_special_chars() { // Check that special characters in labels are passed // correctly to diff. let patch = diff("left 'file'", "foo\nbar\n", "right ~file!", "foo\nnew line\nbar\n"); assert_contains(&patch, "left 'file'"); assert_contains(&patch, "right ~file!"); } #[test] #[should_panic] fn test_assert_eq_with_diff_on_diff() { // We use identical labels to check that we haven't // accidentally mixed up the labels with the file content. assert_eq_with_diff("", "foo\nbar\n", "", "foo\nnew line\nbar\n"); } #[test] fn test_assert_eq_with_diff_on_eq() { // No panic when there is no diff. assert_eq_with_diff("left", "foo\nbar\n", "right", "foo\nbar\n"); } }
tools/pdl/test/pdl_tests.rs +6 −1 Original line number Diff line number Diff line Loading @@ -70,7 +70,12 @@ fn bluetooth_packetgen(code: &str) -> String { fn assert_equal_compilation(pdl_code: &str) { let old_rust = rustfmt(&bluetooth_packetgen(pdl_code)); let new_rust = rustfmt(&pdl(pdl_code)); assert_eq_with_diff(&strip_blank_lines(&old_rust), &strip_blank_lines(&new_rust)); assert_eq_with_diff( "bluetooth_packetgen output", &strip_blank_lines(&old_rust), "pdl output", &strip_blank_lines(&new_rust), ); } #[test] Loading