[Add] exercises\tests

This commit is contained in:
Phan Huy Hoang 2023-01-12 00:01:03 +07:00
parent 59e4589679
commit bacbb59c77
4 changed files with 25 additions and 15 deletions

View File

@ -14,10 +14,27 @@
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE use std::fmt::{Display, Formatter, Result as FmtResult};
pub enum Grade {
Numeric(f32),
Alphabetical(String),
}
impl Display for Grade {
fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
let grade = match &self {
Grade::Numeric(grade) => grade.to_string(),
Grade::Alphabetical(grade) => grade.to_string(),
};
write!(fmt, "{}", grade);
Ok(())
}
}
pub struct ReportCard { pub struct ReportCard {
pub grade: f32, pub grade: Grade,
pub student_name: String, pub student_name: String,
pub student_age: u8, pub student_age: u8,
} }
@ -36,7 +53,7 @@ mod tests {
#[test] #[test]
fn generate_numeric_report_card() { fn generate_numeric_report_card() {
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: Grade::Numeric(2.1),
student_name: "Tom Wriggle".to_string(), student_name: "Tom Wriggle".to_string(),
student_age: 12, student_age: 12,
}; };
@ -48,9 +65,8 @@ mod tests {
#[test] #[test]
fn generate_alphabetic_report_card() { fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard { let report_card = ReportCard {
grade: 2.1, grade: Grade::Alphabetical("A+".into()),
student_name: "Gary Plotter".to_string(), student_name: "Gary Plotter".to_string(),
student_age: 11, student_age: 11,
}; };

View File

@ -7,12 +7,10 @@
// pass! Make the test fail! // pass! Make the test fail!
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint tests1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert() { fn you_can_assert() {
assert!(); assert!(true, true);
} }
} }

View File

@ -3,12 +3,10 @@
// pass! Make the test fail! // pass! Make the test fail!
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint tests2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
fn you_can_assert_eq() { fn you_can_assert_eq() {
assert_eq!(); assert_eq!(true, true);
} }
} }

View File

@ -4,8 +4,6 @@
// we expect to get when we call `is_even(5)`. // we expect to get when we call `is_even(5)`.
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint tests3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub fn is_even(num: i32) -> bool { pub fn is_even(num: i32) -> bool {
num % 2 == 0 num % 2 == 0
} }
@ -16,11 +14,11 @@ mod tests {
#[test] #[test]
fn is_true_when_even() { fn is_true_when_even() {
assert!(); assert!(is_even(4));
} }
#[test] #[test]
fn is_false_when_odd() { fn is_false_when_odd() {
assert!(); assert!(is_even(5) == false);
} }
} }