Write units tests

This commit is contained in:
Benjamin Herman 2022-07-09 13:06:29 -07:00
parent 53b1ae7524
commit 54db766751
No known key found for this signature in database
GPG key ID: A2D1938EC56DBF49
3 changed files with 66 additions and 2 deletions

View file

@ -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

View file

@ -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
View 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());
}