macro typed_test_gen

Macros to help generate tests from functions with a generic type parameter

3 releases

0.1.2 Oct 1, 2024
0.1.1 Jul 20, 2024
0.1.0 Sep 1, 2023

#694 in Testing

Download history 140/week @ 2024-07-29 50/week @ 2024-08-05 30/week @ 2024-08-12 11/week @ 2024-08-19 8/week @ 2024-09-02 40/week @ 2024-09-09 46/week @ 2024-09-16 66/week @ 2024-09-23 240/week @ 2024-09-30 98/week @ 2024-10-07 65/week @ 2024-10-14 50/week @ 2024-10-21 25/week @ 2024-10-28 39/week @ 2024-11-04 15/week @ 2024-11-11

130 downloads per month
Used in twibint

GPL-3.0-only

16KB
118 lines

typed_test_gen

crate documentation

Rust crate for defining a macro that automatically specializes generic tests on provided types. This is useful when we want to write identical tests with different underlying types. This crate provide the syntax #[test_with(Type1, Type2)] which will instantiate 2 tests: one on Type1, the other on Type2. Those types could be any path to any generic type like module::something::Type<Gen>.

An example is better than words:

#[test_with(u32, u64, char)]
fn test_vec<T>() {
    let vec = Vec::<T>::with_capacity(10);
    assert_eq!(vec.len(), 0);
    assert!(vec.capacity() >= 10);
}

This code will generate 3 tests function: _specialized__test_vec__u32_, _specialized__test_vec__u64_, and _specialized__test_vec__char_.

This support adding the attribute #[should_panic] to the definition.

#[test_with(u32, u64, char)]
#[should_panic]
fn test_vec_fail<T>() {
    let vec = Vec::<T>::with_capacity(10);
    assert_eq!(vec.len(), 0);
    assert!(vec.capacity() < 10);
}

Dependencies

~205–640KB
~15K SLoC