From fab1a337be7c5f0b86bf8384a2640ee3e588dec5 Mon Sep 17 00:00:00 2001 From: Augusto Date: Mon, 28 Dec 2020 19:49:23 +0000 Subject: [PATCH] Add the subject and test of the exercise `events` --- rust/tests/events_test/Cargo.lock | 138 ++++++++++++++++++++++++++++ rust/tests/events_test/Cargo.toml | 12 +++ rust/tests/events_test/src/main.rs | 141 +++++++++++++++++++++++++++++ subjects/events/README.md | 115 +++++++++++++++++++++++ 4 files changed, 406 insertions(+) create mode 100644 rust/tests/events_test/Cargo.lock create mode 100644 rust/tests/events_test/Cargo.toml create mode 100644 rust/tests/events_test/src/main.rs create mode 100644 subjects/events/README.md diff --git a/rust/tests/events_test/Cargo.lock b/rust/tests/events_test/Cargo.lock new file mode 100644 index 00000000..01eb47d0 --- /dev/null +++ b/rust/tests/events_test/Cargo.lock @@ -0,0 +1,138 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + +[[package]] +name = "events" +version = "0.1.0" +dependencies = [ + "chrono", + "colored", +] + +[[package]] +name = "events_test" +version = "0.1.0" +dependencies = [ + "chrono", + "colored", + "events", +] + +[[package]] +name = "hermit-abi" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +dependencies = [ + "libc", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" + +[[package]] +name = "num-integer" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" +dependencies = [ + "autocfg", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/rust/tests/events_test/Cargo.toml b/rust/tests/events_test/Cargo.toml new file mode 100644 index 00000000..958cd033 --- /dev/null +++ b/rust/tests/events_test/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "events_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = "0.4" +colored = "2" +events = { path = "../../../../rust-piscine-solutions/events"} \ No newline at end of file diff --git a/rust/tests/events_test/src/main.rs b/rust/tests/events_test/src/main.rs new file mode 100644 index 00000000..f5f5e0e0 --- /dev/null +++ b/rust/tests/events_test/src/main.rs @@ -0,0 +1,141 @@ +// You're have to design a notification system for a platform +// This events are Remainders, Registrations, Appointments, Holidays +// Create an event handler that depending of the type of event creates +// different notification: different color, different size and +// different position + +// The possible positions are Top, Bottom and Center: Create and Enum +// `Position` with those values + +// Create a struct called `Notification` with the fields +// size: u32, +// color: (u8, u8, u8), +// position: Position, +// content: String, + +// The event that you have to handle are +// enum Event { +// Remainder(&str), +// Registration(Duration), +// Appointment(&str), +// Holiday, +// } + +// Create a method called `notify` +// fn notify(&self) -> Notification +// That returns a notification with the following caracteristics for +// each +// Remainder: +// size= 50, +// color= (50, 50, 50), +// position= Bottom, +// content= the slice associated to the enum value + +// Registration(chrono::Duration), +// size = 30, +// color = (255, 2, 22), +// position = Top, +// content = "You have `duration` left before the registration ends", +// `durations` must be displayed in the form of +// {hours}:{minutes}:{seconds} left for the beginning of the event +// for example if there is two hours 32 minutes and 3 seconds left +// before the registration then the content will be `You have 2:32:2 left before the registration ends` + +// Appointment(text) +// size: 100 +// color: (200, 200, 3) +// position: Center +// content: text associated to the value + +// Holiday +// size: 25 +// color: (0, 255, 0) +// position: Top +// content: "Enjoy your holiday" + +use chrono::Duration; +use events::Event::*; +#[allow(unused_imports)] +use events::{Notification, Position}; + +#[allow(dead_code)] +fn main() { + let remainder = Remainder("Go to the doctor"); + println!("{}", remainder.notify()); + let registration = Registration(Duration::seconds(49094)); + println!("{}", registration.notify()); + let appointment = Appointment("Go to the doctor"); + println!("{}", appointment.notify()); + let holiday = Holiday; + println!("{}", holiday.notify()); +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn remainder_notification() { + let remainder = Remainder("Go to the doctor"); + let notification = remainder.notify(); + println!("{}", ¬ification); + assert_eq!( + notification, + Notification { + size: 50, + color: (50, 50, 50), + position: Position::Bottom, + content: "Go to the doctor".to_string(), + } + ); + } + + #[test] + fn registration_notification() { + let registration = Registration(Duration::seconds(49094)); + let notification = registration.notify(); + println!("{}", registration.notify()); + assert_eq!( + notification, + Notification { + size: 30, + color: (255, 2, 22), + position: Position::Top, + content: "You have 13H:38M:14S left before the registration ends".to_string(), + } + ); + } + + #[test] + fn appointment_notification() { + let appointment = Appointment("Go to the doctor"); + let notification = appointment.notify(); + println!("{}", ¬ification); + assert_eq!( + notification, + Notification { + size: 100, + color: (200, 200, 3), + position: Position::Center, + content: "Go to the doctor".to_string(), + } + ); + } + + #[test] + fn holiday_notification() { + let holiday = Holiday; + let notification = Holiday.notify(); + println!("{}", holiday.notify()); + assert_eq!( + notification, + Notification { + size: 25, + color: (0, 255, 0), + position: Position::Top, + content: String::from("Enjoy your holiday"), + } + ); + } +} diff --git a/subjects/events/README.md b/subjects/events/README.md new file mode 100644 index 00000000..31d9f2da --- /dev/null +++ b/subjects/events/README.md @@ -0,0 +1,115 @@ +## events + +### Instructions + +You're have to design a notification system for a platform + +This events can be: Remainders, Registrations, Appointments or Holidays + +- Create an event handler that depending of the type of event creates different notification: different color, different size and different position + +- The possible positions are Top, Bottom and Center: Create and Enum `Position` with those values + +- Create a method called `notify` which returns a notification with the following caracteristics for each + + - Remainder: + size= 50, + color= (50, 50, 50), + position= Bottom, + content= the slice associated to the enum value + + - Registration(chrono::Duration), + size = 30, + color = (255, 2, 22), + position = Top, + content = "You have `duration` left before the registration ends", + + `durations` must be displayed in the form of {hours}:{minutes}:{seconds} left for the beginning of the event for example if there is two hours 32 minutes and 3 seconds left before the registration then the content will be `You have 2:32:2 left before the registration ends` + + - Appointment(text) + size: 100 + color: (200, 200, 3) + position: Center + content: text associated to the value + + - Holiday + size: 25 + color: (0, 255, 0) + position: Top + content: "Enjoy your holiday" + +- Implement the std::fmt::Display trait so the text of the notification is printed in the right color in the command line + +### Notions + +- https://docs.rs/colored/2.0.0/colored/ + +### Expected Functions and Data Structures + +```rust +use chrono::Duration; +use colored::*; + +#[derive(Debug, Eq, PartialEq)] +enum Position { + Top, + Bottom, + Center, +} + +#[derive(Debug, Eq, PartialEq)] +struct Notification { + size: u32, + color: (u8, u8, u8), + position: Position, + content: String, +} + +#[derive(Debug)] +enum Event<'a> { + Remainder(&'a str), + Registration(Duration), + Appointment(&'a str), + Holiday, +} + +use std::fmt; + +impl fmt::Display for Notification { +} + +use Event::*; + +impl Event { + fn notify(&self) -> Notification { + } +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let remainder = Remainder("Go to the doctor"); + println!("{}", remainder.notify()); + let registration = Registration(Duration::seconds(49094)); + println!("{}", registration.notify()); + let appointment = Appointment("Go to the doctor"); + println!("{}", appointment.notify()); + let holiday = Holiday; + println!("{}", holiday.notify()); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +(Bottom, 50, Go to the doctor) +(Top, 30, You have 13H:38M:14S left before the registration ends) +(Center, 100, Go to the doctor) +(Top, 25, Enjoy your holiday) +student@ubuntu:~/[[ROOT]]/test$ +```