|
| 1 | +use crate::{message::Message, result::Violation, rule::Rule}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use super::Level; |
| 5 | + |
| 6 | +/// FootersEmpty represents the footer-empty rule. |
| 7 | +#[derive(Clone, Debug, Deserialize, Serialize)] |
| 8 | +pub struct FootersEmpty { |
| 9 | + /// Level represents the level of the rule. |
| 10 | + /// |
| 11 | + // Note that currently the default literal is not supported. |
| 12 | + // See: https://github.com/serde-rs/serde/issues/368 |
| 13 | + level: Option<Level>, |
| 14 | +} |
| 15 | + |
| 16 | +/// FooterEmpty represents the footer-empty rule. |
| 17 | +impl Rule for FootersEmpty { |
| 18 | + const NAME: &'static str = "footers-empty"; |
| 19 | + const LEVEL: Level = Level::Error; |
| 20 | + |
| 21 | + fn message(&self, _message: &Message) -> String { |
| 22 | + "footers are empty".to_string() |
| 23 | + } |
| 24 | + |
| 25 | + fn validate(&self, message: &Message) -> Option<Violation> { |
| 26 | + if message.footers.is_none() { |
| 27 | + return Some(Violation { |
| 28 | + level: self.level.unwrap_or(Self::LEVEL), |
| 29 | + message: self.message(message), |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + None |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// Default implementation of FooterEmpty. |
| 38 | +impl Default for FootersEmpty { |
| 39 | + fn default() -> Self { |
| 40 | + Self { |
| 41 | + level: Some(Self::LEVEL), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use std::collections::HashMap; |
| 49 | + |
| 50 | + use super::*; |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_non_empty_footer() { |
| 54 | + let rule = FootersEmpty::default(); |
| 55 | + |
| 56 | + let mut f = HashMap::new(); |
| 57 | + f.insert("Link".to_string(), "hello".to_string()); |
| 58 | + |
| 59 | + let message = Message { |
| 60 | + body: Some("Hello world".to_string()), |
| 61 | + description: Some("broadcast $destroy event on scope destruction".to_string()), |
| 62 | + footers: Some(f), |
| 63 | + r#type: Some("feat".to_string()), |
| 64 | + raw: "feat(scope): broadcast $destroy event on scope destruction |
| 65 | +
|
| 66 | +Hello world |
| 67 | +
|
| 68 | +Link: hello" |
| 69 | + .to_string(), |
| 70 | + scope: Some("scope".to_string()), |
| 71 | + subject: Some("feat(scope): broadcast $destroy event on scope destruction".to_string()), |
| 72 | + }; |
| 73 | + |
| 74 | + assert!(rule.validate(&message).is_none()); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_empty_footer() { |
| 79 | + let rule = FootersEmpty::default(); |
| 80 | + let message = Message { |
| 81 | + body: None, |
| 82 | + description: None, |
| 83 | + footers: None, |
| 84 | + r#type: Some("feat".to_string()), |
| 85 | + raw: "feat(scope): broadcast $destroy event on scope destruction".to_string(), |
| 86 | + scope: Some("scope".to_string()), |
| 87 | + subject: None, |
| 88 | + }; |
| 89 | + |
| 90 | + let violation = rule.validate(&message); |
| 91 | + assert!(violation.is_some()); |
| 92 | + assert_eq!(violation.clone().unwrap().level, Level::Error); |
| 93 | + assert_eq!(violation.unwrap().message, "footers are empty".to_string()); |
| 94 | + } |
| 95 | +} |
0 commit comments