Hi there!
I'm completely new to rust and I am still going through the book. Will probably use it for a rewrite of one of my pet projects, which was previously in C++.
I've grown fond of a particular C++ test framework called Catch: GitHub - catchorg/Catch2: A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and C++03 on the Catch1.x branch)
The nice thing about it, is that you can recursively layer tests on the same objects (being reset, since they're passed by-value into each test) so you get some very clean-looking and intuitive tests which read a lot like specs, instead of having a lot of completely separate test methods with all the noise of setting up that usually follows.
For instance (taken from their Getting Started Page):
TEST_CASE( "vectors can be sized and resized", "[vector]" ) {
std::vector<int> v( 5 );
REQUIRE( v.size() == 5 );
REQUIRE( v.capacity() >= 5 );
SECTION( "resizing bigger changes size and capacity" ) {
v.resize( 10 );
REQUIRE( v.size() == 10 );
REQUIRE( v.capacity() >= 10 );
}
SECTION( "resizing smaller changes size but not capacity" ) {
v.resize( 0 );
REQUIRE( v.size() == 0 );
REQUIRE( v.capacity() >= 5 );
}
SECTION( "reserving bigger changes capacity but not size" ) {
v.reserve( 10 );
REQUIRE( v.size() == 5 );
REQUIRE( v.capacity() >= 10 );
}
SECTION( "reserving smaller does not change size or capacity" ) {
v.reserve( 0 );
REQUIRE( v.size() == 5 );
REQUIRE( v.capacity() >= 5 );
}
}
Nice and clean. Sections can be inserted recursively to cover continously more specialised edge-cases, without them effecting the copies of the data passed to sections further down the line.
Do you know if anything like this exists for Rust?
If not, I might have a go at making it myself as an exercise. But would there be problems from the ownership model in Rust, since memory is being copied (and mutated) a lot here?
Can you hook new test home-built test frameworks into the cargo test suite?
Thank you a bunch in advance!
Best Regards
Dan True