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 { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 19 | use crate::commands::Input; |
| 20 | use crate::protos::ProtoParsedFlags; |
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 | |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 23 | pub const TEST_PACKAGE: &str = "com.android.aconfig.test"; |
| 24 | |
| 25 | pub fn parse_test_flags() -> ProtoParsedFlags { |
| 26 | let bytes = crate::commands::parse_flags( |
Mårten Kongstad | fbd71e2 | 2023-05-31 13:29:35 +0200 | [diff] [blame] | 27 | "com.android.aconfig.test", |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 28 | vec![Input { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 29 | source: "tests/test.aconfig".to_string(), |
Mårten Kongstad | 9c59c31 | 2023-05-30 11:15:02 +0200 | [diff] [blame] | 30 | reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()), |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 31 | }], |
| 32 | vec![ |
| 33 | Input { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 34 | source: "tests/first.values".to_string(), |
Mårten Kongstad | 9c59c31 | 2023-05-30 11:15:02 +0200 | [diff] [blame] | 35 | reader: Box::new(include_bytes!("../tests/first.values").as_slice()), |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 36 | }, |
| 37 | Input { |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 38 | source: "tests/second.values".to_string(), |
Mårten Kongstad | 9c59c31 | 2023-05-30 11:15:02 +0200 | [diff] [blame] | 39 | reader: Box::new(include_bytes!("../tests/second.values").as_slice()), |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 40 | }, |
| 41 | ], |
| 42 | ) |
Mårten Kongstad | 403658f | 2023-06-14 09:51:56 +0200 | [diff] [blame^] | 43 | .unwrap(); |
| 44 | crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap() |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 45 | } |
Mårten Kongstad | b025507 | 2023-06-08 10:15:43 +0200 | [diff] [blame] | 46 | |
| 47 | pub fn first_significant_code_diff(a: &str, b: &str) -> Option<String> { |
| 48 | let a = a.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty()); |
| 49 | let b = b.lines().map(|line| line.trim_start()).filter(|line| !line.is_empty()); |
| 50 | match itertools::diff_with(a, b, |left, right| left == right) { |
| 51 | Some(itertools::Diff::FirstMismatch(_, mut left, mut right)) => { |
| 52 | Some(format!("'{}' vs '{}'", left.next().unwrap(), right.next().unwrap())) |
| 53 | } |
| 54 | Some(itertools::Diff::Shorter(_, mut left)) => { |
| 55 | Some(format!("LHS trailing data: '{}'", left.next().unwrap())) |
| 56 | } |
| 57 | Some(itertools::Diff::Longer(_, mut right)) => { |
| 58 | Some(format!("RHS trailing data: '{}'", right.next().unwrap())) |
| 59 | } |
| 60 | None => None, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn test_first_significant_code_diff() { |
| 66 | assert!(first_significant_code_diff("", "").is_none()); |
| 67 | assert!(first_significant_code_diff(" a", "\n\na\n").is_none()); |
| 68 | let a = r#" |
| 69 | public class A { |
| 70 | private static final String FOO = "FOO"; |
| 71 | public static void main(String[] args) { |
| 72 | System.out.println("FOO=" + FOO); |
| 73 | } |
| 74 | } |
| 75 | "#; |
| 76 | let b = r#" |
| 77 | public class A { |
| 78 | private static final String FOO = "BAR"; |
| 79 | public static void main(String[] args) { |
| 80 | System.out.println("foo=" + FOO); |
| 81 | } |
| 82 | } |
| 83 | "#; |
| 84 | 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)); |
| 85 | assert_eq!( |
| 86 | Some("LHS trailing data: 'b'".to_string()), |
| 87 | first_significant_code_diff("a\nb", "a") |
| 88 | ); |
| 89 | assert_eq!( |
| 90 | Some("RHS trailing data: 'b'".to_string()), |
| 91 | first_significant_code_diff("a", "a\nb") |
| 92 | ); |
| 93 | } |
Mårten Kongstad | 83a8760 | 2023-06-02 11:20:15 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | #[cfg(test)] |
| 97 | pub use test_utils::*; |