Skip to content

Commit

Permalink
test: traits_in_action_test
Browse files Browse the repository at this point in the history
  • Loading branch information
raghav-rama committed Feb 27, 2024
1 parent db8bcb0 commit afeb9d8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bin/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mod async_get_request_test;
mod blocking_get_request_test;
mod environment_variable_test;
mod read_json_test;
mod traits_in_action_test;
mod write_json_tests;
55 changes: 55 additions & 0 deletions src/bin/tests/traits_in_action_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[cfg(test)]
mod traits_in_action_test {
use super::{book::Book, impl_me::Loggable};
#[test]
fn it_constructs_book_properly() {
let book = Book::new("Family Problems: The Randi Rona", "Raman Raghav");
assert_eq!(book.title, "Family Problems: The Randi Rona");
assert_eq!(book.author, "Raman Raghav");
}
#[test]
fn it_gets_description_properly() {
let book = Book::new("Family Problems: The Randi Rona", "Raman Raghav");
let description = book.get_description();
assert_eq!(
description,
"Family Problems: The Randi Rona by Raman Raghav"
);
}
}

pub mod book {
pub struct Book {
pub title: String,
pub author: String,
}
}

pub mod book_impl {
use super::book::Book;
impl Book {
pub fn new(title: &str, author: &str) -> Self {
Self {
title: title.to_string(),
author: author.to_string(),
}
}
}
}

pub mod book_loggable_impl {
use super::book::Book;
use super::impl_me::Loggable;

impl Loggable for Book {
fn get_description(&self) -> String {
format!("{} by {}", self.title, self.author)
}
}
}

pub mod impl_me {
pub trait Loggable {
fn get_description(&self) -> String;
}
}

0 comments on commit afeb9d8

Please sign in to comment.