Skip to content

Commit

Permalink
feat: add case in which one parent removes a node and adds another
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Oct 24, 2023
1 parent bb79731 commit 87d063b
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions merge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,24 @@ pub fn merge(
cur_right = children_right_it.next();
}

// Addition in left
if has_matching_base_left && !has_matching_base_right {
if !has_matching_base_left
&& matching_left_right.is_none()
&& has_matching_base_right
{
result_children.push(cur_left.unwrap().to_owned());

cur_left = children_left_it.next();
cur_right = children_right_it.next();
}

if !has_matching_base_right
&& matching_left_right.is_none()
&& has_matching_base_left
{
result_children.push(cur_right.unwrap().to_owned());

cur_left = children_left_it.next();
cur_right = children_right_it.next();
}
}

Expand Down Expand Up @@ -430,4 +444,46 @@ mod tests {
merge,
)
}

#[test]
fn it_merges_when_one_parent_adds_a_node_and_removes_one_that_was_not_edited_in_the_other() {
let base = CSTNode::NonTerminal {
kind: "kind".into(),
children: vec![CSTNode::Terminal {
kind: "kind_a".into(),
value: "value_a".into(),
}],
};

let changed_parent = CSTNode::NonTerminal {
kind: "kind".into(),
children: vec![CSTNode::Terminal {
kind: "kind_b".into(),
value: "value_b".into(),
}],
};

let unchanged_parent = CSTNode::NonTerminal {
kind: "kind".into(),
children: vec![CSTNode::Terminal {
kind: "kind_a".into(),
value: "value_a".into(),
}],
};

let expected_merge = CSTNode::NonTerminal {
kind: "kind".into(),
children: vec![CSTNode::Terminal {
kind: "kind_b".into(),
value: "value_b".into(),
}],
};

assert_merge_is_correct_and_idempotent_with_respect_to_parent_side(
base,
changed_parent,
unchanged_parent,
expected_merge,
)
}
}

0 comments on commit 87d063b

Please sign in to comment.