From 8d2db9d35af95222add76054bf658e7fec702fe4 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 15 May 2024 16:09:03 +0200 Subject: [PATCH] chore(sdk): `assert_items_eq` is shared amongst several test modules. This patch moves the `assert_items_eq` macro in a place where it's accessible to several test modules. This is going to be useful for next patches. --- .../src/event_cache/linked_chunk/mod.rs | 147 +++++++++--------- 1 file changed, 75 insertions(+), 72 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs b/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs index c42136164..c86107f4c 100644 --- a/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs +++ b/crates/matrix-sdk/src/event_cache/linked_chunk/mod.rs @@ -14,6 +14,81 @@ #![allow(dead_code)] +/// A macro to test the items and the gap of a `LinkedChunk`. +/// A chunk is delimited by `[` and `]`. An item chunk has the form `[a, b, +/// c]` where `a`, `b` and `c` are items. A gap chunk has the form `[-]`. +/// +/// For example, here is an assertion of 7 chunks: 1 items chunk, 1 gap +/// chunk, 2 items chunks, 1 gap chunk, 2 items chunk. `a` is the oldest +/// item of the oldest chunk (the first chunk), and `i` is the oldest (and +/// newest) item of the newest chunk (the last chunk). +/// +/// ```rust,no_run +/// assert_items_eq!(linked_chunk, ['a'] [-] ['b', 'c', 'd'] ['e'] [-] ['f', 'g', 'h'] ['i']); +/// ``` +#[cfg(test)] +macro_rules! assert_items_eq { + ( @_ [ $iterator:ident ] { [-] $( $rest:tt )* } { $( $accumulator:tt )* } ) => { + assert_items_eq!( + @_ + [ $iterator ] + { $( $rest )* } + { + $( $accumulator )* + { + let chunk = $iterator .next().expect("next chunk (expect gap)"); + assert!(chunk.is_gap(), "chunk should be a gap"); + } + } + ) + }; + + ( @_ [ $iterator:ident ] { [ $( $item:expr ),* ] $( $rest:tt )* } { $( $accumulator:tt )* } ) => { + assert_items_eq!( + @_ + [ $iterator ] + { $( $rest )* } + { + $( $accumulator )* + { + let chunk = $iterator .next().expect("next chunk (expect items)"); + assert!(chunk.is_items(), "chunk should contain items"); + + let $crate::event_cache::linked_chunk::ChunkContent::Items(items) = chunk.content() else { + unreachable!() + }; + + let mut items_iterator = items.iter(); + + $( + assert_eq!(items_iterator.next(), Some(& $item )); + )* + + assert!(items_iterator.next().is_none(), "no more items"); + } + } + ) + }; + + ( @_ [ $iterator:ident ] {} { $( $accumulator:tt )* } ) => { + { + $( $accumulator )* + assert!( $iterator .next().is_none(), "no more chunks"); + } + }; + + ( $linked_chunk:expr, $( $all:tt )* ) => { + assert_items_eq!( + @_ + [ iterator ] + { $( $all )* } + { + let mut iterator = $linked_chunk.chunks(); + } + ) + } +} + mod as_vector; mod updates; @@ -1164,78 +1239,6 @@ mod tests { Position, }; - /// A macro to test the items and the gap of a `LinkedChunk`. - /// A chunk is delimited by `[` and `]`. An item chunk has the form `[a, b, - /// c]` where `a`, `b` and `c` are items. A gap chunk has the form `[-]`. - /// - /// For example, here is an assertion of 7 chunks: 1 items chunk, 1 gap - /// chunk, 2 items chunks, 1 gap chunk, 2 items chunk. `a` is the oldest - /// item of the oldest chunk (the first chunk), and `i` is the oldest (and - /// newest) item of the newest chunk (the last chunk). - /// - /// ```rust,no_run - /// assert_items_eq!(linked_chunk, ['a'] [-] ['b', 'c', 'd'] ['e'] [-] ['f', 'g', 'h'] ['i']); - /// ``` - macro_rules! assert_items_eq { - ( @_ [ $iterator:ident ] { [-] $( $rest:tt )* } { $( $accumulator:tt )* } ) => { - assert_items_eq!( - @_ - [ $iterator ] - { $( $rest )* } - { - $( $accumulator )* - { - let chunk = $iterator .next().expect("next chunk (expect gap)"); - assert!(chunk.is_gap(), "chunk should be a gap"); - } - } - ) - }; - - ( @_ [ $iterator:ident ] { [ $( $item:expr ),* ] $( $rest:tt )* } { $( $accumulator:tt )* } ) => { - assert_items_eq!( - @_ - [ $iterator ] - { $( $rest )* } - { - $( $accumulator )* - { - let chunk = $iterator .next().expect("next chunk (expect items)"); - assert!(chunk.is_items(), "chunk should contain items"); - - let ChunkContent::Items(items) = chunk.content() else { unreachable!() }; - - let mut items_iterator = items.iter(); - - $( - assert_eq!(items_iterator.next(), Some(& $item )); - )* - - assert!(items_iterator.next().is_none(), "no more items"); - } - } - ) - }; - - ( @_ [ $iterator:ident ] {} { $( $accumulator:tt )* } ) => { - { - $( $accumulator )* - assert!( $iterator .next().is_none(), "no more chunks"); - } - }; - - ( $linked_chunk:expr, $( $all:tt )* ) => { - assert_items_eq!( - @_ - [ iterator ] - { $( $all )* } - { - let mut iterator = $linked_chunk.chunks(); - } - ) - } - } - #[test] fn test_chunk_identifier_generator() { let generator = ChunkIdentifierGenerator::new_from_scratch();