aconfig: improve code diffs in tests
Implement a helper function to make it easier for unit tests to diff (and find the first difference) generated code and expected code. Bug: 283910447 Test: atest aconfig.test Change-Id: I460e8fbf05e8f33e8a62ecef67b2d9d77051e876
This commit is contained in:
@@ -35,4 +35,7 @@ rust_binary_host {
|
|||||||
rust_test_host {
|
rust_test_host {
|
||||||
name: "aconfig.test",
|
name: "aconfig.test",
|
||||||
defaults: ["aconfig.defaults"],
|
defaults: ["aconfig.defaults"],
|
||||||
|
rustlibs: [
|
||||||
|
"libitertools",
|
||||||
|
],
|
||||||
}
|
}
|
||||||
|
@@ -18,3 +18,6 @@ tinytemplate = "1.2.1"
|
|||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
protobuf-codegen = "3.2.0"
|
protobuf-codegen = "3.2.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
itertools = "0.10.5"
|
||||||
|
@@ -227,8 +227,11 @@ mod tests {
|
|||||||
let file = generate_cpp_code(&cache).unwrap();
|
let file = generate_cpp_code(&cache).unwrap();
|
||||||
assert_eq!("aconfig/com_example.h", file.path.to_str().unwrap());
|
assert_eq!("aconfig/com_example.h", file.path.to_str().unwrap());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expect_content.replace(' ', ""),
|
None,
|
||||||
String::from_utf8(file.contents).unwrap().replace(' ', "")
|
crate::test::first_significant_code_diff(
|
||||||
|
expect_content,
|
||||||
|
&String::from_utf8(file.contents).unwrap()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -135,8 +135,11 @@ mod tests {
|
|||||||
let file = generate_java_code(&cache).unwrap();
|
let file = generate_java_code(&cache).unwrap();
|
||||||
assert_eq!("com/example/Flags.java", file.path.to_str().unwrap());
|
assert_eq!("com/example/Flags.java", file.path.to_str().unwrap());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
expect_content.replace(' ', ""),
|
None,
|
||||||
String::from_utf8(file.contents).unwrap().replace(' ', "")
|
crate::test::first_significant_code_diff(
|
||||||
|
expect_content,
|
||||||
|
&String::from_utf8(file.contents).unwrap()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -126,6 +126,12 @@ pub fn r#enabled_rw() -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
assert_eq!(expected.trim(), String::from_utf8(generated.contents).unwrap().trim());
|
assert_eq!(
|
||||||
|
None,
|
||||||
|
crate::test::first_significant_code_diff(
|
||||||
|
expected,
|
||||||
|
&String::from_utf8(generated.contents).unwrap()
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@
|
|||||||
pub mod test_utils {
|
pub mod test_utils {
|
||||||
use crate::cache::Cache;
|
use crate::cache::Cache;
|
||||||
use crate::commands::{Input, Source};
|
use crate::commands::{Input, Source};
|
||||||
|
use itertools;
|
||||||
|
|
||||||
pub fn create_cache() -> Cache {
|
pub fn create_cache() -> Cache {
|
||||||
crate::commands::create_cache(
|
crate::commands::create_cache(
|
||||||
@@ -39,6 +40,54 @@ pub mod test_utils {
|
|||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> {
|
||||||
|
let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
|
||||||
|
let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty());
|
||||||
|
match itertools::diff_with(a, b, |left, right| left == right) {
|
||||||
|
Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => {
|
||||||
|
Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap()))
|
||||||
|
}
|
||||||
|
Some(itertools::Diff::Shorter(_, mut left)) => {
|
||||||
|
Some(format!("LHS trailing data: '{}'", left.next().unwrap()))
|
||||||
|
}
|
||||||
|
Some(itertools::Diff::Longer(_, mut right)) => {
|
||||||
|
Some(format!("RHS trailing data: '{}'", right.next().unwrap()))
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_first_significant_code_diff() {
|
||||||
|
assert!(first_significant_code_diff("", "").is_none());
|
||||||
|
assert!(first_significant_code_diff(" a", "\n\na\n").is_none());
|
||||||
|
let a = r#"
|
||||||
|
public class A {
|
||||||
|
private static final String FOO = "FOO";
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("FOO=" + FOO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
let b = r#"
|
||||||
|
public class A {
|
||||||
|
private static final String FOO = "BAR";
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("foo=" + FOO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert_eq!(Some(r#"'private static final String FOO = "FOO";' vs 'private static final String FOO = "BAR";'"#.to_string()), first_significant_code_diff(a, b));
|
||||||
|
assert_eq!(
|
||||||
|
Some("LHS trailing data: 'b'".to_string()),
|
||||||
|
first_significant_code_diff("a\nb", "a")
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
Some("RHS trailing data: 'b'".to_string()),
|
||||||
|
first_significant_code_diff("a", "a\nb")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Reference in New Issue
Block a user