Skip to content

Commit

Permalink
test: add some test cases on bin
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-duarte-ifood committed Oct 20, 2023
1 parent da30179 commit a84546c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 36 deletions.
147 changes: 115 additions & 32 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<CSTNode, &'static str> {
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 {
Expand All @@ -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())
}
}
5 changes: 1 addition & 4 deletions matching/src/ordered_tree_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
1 change: 1 addition & 0 deletions model/src/language.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Clone, Copy)]
pub enum Language {
Java,
}

0 comments on commit a84546c

Please sign in to comment.