blob: 9d29083dacf2018f458057cd3f9d2b309cfbf910 [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 {
19 use crate::cache::Cache;
20 use crate::commands::{Input, Source};
Mårten Kongstadb0255072023-06-08 10:15:43 +020021 use itertools;
Mårten Kongstad83a87602023-06-02 11:20:15 +020022
23 pub fn create_cache() -> Cache {
24 crate::commands::create_cache(
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020025 "com.android.aconfig.test",
Mårten Kongstad83a87602023-06-02 11:20:15 +020026 vec![Input {
Mårten Kongstad9c59c312023-05-30 11:15:02 +020027 source: Source::File("tests/test.aconfig".to_string()),
28 reader: Box::new(include_bytes!("../tests/test.aconfig").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +020029 }],
30 vec![
31 Input {
Mårten Kongstad9c59c312023-05-30 11:15:02 +020032 source: Source::File("tests/first.values".to_string()),
33 reader: Box::new(include_bytes!("../tests/first.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +020034 },
35 Input {
Mårten Kongstad0cd80922023-06-15 11:43:33 +020036 source: Source::File("tests/second.values".to_string()),
Mårten Kongstad9c59c312023-05-30 11:15:02 +020037 reader: Box::new(include_bytes!("../tests/second.values").as_slice()),
Mårten Kongstad83a87602023-06-02 11:20:15 +020038 },
39 ],
40 )
41 .unwrap()
42 }
Mårten Kongstadb0255072023-06-08 10:15:43 +020043
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 Kongstad83a87602023-06-02 11:20:15 +020091}
92
93#[cfg(test)]
94pub use test_utils::*;