blob: abe90150276705c2405c3db8fe10f1ac6f918e77 [file] [log] [blame]
Mårten Kongstad83a87602023-06-02 11:20:15 +02001/*
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)]
18pub mod test_utils {
Mårten Kongstad403658f2023-06-14 09:51:56 +020019 use crate::commands::Input;
20 use crate::protos::ProtoParsedFlags;
Mårten Kongstadb0255072023-06-08 10:15:43 +020021 use itertools;
Mårten Kongstad83a87602023-06-02 11:20:15 +020022
Mårten Kongstad403658f2023-06-14 09:51:56 +020023 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 Kongstadfbd71e22023-05-31 13:29:35 +020027 "com.android.aconfig.test",
Mårten Kongstad83a87602023-06-02 11:20:15 +020028 vec![Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +020029 source: "tests/test.aconfig".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +020030 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +020031 }],
32 vec![
33 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +020034 source: "tests/first.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +020035 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +020036 },
37 Input {
Mårten Kongstad403658f2023-06-14 09:51:56 +020038 source: "tests/second.values".to_string(),
Mårten Kongstad9c59c312023-05-30 11:15:02 +020039 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +020040 },
41 ],
42 )
Mårten Kongstad403658f2023-06-14 09:51:56 +020043 .unwrap();
44 crate::protos::parsed_flags::try_from_binary_proto(&bytes).unwrap()
Mårten Kongstad83a87602023-06-02 11:20:15 +020045 }
Mårten Kongstadb0255072023-06-08 10:15:43 +020046
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 Kongstad83a87602023-06-02 11:20:15 +020094}
95
96#[cfg(test)]
97pub use test_utils::*;