radicle: Fix `cob::common::Title::new`

The title should be trimmed before checking whether it is empty.
Also added a few tests.
This commit is contained in:
Defelo 2025-09-23 08:50:08 +02:00
parent 5fea9ac05c
commit 9f62a82b0b
No known key found for this signature in database
GPG Key ID: 2A05272471204DD3
1 changed files with 17 additions and 4 deletions

View File

@ -43,7 +43,7 @@ impl Deref for Timestamp {
} }
} }
#[derive(Error, Debug)] #[derive(Error, Debug, PartialEq, Eq)]
pub enum TitleError { pub enum TitleError {
#[error("empty title")] #[error("empty title")]
EmptyTitle, EmptyTitle,
@ -70,11 +70,14 @@ impl Title {
/// characters /// characters
pub fn new(title: &str) -> Result<Self, TitleError> { pub fn new(title: &str) -> Result<Self, TitleError> {
if title.contains('\n') || title.contains('\r') { if title.contains('\n') || title.contains('\r') {
Err(TitleError::InvalidTitle) return Err(TitleError::InvalidTitle);
} else if title.is_empty() { }
let title = title.trim();
if title.is_empty() {
Err(TitleError::EmptyTitle) Err(TitleError::EmptyTitle)
} else { } else {
Ok(Self(title.trim().to_string())) Ok(Self(title.into()))
} }
} }
} }
@ -493,6 +496,16 @@ mod test {
use super::*; use super::*;
#[test]
fn test_title() {
assert_eq!(Title::new(""), Err(TitleError::EmptyTitle));
assert_eq!(Title::new(" "), Err(TitleError::EmptyTitle));
assert_eq!(Title::new("\t"), Err(TitleError::EmptyTitle));
assert_eq!(Title::new("foo\nbar"), Err(TitleError::InvalidTitle));
assert_eq!(Title::new("foobar\n"), Err(TitleError::InvalidTitle));
assert_eq!(Title::new(" valid title ").unwrap().0, "valid title");
}
#[test] #[test]
fn test_color() { fn test_color() {
let c = Color::from_str("#ffccaa").unwrap(); let c = Color::from_str("#ffccaa").unwrap();