feat: method to check if Quantity is empty
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ci/woodpecker/pr/woodpecker Pipeline failed
ci/woodpecker/pull_request_closed/woodpecker Pipeline failed

This commit is contained in:
Aravinth Manivannan 2024-07-17 23:46:01 +05:30
parent 8be134956b
commit 7b0492d926
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 94 additions and 0 deletions

View file

@ -1 +1,5 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
pub mod quantity;

View file

@ -54,6 +54,12 @@ pub struct QuantityPart {
unit: QuantityUnit,
}
impl QuantityPart {
pub fn is_empty(&self) -> bool {
self.number == 0
}
}
#[derive(
Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Builder, Getters,
)]
@ -62,10 +68,78 @@ pub struct Quantity {
major: QuantityPart,
}
impl Quantity {
pub fn is_empty(&self) -> bool {
self.minor.is_empty() == true && self.major.is_empty() == true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quantity_part_empty() {
let q = QuantityPartBuilder::default()
.number(0)
.unit(QuantityUnit::DiscreteNumber)
.build()
.unwrap();
assert!(q.is_empty());
let q_not_empty = QuantityPartBuilder::default()
.number(1)
.unit(QuantityUnit::DiscreteNumber)
.build()
.unwrap();
assert!(!q_not_empty.is_empty());
}
#[test]
fn quantity_empty() {
let q = QuantityPartBuilder::default()
.number(0)
.unit(QuantityUnit::DiscreteNumber)
.build()
.unwrap();
let q_not_empty = QuantityPartBuilder::default()
.number(1)
.unit(QuantityUnit::DiscreteNumber)
.build()
.unwrap();
// not empty; major has items
assert!(!QuantityBuilder::default()
.minor(q.clone())
.major(q_not_empty.clone())
.build()
.unwrap()
.is_empty());
// not empty; minor has items
assert!(!QuantityBuilder::default()
.minor(q.clone())
.major(q_not_empty.clone())
.build()
.unwrap()
.is_empty());
// not empty; minor & major have items
assert!(!QuantityBuilder::default()
.minor(q_not_empty.clone())
.major(q_not_empty.clone())
.build()
.unwrap()
.is_empty());
// empty
assert!(QuantityBuilder::default()
.minor(q.clone())
.major(q)
.build()
.unwrap()
.is_empty());
}
fn test_helper<T>(t: T, str_value: &str) -> bool
where
T: ToString + FromStr + std::fmt::Debug + PartialEq,
@ -105,4 +179,20 @@ mod tests {
fn quantity_unit_liter() {
assert!(test_helper(QuantityUnit::Liter, LITER));
}
impl Quantity {
pub fn get_quantity() -> Self {
let q = QuantityPartBuilder::default()
.number(1)
.unit(QuantityUnit::DiscreteNumber)
.build()
.unwrap();
QuantityBuilder::default()
.minor(q.clone())
.major(q.clone())
.build()
.unwrap()
}
}
}