Write units tests
This commit is contained in:
parent
af1c088507
commit
9881e3df6a
3 changed files with 66 additions and 2 deletions
6
.github/workflows/main.yml
vendored
6
.github/workflows/main.yml
vendored
|
|
@ -20,5 +20,9 @@ jobs:
|
||||||
run: cargo build --no-default-features
|
run: cargo build --no-default-features
|
||||||
- name: Build no_std with allocator
|
- name: Build no_std with allocator
|
||||||
run: cargo build --no-default-features --features alloc
|
run: cargo build --no-default-features --features alloc
|
||||||
- name: Run tests
|
- name: Test
|
||||||
run: cargo test
|
run: cargo test
|
||||||
|
- name: Test no_std
|
||||||
|
run: cargo test --no-default-features
|
||||||
|
- name: Test no_std with allocator
|
||||||
|
run: cargo test --no-default-features --features alloc
|
||||||
|
|
@ -67,6 +67,9 @@
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
/// A wrapper around a validated instance
|
/// A wrapper around a validated instance
|
||||||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||||
pub struct Valid<T>(T);
|
pub struct Valid<T>(T);
|
||||||
|
|
|
||||||
57
src/tests.rs
Normal file
57
src/tests.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
struct EvenUsize(usize);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct OddUsize;
|
||||||
|
|
||||||
|
impl Vet for EvenUsize {
|
||||||
|
type VetError = OddUsize;
|
||||||
|
fn is_valid(&self) -> Result<(), Self::VetError> {
|
||||||
|
if self.0 % 2 == 0 {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(OddUsize)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vet_type() {
|
||||||
|
let foo = EvenUsize(2);
|
||||||
|
assert!(foo.is_valid().is_ok());
|
||||||
|
|
||||||
|
let foo = EvenUsize(3);
|
||||||
|
assert!(foo.is_valid().is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn vet_option() {
|
||||||
|
let foo = None::<EvenUsize>;
|
||||||
|
assert!(foo.is_valid().is_ok());
|
||||||
|
|
||||||
|
let foo = Some(EvenUsize(4));
|
||||||
|
assert!(foo.is_valid().is_ok());
|
||||||
|
|
||||||
|
let foo = Some(EvenUsize(5));
|
||||||
|
assert!(foo.is_valid().is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
|
fn vet_vec() {
|
||||||
|
use alloc::{vec, vec::Vec};
|
||||||
|
|
||||||
|
let foo = Vec::<EvenUsize>::new();
|
||||||
|
assert!(foo.is_valid().is_ok());
|
||||||
|
|
||||||
|
let foo = vec![EvenUsize(8)];
|
||||||
|
assert!(foo.is_valid().is_ok());
|
||||||
|
|
||||||
|
let foo = vec![EvenUsize(7)];
|
||||||
|
assert!(foo.is_valid().is_err());
|
||||||
|
|
||||||
|
let foo = vec![EvenUsize(8), EvenUsize(7)];
|
||||||
|
assert!(foo.is_valid().is_err());
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue