From f64f96251f897c1e105164c173bb9ac1510a2716 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 6 Dec 2022 13:04:33 +0900 Subject: [PATCH] revset: add basic tests for revset combinator I've made MutableIndex::add_commit_data() crate-public since it's convenient to build an IndexEntry by that function. --- lib/src/index.rs | 2 +- lib/src/revset.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index 64dc3bbce..329dc807d 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -366,7 +366,7 @@ impl MutableIndex { ); } - fn add_commit_data( + pub(crate) fn add_commit_data( &mut self, commit_id: CommitId, change_id: ChangeId, diff --git a/lib/src/revset.rs b/lib/src/revset.rs index e666125fc..7812485ef 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -1850,6 +1850,8 @@ fn has_diff_from_parent(repo: RepoRef<'_>, entry: &IndexEntry<'_>, matcher: &dyn #[cfg(test)] mod tests { use super::*; + use crate::commit_builder::new_change_id; + use crate::index::MutableIndex; fn parse(revset_str: &str) -> Result, RevsetParseErrorKind> { parse_with_aliases(revset_str, [] as [(&str, &str); 0]) @@ -2866,4 +2868,53 @@ mod tests { ) "###); } + + #[test] + fn test_revset_combinator() { + let mut index = MutableIndex::full(3); + let id_0 = CommitId::from_hex("000000"); + let id_1 = CommitId::from_hex("111111"); + let id_2 = CommitId::from_hex("222222"); + let id_3 = CommitId::from_hex("333333"); + let id_4 = CommitId::from_hex("444444"); + index.add_commit_data(id_0.clone(), new_change_id(), &[]); + index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]); + index.add_commit_data(id_2.clone(), new_change_id(), &[id_1.clone()]); + index.add_commit_data(id_3.clone(), new_change_id(), &[id_2.clone()]); + index.add_commit_data(id_4.clone(), new_change_id(), &[id_3.clone()]); + + let get_entry = |id: &CommitId| index.entry_by_id(id).unwrap(); + let make_entries = |ids: &[&CommitId]| ids.iter().map(|id| get_entry(id)).collect_vec(); + let make_set = |ids: &[&CommitId]| -> Box> { + let index_entries = make_entries(ids); + Box::new(EagerRevset { index_entries }) + }; + + let set = FilterRevset { + candidates: make_set(&[&id_4, &id_2, &id_0]), + predicate: Box::new(|entry| entry.commit_id() != id_4), + }; + assert_eq!(set.iter().collect_vec(), make_entries(&[&id_2, &id_0])); + + let set = UnionRevset { + set1: make_set(&[&id_4, &id_2]), + set2: make_set(&[&id_3, &id_2, &id_1]), + }; + assert_eq!( + set.iter().collect_vec(), + make_entries(&[&id_4, &id_3, &id_2, &id_1]) + ); + + let set = IntersectionRevset { + set1: make_set(&[&id_4, &id_2, &id_0]), + set2: make_set(&[&id_3, &id_2, &id_1]), + }; + assert_eq!(set.iter().collect_vec(), make_entries(&[&id_2])); + + let set = DifferenceRevset { + set1: make_set(&[&id_4, &id_2, &id_0]), + set2: make_set(&[&id_3, &id_2, &id_1]), + }; + assert_eq!(set.iter().collect_vec(), make_entries(&[&id_4, &id_0])); + } }