Skip to content

Commit 3bf80c5

Browse files
LucaCappelletti94alamb
authored andcommitted
Cover first_char's guard against ignored errors
1 parent 31ed161 commit 3bf80c5

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

‎src/display_utils.rs‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,34 @@ mod tests {
157157

158158
#[test]
159159
fn test_first_char_stops_at_first_char() {
160-
struct EmptyThenChars(core::cell::Cell<bool>);
160+
struct Chunks(core::cell::Cell<usize>);
161161

162-
impl Display for EmptyThenChars {
162+
impl Display for Chunks {
163163
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164-
f.write_str("")?;
165-
f.write_str("ab")?;
166-
self.0.set(true);
167-
Ok(())
164+
["", "ab", "c"].iter().try_for_each(|chunk| {
165+
self.0.set(self.0.get() + 1);
166+
f.write_str(chunk)
167+
})
168168
}
169169
}
170170

171-
let value = EmptyThenChars(Default::default());
172-
assert_eq!(first_char(&value), Some('a'));
173-
assert!(!value.0.get(), "rendered past the first char");
171+
let chunks = Chunks(Default::default());
172+
assert_eq!(first_char(&chunks), Some('a'));
173+
assert_eq!(chunks.0.get(), 2, "rendered past the first char");
174174
assert_eq!(first_char(&""), None);
175175
}
176+
177+
#[test]
178+
fn test_first_char_keeps_first_char_when_errors_are_ignored() {
179+
struct IgnoresErrors;
180+
181+
impl Display for IgnoresErrors {
182+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
183+
let _ = f.write_str("a");
184+
f.write_str("b")
185+
}
186+
}
187+
188+
assert_eq!(first_char(&IgnoresErrors), Some('a'));
189+
}
176190
}

0 commit comments

Comments
 (0)