Code Monkey home page Code Monkey logo

generic-tests's Introduction

Procedural macro support for generic test definitions

The procedural attribute macro provided by this crate allows the test writer to reuse code between test cases or benchmarks that use the same test protocol with different types under test. As in general programming with Rust, this is achieved by using generic parameters and trait bounds. The specific test cases are expanded in multiple submodules with type arguments provided in another attribute.

Features

  • Instantiates tests and benchmarks for the built-in test framework.
  • Supports arbitrary test function attributes provided by other crates.
  • A customizable set of attributes is copied from the generic test function to its instantiations.
  • Supports async tests.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

generic-tests's People

Contributors

mzabaluev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

getditto

generic-tests's Issues

Compilation failed when test inside of submodule

I trying to replace macro_rules-based tests in my PR with generic-tests one. Everyseen works fine when I define test function in the test module (which is annotated with #[generic_tests::define]), but when I move my test into inner test::submod module, the compilation fails with impossible to find a trait.

I want to define my tests tree, including tests in the inner modules, like I do that for the ordinary tests framework, and then copy that tree for every set of template parameters. Is this possible?

The code that demonstrates problem:
use std::io::BufRead;

//---------------------------------------------------------------
// quick-xml like code
trait BufferedInput<'b, B> {
  fn read(&mut self, buf: B) -> &'b [u8];
}

/// Generic reader, copies data from reader into intermediate buffer
impl<'b, R: BufRead> BufferedInput<'b, &'b mut Vec<u8>> for R {
  /// Read & Copy data from `self` into `buf`,
  /// return data that borrow from `buf`
  fn read(&mut self, buf: &'b mut Vec<u8>) -> &'b [u8] {
    &buf[..]
  }
}

/// Borrowed reader, zero-copy
impl<'i> BufferedInput<'i, ()> for &'i [u8] {
  /// Directly borrow data from `self`, do not copy,
  /// return data that borrow from `self`
  fn read(&mut self, _buf: ()) -> &'i [u8] {
    self
  }
}

//---------------------------------------------------------------
// My tests
#[cfg(test)]
#[generic_tests::define]
mod test {
  /// Helper trait for `generic_tests` crate
  trait Storage<'b>: Default {
    type Buffer;

    /// Returns buffer for `BufferedInput::read`
    fn buffer(&'b mut self) -> Self::Buffer;
  }
  impl<'b> Storage<'b> for () {
    type Buffer = ();
    fn buffer(&'b mut self) {}
  }
  impl<'b> Storage<'b> for Vec<u8> {
    type Buffer = &'b mut Vec<u8>;
    fn buffer(&'b mut self) -> Self::Buffer {
      self
    }
  }

  #[test]
  fn test<S>()
    where for<'b> S: Storage<'b>,
          for<'b> &'b [u8]: crate::BufferedInput<'b, <S as Storage<'b>>::Buffer>,
  {
    let mut storage = S::default();

    storage.buffer();
  }

  mod submod {
    use super::Storage;
    use crate::BufferedInput;

    // This is failed to compile
    #[test]
    fn test<S>()
      where for<'b> S: Storage<'b>,
            for<'b> &'b [u8]: BufferedInput<'b, <S as Storage<'b>>::Buffer>,
    {
      let mut storage = S::default();

      storage.buffer();
    }
  }

  /// Tests for reader that generates events that borrow from the provided buffer
  #[instantiate_tests(<Vec<u8>>)]
  mod buffered {}

  /// Tests for reader that generates events that borrow from the input
  #[instantiate_tests(<()>)]
  mod borrowed {}
}
`cargo expand --tests` output
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use std::io::BufRead;
trait BufferedInput<'b, B> {
    fn read(&mut self, buf: B) -> &'b [u8];
}
/// Generic reader, copies data from reader into intermediate buffer
impl<'b, R: BufRead> BufferedInput<'b, &'b mut Vec<u8>> for R {
    /// Read & Copy data from `self` into `buf`,
    /// return data that borrow from `buf`
    fn read(&mut self, buf: &'b mut Vec<u8>) -> &'b [u8] {
        &buf[..]
    }
}
/// Borrowed reader, zero-copy
impl<'i> BufferedInput<'i, ()> for &'i [u8] {
    /// Directly borrow data from `self`, do not copy,
    /// return data that borrow from `self`
    fn read(&mut self, _buf: ()) -> &'i [u8] {
        self
    }
}
#[cfg(test)]
mod test {
    /// Helper trait for `generic_tests` crate
    trait Storage<'b>: Default {
        type Buffer;
        /// Returns buffer for `BufferedInput::read`
        fn buffer(&'b mut self) -> Self::Buffer;
    }
    impl<'b> Storage<'b> for () {
        type Buffer = ();
        fn buffer(&'b mut self) {}
    }
    impl<'b> Storage<'b> for Vec<u8> {
        type Buffer = &'b mut Vec<u8>;
        fn buffer(&'b mut self) -> Self::Buffer {
            self
        }
    }
    fn test<S>()
    where
        for<'b> S: Storage<'b>,
        for<'b> &'b [u8]: crate::BufferedInput<'b, <S as Storage<'b>>::Buffer>,
    {
        let mut storage = S::default();
        storage.buffer();
    }
    mod submod {
        use super::Storage;
        use crate::BufferedInput;
        extern crate test;
        #[cfg(test)]
        #[rustc_test_marker]
        pub const test: test::TestDescAndFn = test::TestDescAndFn {
            desc: test::TestDesc {
                name: test::StaticTestName("test::submod::test"),
                ignore: false,
                allow_fail: false,
                compile_fail: false,
                no_run: false,
                should_panic: test::ShouldPanic::No,
                test_type: test::TestType::UnitTest,
            },
            testfn: test::StaticTestFn(|| test::assert_test_result(test())),
        };
        fn test<S>()
        where
            for<'b> S: Storage<'b>,
            for<'b> &'b [u8]: BufferedInput<'b, <S as Storage<'b>>::Buffer>,
        {
            let mut storage = S::default();
            storage.buffer();
        }
    }
    /// Tests for reader that generates events that borrow from the provided buffer
    mod buffered {
        #[allow(unused_imports)]
        use super::*;
        extern crate test;
        #[cfg(test)]
        #[rustc_test_marker]
        pub const test: test::TestDescAndFn = test::TestDescAndFn {
            desc: test::TestDesc {
                name: test::StaticTestName("test::buffered::test"),
                ignore: false,
                allow_fail: false,
                compile_fail: false,
                no_run: false,
                should_panic: test::ShouldPanic::No,
                test_type: test::TestType::UnitTest,
            },
            testfn: test::StaticTestFn(|| test::assert_test_result(test())),
        };
        fn test() {
            mod shim {
                pub(super) mod _generic_tests_call_sig {
                    #[allow(unused_imports)]
                    use super::super::super::*;
                    pub(in super::super) struct Args {}
                    pub(super) type Ret = ();
                }
                #[allow(unused_imports)]
                use super::super::*;
                pub(super) unsafe fn shim(
                    _args: _generic_tests_call_sig::Args,
                ) -> _generic_tests_call_sig::Ret {
                    super::super::test::<Vec<u8>>()
                }
            }
            let args = shim::_generic_tests_call_sig::Args {};
            unsafe { shim::shim(args) }
        }
    }
    /// Tests for reader that generates events that borrow from the input
    mod borrowed {
        #[allow(unused_imports)]
        use super::*;
        extern crate test;
        #[cfg(test)]
        #[rustc_test_marker]
        pub const test: test::TestDescAndFn = test::TestDescAndFn {
            desc: test::TestDesc {
                name: test::StaticTestName("test::borrowed::test"),
                ignore: false,
                allow_fail: false,
                compile_fail: false,
                no_run: false,
                should_panic: test::ShouldPanic::No,
                test_type: test::TestType::UnitTest,
            },
            testfn: test::StaticTestFn(|| test::assert_test_result(test())),
        };
        fn test() {
            mod shim {
                pub(super) mod _generic_tests_call_sig {
                    #[allow(unused_imports)]
                    use super::super::super::*;
                    pub(in super::super) struct Args {}
                    pub(super) type Ret = ();
                }
                #[allow(unused_imports)]
                use super::super::*;
                pub(super) unsafe fn shim(
                    _args: _generic_tests_call_sig::Args,
                ) -> _generic_tests_call_sig::Ret {
                    super::super::test::<()>()
                }
            }
            let args = shim::_generic_tests_call_sig::Args {};
            unsafe { shim::shim(args) }
        }
    }
}
#[rustc_main]
pub fn main() -> () {
    extern crate test;
    test::test_main_static(&[&test, &test, &test])
}

Customization of test attributes and copied attributes

The macro should support custom test attributes, like tokio::test.

Here's a draft of the realization:

#[generic_tests::define(attrs(tokio::test, test))]
mod tests {
    #[tokio::test]
    async fn it_works_asynchronously() {
        // ...
    }
}

Attributes other than cfg may need to be copied to the instantiated test cases, so customization of this set should be supported (I think it saner to have whitelist logic with a conservative default rather than copy every attribute by default and blacklist exceptions when/if problems are detected):

#[generic_tests::define(copy_attrs(cfg, my_annotation))]
mod tests {
    #[test]
    #[my_annotation]
    it_works_only_when_annotated() {
        // ...
    }
}

Use `generic-tests` along with `test-case`

Hi there and first of all thanks for this great library.

I want to reduce my test logic by using parameterized tests both on the type and value level. For that I'm already using test-case library, but I can't this to work together with generic-tests:

struct Foo;
struct Bar;

#[cfg(test)]
#[generic_tests::define]
mod tests {
    use super::*;
    use test_case::test_case;

    #[test]
    #[test_case(1 ; "one")]
    #[test_case(2 ; "two")]
    #[test_case(3 ; "three")]
    fn test_blubbi<T>(x: u32) {
        // test logic that uses x: {1,2,3} * T: {Foo, Bar} thus 6 test cases in total
    }

    #[instantiate_tests(<Foo>)]
    mod foo {}

    #[instantiate_tests(<Bar>)]
    mod bar {}
}

But I get the error, that generic test function signatures must not take any arguments

error: functions used as tests can not have any arguments
 --> src/main.rs:9:1
  |
9 | #[generic_tests::define]
  | ^^^^^^^^^^^^^^^^^^^^^^^^
  |

What shall I do?

Support limited use of generic type parameters in function signatures

The aim is to support signatures like this:

#[test]
fn decode_from_bytes<'a, T>>() -> Result<(), <&'a [u8] as TryInto<T>>::Error>
where
    &'a [u8]: TryInto<T>,
    <&'a [u8] as TryInto<T>>::Error: std::error::Error,
{
    let v = b"Hello, world!".try_into()?;
    // ...
}

Substitution of arbitrary type parameters is fraught with breakage due to lack of hygiene, possible inferred lifetimes, lifetimes bound by a type, etc.

Ambiguity between glob imports in nested modules

This test module is currently disabled because of ambiguity between glob imports in the expansion of the instantiation module, which combines imports from both its parent module and the macro invocation root. When a path used in the instantiation arguments is resolved differently in the origin modules, a compiler error occurs which does not follow from the "lay of the code", where the path in the parent module is the one that should be used.

Support two-dimensional generic tests

First of all thanks for this great library :)

We are using it quite often but recently noticed that we would need to parameterize the tests in two dimensions.

It would be great if this

#[instantiate_tests(<<Foo, Bar>, <u8, u16, u32, u64>>)]
mod do_test {}

could generate tests for the following combination of generic parameter for fn my_test<T1, T2>()

Foo - u8
Foo - u16
Foo - u32
Foo - u64
Bar - u8
Bar - u16
Bar - u32
Bar - u64

Alternatively, something like this would also help quite a lot

#[generic_tests::define]
mod generic1 {

    #[generic_tests::define]
    mod generic2 {
        type T1 = u8;//generic_tests::GenericParameter;

        #[test]
        fn my_test<T2>() {
            // ...
        }

        #[instantiate_tests(<u8>)]
        mod do_test_u8 {}
        #[instantiate_tests(<u16>)]
        mod do_test_u16 {}
    }

    #[instantiate_tests(<Foo>)]
    mod do_test_foo {}
    #[instantiate_tests(<Bar>)]
    mod do_test_bar {}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.