Skip to content

Commit

Permalink
impl test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Feb 23, 2024
1 parent 4c80853 commit 764d757
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
56 changes: 56 additions & 0 deletions src/rule/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,59 @@ mod odd;

pub use even::*;
pub use odd::*;

#[cfg(test)]
mod test {
use crate::rule::composer::{And, Or};
use crate::{equal_rule, greater_rule, less_rule, Refined};

greater_rule!((18, u8));
less_rule!((80, u8));
equal_rule!((18, u8), (80, u8));

type TargetAge = Refined<TargetAgeRule>;

// 18 <= age
type TargetAge18OrMore = Or<EqualRule18u8, GreaterRule18u8>;

// age <= 80
type TargetAge80OrLess = Or<EqualRule80u8, LessRule80u8>;

// 18 <= age <= 80
type TargetAgeRule = And<TargetAge18OrMore, TargetAge80OrLess>;

#[test]
fn test_age_0() -> anyhow::Result<()> {
let age_result = TargetAge::new(0);
assert!(age_result.is_err());
Ok(())
}

#[test]
fn test_age_18() -> anyhow::Result<()> {
let age_result = TargetAge::new(18);
assert!(age_result.is_ok());
Ok(())
}

#[test]
fn test_age_50() -> anyhow::Result<()> {
let age_result = TargetAge::new(50);
assert!(age_result.is_ok());
Ok(())
}

#[test]
fn test_age_80() -> anyhow::Result<()> {
let age_result = TargetAge::new(80);
assert!(age_result.is_ok());
Ok(())
}

#[test]
fn test_age_81() -> anyhow::Result<()> {
let age_result = TargetAge::new(81);
assert!(age_result.is_err());
Ok(())
}
}
9 changes: 6 additions & 3 deletions src/rule/number/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
macro_rules! equal_rule {
(($e: literal, $t: ty)) => {
paste::item! {
#[allow(dead_code)]
pub type [<Equal $e $t>] = $crate::Refined<[<EqualRule $e $t>]>;

#[allow(dead_code)]
pub struct [<EqualRule $e $t>];

impl $crate::rule::Rule for [<EqualRule $e $t>] {
Expand All @@ -18,9 +21,9 @@ macro_rules! equal_rule {
}
}
};
($t: ty, $($ts: ty),+) => {
odd_rule! {$t}
odd_rule! {$($ts), +}
(($e: literal, $t: ty), $(($es: literal, $ts: ty)),+) => {
equal_rule!(($e, $t));
equal_rule!($(($es, $ts)), +);
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/rule/number/even.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
macro_rules! even_rule {
($t: ty) => {
paste::item! {
#[allow(dead_code)]
pub type [<Even $t:upper>] = $crate::Refined<[<EvenRule $t:upper>]>;

#[allow(dead_code)]
pub struct [<EvenRule $t:upper>];

impl $crate::rule::Rule for [<EvenRule $t:upper>] {
Expand Down
3 changes: 3 additions & 0 deletions src/rule/number/greater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
macro_rules! greater_rule {
(($e: literal, $t: ty)) => {
paste::item! {
#[allow(dead_code)]
pub type [<Greater $e $t>] = $crate::Refined<[<GreaterRule $e $t>]>;

#[allow(dead_code)]
pub struct [<GreaterRule $e $t>];

impl $crate::rule::Rule for [<GreaterRule $e $t>] {
Expand Down
3 changes: 3 additions & 0 deletions src/rule/number/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
macro_rules! less_rule {
(($e: literal, $t: ty)) => {
paste::item! {
#[allow(dead_code)]
pub type [<Less $e $t>] = $crate::Refined<[<LessRule $e $t>]>;

#[allow(dead_code)]
pub struct [<LessRule $e $t>];

impl $crate::rule::Rule for [<LessRule $e $t>] {
Expand Down

0 comments on commit 764d757

Please sign in to comment.