2024-05-12 22:21:03 +02:00
|
|
|
// TODO: Implement `TryFrom<String>` and `TryFrom<&str>` for the `TicketTitle` type,
|
2024-10-19 09:36:26 +02:00
|
|
|
// enforcing that the title is not empty and is not longer than 50 bytes.
|
2024-05-12 22:21:03 +02:00
|
|
|
// Implement the traits required to make the tests pass too.
|
|
|
|
|
|
|
|
pub struct TicketTitle(String);
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_try_from_string() {
|
|
|
|
let title = TicketTitle::try_from("A title".to_string()).unwrap();
|
|
|
|
assert_eq!(title.0, "A title");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_try_from_empty_string() {
|
|
|
|
let err = TicketTitle::try_from("".to_string()).unwrap_err();
|
|
|
|
assert_eq!(err.to_string(), "The title cannot be empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_try_from_long_string() {
|
|
|
|
let title =
|
|
|
|
"A title that's definitely longer than what should be allowed in a development ticket"
|
|
|
|
.to_string();
|
|
|
|
let err = TicketTitle::try_from(title).unwrap_err();
|
2024-05-22 12:06:40 +02:00
|
|
|
assert_eq!(err.to_string(), "The title cannot be longer than 50 bytes");
|
2024-05-12 22:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_try_from_str() {
|
|
|
|
let title = TicketTitle::try_from("A title").unwrap();
|
|
|
|
assert_eq!(title.0, "A title");
|
|
|
|
}
|
|
|
|
}
|