Skip to content

Commit 537ef70

Browse files
committed
test(common): Add tests for LinkedChunkBuilder::from_last_chunk.
This patch adds tests for the `LinkedChunkBuilder::from_last_chunk` method.
1 parent 2c406ed commit 537ef70

File tree

1 file changed

+104
-1
lines changed
  • crates/matrix-sdk-common/src/linked_chunk

1 file changed

+104
-1
lines changed

crates/matrix-sdk-common/src/linked_chunk/builder.rs

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,109 @@ pub enum LinkedChunkBuilderError {
213213
ChunkTooLarge { id: ChunkIdentifier },
214214
}
215215

216+
#[cfg(test)]
217+
mod tests {
218+
219+
use assert_matches::assert_matches;
220+
221+
use super::{
222+
ChunkContent, ChunkIdentifier, ChunkIdentifierGenerator, LinkedChunkBuilder,
223+
LinkedChunkBuilderError, RawChunk,
224+
};
225+
226+
#[test]
227+
fn test_from_last_chunk_err_too_much_items() {
228+
let last_chunk = RawChunk {
229+
previous: None,
230+
identifier: ChunkIdentifier::new(0),
231+
next: None,
232+
content: ChunkContent::Items(vec!['a', 'b', 'c']),
233+
};
234+
let chunk_identifier_generator =
235+
ChunkIdentifierGenerator::new_from_previous_chunk_identifier(ChunkIdentifier::new(0));
236+
237+
let maybe_linked_chunk = LinkedChunkBuilder::from_last_chunk::<2, char, ()>(
238+
Some(last_chunk),
239+
chunk_identifier_generator,
240+
);
241+
242+
assert_matches!(
243+
maybe_linked_chunk,
244+
Err(LinkedChunkBuilderError::ChunkTooLarge { id }) => {
245+
assert_eq!(id, 0);
246+
}
247+
);
248+
}
249+
250+
#[test]
251+
fn test_from_last_chunk_err_is_not_last_chunk() {
252+
let last_chunk = RawChunk {
253+
previous: None,
254+
identifier: ChunkIdentifier::new(0),
255+
next: Some(ChunkIdentifier::new(42)),
256+
content: ChunkContent::Items(vec!['a']),
257+
};
258+
let chunk_identifier_generator =
259+
ChunkIdentifierGenerator::new_from_previous_chunk_identifier(ChunkIdentifier::new(0));
260+
261+
let maybe_linked_chunk = LinkedChunkBuilder::from_last_chunk::<2, char, ()>(
262+
Some(last_chunk),
263+
chunk_identifier_generator,
264+
);
265+
266+
assert_matches!(
267+
maybe_linked_chunk,
268+
Err(LinkedChunkBuilderError::ChunkIsNotLast { id }) => {
269+
assert_eq!(id, 0);
270+
}
271+
);
272+
}
273+
274+
#[test]
275+
fn test_from_last_chunk_none() {
276+
let chunk_identifier_generator =
277+
ChunkIdentifierGenerator::new_from_previous_chunk_identifier(ChunkIdentifier::new(0));
278+
279+
let maybe_linked_chunk =
280+
LinkedChunkBuilder::from_last_chunk::<2, char, ()>(None, chunk_identifier_generator)
281+
.unwrap();
282+
283+
assert!(maybe_linked_chunk.is_none());
284+
}
285+
286+
#[test]
287+
fn test_from_last_chunk() {
288+
let last_chunk = RawChunk {
289+
previous: Some(ChunkIdentifier::new(42)),
290+
identifier: ChunkIdentifier::new(0),
291+
next: None,
292+
content: ChunkContent::Items(vec!['a']),
293+
};
294+
let chunk_identifier_generator =
295+
ChunkIdentifierGenerator::new_from_previous_chunk_identifier(ChunkIdentifier::new(0));
296+
297+
let maybe_linked_chunk = LinkedChunkBuilder::from_last_chunk::<2, char, ()>(
298+
Some(last_chunk),
299+
chunk_identifier_generator,
300+
)
301+
.unwrap();
302+
303+
assert_matches!(maybe_linked_chunk, Some(mut linked_chunk) => {
304+
let mut chunks = linked_chunk.chunks();
305+
306+
assert_matches!(chunks.next(), Some(chunk) => {
307+
assert_eq!(chunk.identifier(), 0);
308+
// The chunk's previous has been set to `None`
309+
assert!(chunk.previous().is_none());
310+
});
311+
assert!(chunks.next().is_none());
312+
313+
// It has updates enabled.
314+
assert!(linked_chunk.updates().is_some());
315+
});
316+
}
317+
}
318+
216319
/// A temporary chunk representation in the [`LinkedChunkBuilderTest`].
217320
///
218321
/// Instead of using linking the chunks with pointers, this uses
@@ -480,7 +583,7 @@ pub enum LinkedChunkBuilderTestError {
480583
}
481584

482585
#[cfg(test)]
483-
mod tests {
586+
mod linked_builder_test_tests {
484587
use assert_matches::assert_matches;
485588

486589
use super::{ChunkIdentifier, LinkedChunkBuilderTest, LinkedChunkBuilderTestError};

0 commit comments

Comments
 (0)