From a84546cc491d20b153348a75ea42f358f4fdfd07 Mon Sep 17 00:00:00 2001 From: Joao Duarte Date: Fri, 20 Oct 2023 14:07:31 -0300 Subject: [PATCH] test: add some test cases on bin --- bin/src/main.rs | 147 ++++++++++++++++++++------ matching/src/ordered_tree_matching.rs | 5 +- model/src/language.rs | 1 + 3 files changed, 117 insertions(+), 36 deletions(-) diff --git a/bin/src/main.rs b/bin/src/main.rs index e706af3..c29a60e 100644 --- a/bin/src/main.rs +++ b/bin/src/main.rs @@ -1,51 +1,54 @@ use matching::ordered_tree_matching; use merge::merge; -use model::CSTNode; +use model::{CSTNode, Language}; +use parsing::ParserConfiguration; + +fn run_semi_structured_merge_on_revisions( + language: Language, + base: &str, + left: &str, + right: &str, +) -> Result { + let base_tree = parsing::parse_string(base, ParserConfiguration::from_language(language))?; + let left_tree = parsing::parse_string(left, ParserConfiguration::from_language(language))?; + let right_tree = parsing::parse_string(right, ParserConfiguration::from_language(language))?; + + let matchings_left_base = ordered_tree_matching(&left_tree, &base_tree); + let matchings_right_base = ordered_tree_matching(&right_tree, &base_tree); + let matchings_left_right = ordered_tree_matching(&left_tree, &right_tree); + + Ok(merge( + &base_tree, + &left_tree, + &right_tree, + &matchings_left_base, + &matchings_right_base, + &matchings_left_right, + )) +} fn main() { - let base = parsing::parse_string( - r#" + let base = r#" public static interface HelloWorld { void sayHello(String name); } - "#, - parsing::ParserConfiguration::from_language(model::Language::Java), - ) - .unwrap(); - let left = parsing::parse_string( - r#" + "#; + let left = r#" public static interface HelloWorld { void sayHello(String name); void sayBye(String name); } - "#, - parsing::ParserConfiguration::from_language(model::Language::Java), - ) - .unwrap(); - let right = parsing::parse_string( - r#" + "#; + let right = r#" public static interface HelloWorld { void killAllHumans(); void sayHello(String name); } - "#, - parsing::ParserConfiguration::from_language(model::Language::Java), - ) - .unwrap(); - - let matchings_left_base = ordered_tree_matching(&left, &base); - let matchings_right_base = ordered_tree_matching(&right, &base); - let matchings_lef_right = ordered_tree_matching(&left, &right); - let result = merge( - &base, - &left, - &right, - &matchings_left_base, - &matchings_right_base, - &matchings_lef_right, - ); + "#; - println!("{:#?}", pretty_print(result)) + let result = run_semi_structured_merge_on_revisions(Language::Java, base, left, right); + + println!("{:#?}", pretty_print(result.unwrap())) } pub fn pretty_print(node: CSTNode) -> String { @@ -61,3 +64,83 @@ pub fn pretty_print(node: CSTNode) -> String { } } } + +#[cfg(test)] +mod tests { + use model::language::Language; + use model::CSTNode; + use parsing::ParserConfiguration; + + use crate::run_semi_structured_merge_on_revisions; + + fn parse_java_string(contents: &str) -> CSTNode { + parsing::parse_string(contents, ParserConfiguration::from_language(Language::Java)).unwrap() + } + + #[test] + fn it_merges_three_java_revisions_that_are_equal() { + let code = r#" + public static interface HelloWorld { + void sayHello(String name); + void sayBye(String name); + } + "#; + + let result = run_semi_structured_merge_on_revisions(Language::Java, code, code, code); + + assert_eq!(parse_java_string(code), result.unwrap()) + } + + #[test] + fn it_merges_three_java_revisions_that_adds_the_same_node() { + let base = r#" + public static interface HelloWorld { + void sayBye(String name); + } + "#; + + let parents = r#" + public static interface HelloWorld { + void sayHello(String name); + void sayBye(String name); + } + "#; + + let merge = r#" + public static interface HelloWorld { + void sayHello(String name); + void sayBye(String name); + } + "#; + + let result = run_semi_structured_merge_on_revisions(Language::Java, base, parents, parents); + + assert_eq!(parse_java_string(merge), result.unwrap()) + } + + #[test] + fn it_merges_three_java_revisions_that_removes_the_same_node() { + let base = r#" + public static interface HelloWorld { + void sayHello(String name); + void sayBye(String name); + } + "#; + + let parents = r#" + public static interface HelloWorld { + void sayBye(String name); + } + "#; + + let merge = r#" + public static interface HelloWorld { + void sayBye(String name); + } + "#; + + let result = run_semi_structured_merge_on_revisions(Language::Java, base, parents, parents); + + assert_eq!(parse_java_string(merge), result.unwrap()) + } +} diff --git a/matching/src/ordered_tree_matching.rs b/matching/src/ordered_tree_matching.rs index 75336f3..b79978a 100644 --- a/matching/src/ordered_tree_matching.rs +++ b/matching/src/ordered_tree_matching.rs @@ -100,10 +100,7 @@ fn ordered_tree_matching_helper( } } - let matching = MatchingEntry::new( - matrix_m[m][n] + root_matching, - left == right, - ); + let matching = MatchingEntry::new(matrix_m[m][n] + root_matching, left == right); let mut result = HashMap::new(); result.insert( UnorderedPair::new(left.to_owned(), right.to_owned()), diff --git a/model/src/language.rs b/model/src/language.rs index 5ffd8be..bf06bfa 100644 --- a/model/src/language.rs +++ b/model/src/language.rs @@ -1,3 +1,4 @@ +#[derive(Clone, Copy)] pub enum Language { Java, }