Created
May 1, 2022 13:11
-
-
Save algermissen/b225db7ed9f91976530816955a0f2f02 to your computer and use it in GitHub Desktop.
COnditional compile test support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, Clone, Default)] | |
pub struct User{ | |
} | |
#[derive(Debug, Clone, Default)] | |
pub struct Profile{ | |
} | |
#[derive(Default)] | |
pub struct DB { | |
pub c: i32, | |
#[cfg(test)] | |
test_user: User, | |
#[cfg(test)] | |
test_profile: Profile, | |
} | |
#[cfg(not(test))] | |
impl DB { | |
pub fn load_user(&self, id: i32) -> User { | |
User{} | |
} | |
pub fn load_profile(&self, id: i32) -> Profile { | |
Profile{} | |
} | |
} | |
#[cfg(test)] | |
impl DB { | |
pub fn load_user(&self, id: i32) -> User { | |
self.test_user.clone() | |
} | |
pub fn load_profile(&self, id: i32) -> Profile { | |
self.test_profile.clone() | |
} | |
pub fn set_user(&mut self, u: User) -> &mut Self { | |
self.test_user = u; | |
self | |
} | |
pub fn set_profile(&mut self, p: Profile) -> &mut Self { | |
self.test_profile = p; | |
self | |
} | |
} | |
pub fn get_user(db: DB, user_id: i32) -> (User,Profile) { | |
let u = db.load_user(user_id); | |
let p = db.load_profile(user_id); | |
(u,p) | |
} | |
#[cfg(test)] | |
mod tests { | |
use crate::{DB, get_user, Profile, User}; | |
#[test] | |
fn it_works() { | |
let mut db = DB{ c: 0 , ..DB::default()}; | |
db.set_user(User::default()).set_profile(Profile::default()); | |
let (u,p) = get_user(db,42); | |
dbg!(u); | |
dbg!(p); | |
assert_eq!(1,1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment