Advertisement
kyoo0000

struct+test

Aug 26th, 2021
2,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.17 KB | None | 0 0
  1. use serde::{Serialize, Deserialize};
  2.  
  3. #[allow(dead_code)]
  4. #[derive(Serialize, Deserialize, Debug, PartialEq)]
  5. pub struct CreditCard {
  6.   amount: u32,
  7.   number: String,
  8.   date: String,
  9.   cvv: String,
  10. }
  11.  
  12. #[allow(dead_code)]
  13. impl CreditCard {
  14.   pub fn new(number: String, date: String, cvv: String) -> CreditCard {
  15.     CreditCard {
  16.       amount: 100_000,
  17.       number,
  18.       date,
  19.       cvv,
  20.     }
  21.   }
  22.  
  23.   pub fn set_amount(&mut self, amount: u32) {
  24.     self.amount = amount;
  25.   }
  26.  
  27.   pub fn get_amount(&self) -> u32 {
  28.     self.amount
  29.   }
  30. }
  31.  
  32. #[cfg(test)]
  33. mod test {
  34.   use super::*;
  35.   #[test]
  36.   fn new_credit_card_test() {
  37.     let new_cc =
  38.       CreditCard::new("123546".to_string(), "06/09".to_string(), "669".to_string());
  39.     assert_eq!(
  40.       new_cc,
  41.       CreditCard {
  42.         amount: 100_000,
  43.         number: "123546".to_string(),
  44.         date: "06/09".to_string(),
  45.         cvv: "669".to_string(),
  46.       }
  47.     );
  48.   }
  49.  
  50.   #[test]
  51.   fn set_credit_card_test() {
  52.     let mut new_cc =
  53.       CreditCard::new("123546".to_string(), "06/09".to_string(), "669".to_string());
  54.     new_cc.set_amount(200_000);
  55.     assert_eq!(new_cc.amount, 200_000);
  56.   }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement