diff --git a/matching_handlers/src/java/method_declaration.rs b/matching_handlers/src/java/method_declaration.rs index 9ad8cde..3fdb317 100644 --- a/matching_handlers/src/java/method_declaration.rs +++ b/matching_handlers/src/java/method_declaration.rs @@ -37,3 +37,140 @@ pub fn compute_matching_score_for_method_declaration<'a>( (_, _) => 0, } } +#[cfg(test)] +mod tests { + use model::{ + cst_node::{NonTerminal, Terminal}, + CSTNode, + }; + + use crate::java::method_declaration::compute_matching_score_for_method_declaration; + + #[test] + fn it_returns_one_if_methods_have_the_same_identifier() { + let left = + make_method_declaration_node("an_identifier", make_method_parameter("String", "name")); + let right = + make_method_declaration_node("an_identifier", make_method_parameter("String", "name")); + let matching_score = compute_matching_score_for_method_declaration(&left, &right); + assert_eq!(1, matching_score); + } + + #[test] + fn it_returns_zero_if_methods_have_different_identifiers() { + let left = make_method_declaration_node( + "an_identifier_a", + make_method_parameter("String", "name"), + ); + let right = make_method_declaration_node( + "an_identifier_b", + make_method_parameter("String", "name"), + ); + let matching_score = compute_matching_score_for_method_declaration(&left, &right); + assert_eq!(0, matching_score); + } + + #[test] + fn it_returns_one_if_methods_have_equal_identifiers_and_equal_parameters_list() { + let left = + make_method_declaration_node("an_identifier", make_method_parameter("String", "name")); + let right = + make_method_declaration_node("an_identifier", make_method_parameter("String", "name")); + let matching_score = compute_matching_score_for_method_declaration(&left, &right); + assert_eq!(1, matching_score); + } + + #[test] + fn it_returns_zero_if_methods_have_equal_identifiers_but_different_parameters_list() { + let parameter_left = make_method_parameter("String", "name"); + let parameter_right = make_method_parameter("int", "another_name"); + + let left = make_method_declaration_node("an_identifier", parameter_left); + let right = make_method_declaration_node("an_identifier", parameter_right); + let matching_score = compute_matching_score_for_method_declaration(&left, &right); + assert_eq!(0, matching_score); + } + + fn make_method_declaration_node<'a>( + identifier: &'a str, + parameter: CSTNode<'a>, + ) -> CSTNode<'a> { + CSTNode::NonTerminal(NonTerminal { + kind: "method_declaration", + children: vec![ + CSTNode::NonTerminal(NonTerminal { + kind: "modifiers", + children: vec![CSTNode::Terminal(Terminal { + kind: "public", + value: "public", + ..Default::default() + })], + ..Default::default() + }), + CSTNode::Terminal(Terminal { + kind: "void_type", + value: "void", + ..Default::default() + }), + CSTNode::Terminal(Terminal { + kind: "identifier", + value: identifier, + ..Default::default() + }), + CSTNode::NonTerminal(NonTerminal { + kind: "formal_parameters", + children: vec![ + CSTNode::Terminal(Terminal { + kind: "(", + value: "(", + ..Default::default() + }), + parameter, + CSTNode::Terminal(Terminal { + kind: ")", + value: ")", + ..Default::default() + }), + ], + ..Default::default() + }), + CSTNode::NonTerminal(NonTerminal { + kind: "block", + children: vec![ + CSTNode::Terminal(Terminal { + kind: "{", + value: "{", + ..Default::default() + }), + CSTNode::Terminal(Terminal { + kind: "}", + value: "}", + ..Default::default() + }), + ], + ..Default::default() + }), + ], + ..Default::default() + }) + } + + fn make_method_parameter<'a>(a_type: &'a str, identifier: &'a str) -> CSTNode<'a> { + CSTNode::NonTerminal(NonTerminal { + kind: "formal_parameter", + children: vec![ + CSTNode::Terminal(Terminal { + kind: "type_identifier", + value: a_type, + ..Default::default() + }), + CSTNode::Terminal(Terminal { + kind: "identifier", + value: identifier, + ..Default::default() + }), + ], + ..Default::default() + }) + } +}