Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #[cfg(test)] |
| 18 | pub mod test_utils { |
| 19 | use crate::cache::Cache; |
| 20 | use crate::commands::{Input, Source}; |
Mårten Kongstad | b025507 | 2023-06-08 10:15:43 +0200 | [diff] [blame^] | 21 | use itertools; |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 22 | |
| 23 | pub fn create_cache() -> Cache { |
| 24 | crate::commands::create_cache( |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 25 | "com.android.aconfig.test", |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 26 | vec![Input { |
| 27 | source: Source::File("testdata/test.aconfig".to_string()), |
| 28 | reader: Box::new(include_bytes!("../testdata/test.aconfig").as_slice()), |
| 29 | }], |
| 30 | vec![ |
| 31 | Input { |
| 32 | source: Source::File("testdata/first.values".to_string()), |
| 33 | reader: Box::new(include_bytes!("../testdata/first.values").as_slice()), |
| 34 | }, |
| 35 | Input { |
| 36 | source: Source::File("testdata/test.aconfig".to_string()), |
| 37 | reader: Box::new(include_bytes!("../testdata/second.values").as_slice()), |
| 38 | }, |
| 39 | ], |
| 40 | ) |
| 41 | .unwrap() |
| 42 | } |
Mårten Kongstad | b025507 | 2023-06-08 10:15:43 +0200 | [diff] [blame^] | 43 | |
| 44 | pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> { |
| 45 | let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty()); |
| 46 | let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty()); |
| 47 | match itertools::diff_with(a, b, |left, right| left == right) { |
| 48 | Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => { |
| 49 | Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap())) |
| 50 | } |
| 51 | Some(itertools::Diff::Shorter(_, mut left)) => { |
| 52 | Some(format!("LHS trailing data: '{}'", left.next().unwrap())) |
| 53 | } |
| 54 | Some(itertools::Diff::Longer(_, mut right)) => { |
| 55 | Some(format!("RHS trailing data: '{}'", right.next().unwrap())) |
| 56 | } |
| 57 | None => None, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn test_first_significant_code_diff() { |
| 63 | assert!(first_significant_code_diff("", "").is_none()); |
| 64 | assert!(first_significant_code_diff(" a", "\n\na\n").is_none()); |
| 65 | let a = r#" |
| 66 | public class A { |
| 67 | private static final String FOO = "FOO"; |
| 68 | public static void main(String[] args) { |
| 69 | System.out.println("FOO=" + FOO); |
| 70 | } |
| 71 | } |
| 72 | "#; |
| 73 | let b = r#" |
| 74 | public class A { |
| 75 | private static final String FOO = "BAR"; |
| 76 | public static void main(String[] args) { |
| 77 | System.out.println("foo=" + FOO); |
| 78 | } |
| 79 | } |
| 80 | "#; |
| 81 | 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)); |
| 82 | assert_eq!( |
| 83 | Some("LHS trailing data: 'b'".to_string()), |
| 84 | first_significant_code_diff("a\nb", "a") |
| 85 | ); |
| 86 | assert_eq!( |
| 87 | Some("RHS trailing data: 'b'".to_string()), |
| 88 | first_significant_code_diff("a", "a\nb") |
| 89 | ); |
| 90 | } |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | pub use test_utils::*; |