Coverage Report

Created: 2025-06-16 06:50

/rust/registry/src/index.crates.io-6f17d22bba15001f/writeable-0.5.5/src/testing.rs
Line
Count
Source (jump to first uncovered line)
1
// This file is part of ICU4X. For terms of use, please see the file
2
// called LICENSE at the top level of the ICU4X source tree
3
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5
use crate::*;
6
use alloc::string::String;
7
use alloc::vec::Vec;
8
9
pub(crate) struct TestWriter {
10
    pub(crate) string: String,
11
    pub(crate) parts: Vec<(usize, usize, Part)>,
12
}
13
14
impl TestWriter {
15
0
    pub(crate) fn finish(mut self) -> (String, Vec<(usize, usize, Part)>) {
16
0
        // Sort by first open and last closed
17
0
        self.parts
18
0
            .sort_unstable_by_key(|(begin, end, _)| (*begin, end.wrapping_neg()));
19
0
        (self.string, self.parts)
20
0
    }
21
}
22
23
impl fmt::Write for TestWriter {
24
0
    fn write_str(&mut self, s: &str) -> fmt::Result {
25
0
        self.string.write_str(s)
26
0
    }
27
0
    fn write_char(&mut self, c: char) -> fmt::Result {
28
0
        self.string.write_char(c)
29
0
    }
30
}
31
32
impl PartsWrite for TestWriter {
33
    type SubPartsWrite = Self;
34
0
    fn with_part(
35
0
        &mut self,
36
0
        part: Part,
37
0
        mut f: impl FnMut(&mut Self::SubPartsWrite) -> fmt::Result,
38
0
    ) -> fmt::Result {
39
0
        let start = self.string.len();
40
0
        f(self)?;
41
0
        let end = self.string.len();
42
0
        if start < end {
43
0
            self.parts.push((start, end, part));
44
0
        }
45
0
        Ok(())
46
0
    }
47
}
48
49
#[allow(clippy::type_complexity)]
50
0
pub fn writeable_to_parts_for_test<W: Writeable>(
51
0
    writeable: &W,
52
0
) -> (String, Vec<(usize, usize, Part)>) {
53
0
    let mut writer = TestWriter {
54
0
        string: alloc::string::String::new(),
55
0
        parts: Vec::new(),
56
0
    };
57
0
    #[allow(clippy::expect_used)] // for test code
58
0
    writeable
59
0
        .write_to_parts(&mut writer)
60
0
        .expect("String writer infallible");
61
0
    writer.finish()
62
0
}
63
64
#[allow(clippy::type_complexity)]
65
0
pub fn try_writeable_to_parts_for_test<W: TryWriteable>(
66
0
    writeable: &W,
67
0
) -> (String, Vec<(usize, usize, Part)>, Option<W::Error>) {
68
0
    let mut writer = TestWriter {
69
0
        string: alloc::string::String::new(),
70
0
        parts: Vec::new(),
71
0
    };
72
0
    #[allow(clippy::expect_used)] // for test code
73
0
    let result = writeable
74
0
        .try_write_to_parts(&mut writer)
75
0
        .expect("String writer infallible");
76
0
    let (actual_str, actual_parts) = writer.finish();
77
0
    (actual_str, actual_parts, result.err())
78
0
}