Skip to content

Commit

Permalink
test: Cover method declaration handler with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jan 8, 2024
1 parent 3d74d0f commit 75de1d9
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions matching_handlers/src/java/method_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
}

0 comments on commit 75de1d9

Please sign in to comment.