initial commit
This commit is contained in:
7
exercises/03_ticket_v1/07_setters/Cargo.toml
Normal file
7
exercises/03_ticket_v1/07_setters/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "setters"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dev-dependencies]
|
||||
common = { path = "../../../helpers/common" }
|
||||
98
exercises/03_ticket_v1/07_setters/src/lib.rs
Normal file
98
exercises/03_ticket_v1/07_setters/src/lib.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
// TODO: Add &mut-setters to the `Ticket` struct for each of its fields.
|
||||
// Make sure to enforce the same validation rules you have in `Ticket::new`!
|
||||
// Even better, extract that logic and reuse it in both places. You can use
|
||||
// private functions or private static methods for that.
|
||||
|
||||
pub struct Ticket {
|
||||
title: String,
|
||||
description: String,
|
||||
status: String,
|
||||
}
|
||||
|
||||
impl Ticket {
|
||||
pub fn new(title: String, description: String, status: String) -> Ticket {
|
||||
if title.is_empty() {
|
||||
panic!("Title cannot be empty");
|
||||
}
|
||||
if title.len() > 50 {
|
||||
panic!("Title cannot be longer than 50 bytes");
|
||||
}
|
||||
if description.is_empty() {
|
||||
panic!("Description cannot be empty");
|
||||
}
|
||||
if description.len() > 500 {
|
||||
panic!("Description cannot be longer than 500 bytes");
|
||||
}
|
||||
if status != "To-Do" && status != "In Progress" && status != "Done" {
|
||||
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
|
||||
}
|
||||
|
||||
Ticket {
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self) -> &String {
|
||||
&self.title
|
||||
}
|
||||
|
||||
pub fn description(&self) -> &String {
|
||||
&self.description
|
||||
}
|
||||
|
||||
pub fn status(&self) -> &String {
|
||||
&self.status
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Ticket;
|
||||
use common::{overly_long_description, overly_long_title, valid_description, valid_title};
|
||||
|
||||
#[test]
|
||||
fn works() {
|
||||
let mut ticket = Ticket::new("A title".into(), "A description".into(), "To-Do".into());
|
||||
ticket.set_title("A new title".into());
|
||||
ticket.set_description("A new description".into());
|
||||
ticket.set_status("Done".into());
|
||||
|
||||
assert_eq!(ticket.title(), "A new title");
|
||||
assert_eq!(ticket.description(), "A new description");
|
||||
assert_eq!(ticket.status(), "Done");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Title cannot be empty")]
|
||||
fn title_cannot_be_empty() {
|
||||
Ticket::new(valid_title(), valid_description(), "To-Do".into()).set_title("".into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Description cannot be empty")]
|
||||
fn description_cannot_be_empty() {
|
||||
Ticket::new(valid_title(), valid_description(), "To-Do".into()).set_description("".into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Title cannot be longer than 50 bytes")]
|
||||
fn title_cannot_be_longer_than_fifty_chars() {
|
||||
Ticket::new(valid_title(), valid_description(), "To-Do".into())
|
||||
.set_title(overly_long_title())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Description cannot be longer than 500 bytes")]
|
||||
fn description_cannot_be_longer_than_500_chars() {
|
||||
Ticket::new(valid_title(), valid_description(), "To-Do".into())
|
||||
.set_description(overly_long_description())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Only `To-Do`, `In Progress`, and `Done` statuses are allowed")]
|
||||
fn status_must_be_valid() {
|
||||
Ticket::new(valid_title(), valid_description(), "To-Do".into()).set_status("Funny".into());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user