mirror of
https://github.com/BlackMATov/defer.hpp.git
synced 2025-12-16 14:11:13 +07:00
add tests and examples
This commit is contained in:
@@ -7,11 +7,53 @@
|
||||
#define CATCH_CONFIG_FAST_COMPILE
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <defer.hpp/defer.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
}
|
||||
|
||||
TEST_CASE("examples") {
|
||||
SECTION("basic_defer") {
|
||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
||||
// defer will close the file after scope or on exception
|
||||
DEFER([file]{ ::fclose(file); });
|
||||
|
||||
const char buffer[] = "hello world\n";
|
||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
||||
throw std::runtime_error("some exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("error_defer") {
|
||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
||||
// defer will close the file after scope or on exception
|
||||
DEFER([file]{ ::fclose(file); });
|
||||
|
||||
// error defer will be called on exception
|
||||
ERROR_DEFER([]{
|
||||
std::cerr << "there is something wrong" << std::endl;
|
||||
});
|
||||
|
||||
const char buffer[] = "hello world\n";
|
||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
||||
throw std::runtime_error("some exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("return_defer") {
|
||||
if ( FILE *file = ::fopen("output.txt", "a") ) {
|
||||
// defer will close the file after scope or on exception
|
||||
DEFER([file]{ ::fclose(file); });
|
||||
|
||||
// return defer will be called on successful scope exit
|
||||
RETURN_DEFER([]{
|
||||
std::cout << "all is ok!" << std::endl;
|
||||
});
|
||||
|
||||
const char buffer[] = "hello world\n";
|
||||
if ( 12 != ::fwrite(buffer, sizeof(buffer[0]), ::strlen(buffer), file) ) {
|
||||
throw std::runtime_error("some exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,127 @@ namespace
|
||||
}
|
||||
|
||||
TEST_CASE("defer") {
|
||||
SECTION("simple") {
|
||||
int i = 0;
|
||||
{
|
||||
DEFER([&i]{ ++i; });
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
}
|
||||
|
||||
SECTION("simple_with_arg") {
|
||||
int i = 0;
|
||||
{
|
||||
DEFER([](int& i){ ++i; }, std::ref(i));
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
}
|
||||
|
||||
SECTION("simple_with_args") {
|
||||
int i = 0, j = 0;
|
||||
{
|
||||
DEFER([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
|
||||
REQUIRE(i == 0);
|
||||
REQUIRE(j == 0);
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
REQUIRE(j == 2);
|
||||
}
|
||||
|
||||
SECTION("simple_with_exception") {
|
||||
int i = 0;
|
||||
try {
|
||||
DEFER([&i]{ ++i; });
|
||||
REQUIRE(i == 0);
|
||||
throw std::exception();
|
||||
} catch (...) {
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("error_defer") {
|
||||
SECTION("simple") {
|
||||
int i = 0;
|
||||
{
|
||||
ERROR_DEFER([&i]{ ++i; });
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
|
||||
SECTION("simple_with_arg") {
|
||||
int i = 0;
|
||||
{
|
||||
ERROR_DEFER([](int& i){ ++i; }, std::ref(i));
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
|
||||
SECTION("simple_with_args") {
|
||||
int i = 0, j = 0;
|
||||
{
|
||||
ERROR_DEFER([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
|
||||
REQUIRE(i == 0);
|
||||
REQUIRE(j == 0);
|
||||
}
|
||||
REQUIRE(i == 0);
|
||||
REQUIRE(j == 0);
|
||||
}
|
||||
|
||||
SECTION("simple_with_exception") {
|
||||
int i = 0;
|
||||
try {
|
||||
ERROR_DEFER([&i]{ ++i; });
|
||||
REQUIRE(i == 0);
|
||||
throw std::exception();
|
||||
} catch (...) {
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("return_defer") {
|
||||
SECTION("simple") {
|
||||
int i = 0;
|
||||
{
|
||||
RETURN_DEFER([&i]{ ++i; });
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
}
|
||||
|
||||
SECTION("simple_with_arg") {
|
||||
int i = 0;
|
||||
{
|
||||
RETURN_DEFER([](int& i){ ++i; }, std::ref(i));
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
}
|
||||
|
||||
SECTION("simple_with_args") {
|
||||
int i = 0, j = 0;
|
||||
{
|
||||
RETURN_DEFER([](int& i, int& j){ ++i; j += 2; }, std::ref(i), std::ref(j));
|
||||
REQUIRE(i == 0);
|
||||
REQUIRE(j == 0);
|
||||
}
|
||||
REQUIRE(i == 1);
|
||||
REQUIRE(j == 2);
|
||||
}
|
||||
|
||||
SECTION("simple_with_exception") {
|
||||
int i = 0;
|
||||
try {
|
||||
RETURN_DEFER([&i]{ ++i; });
|
||||
REQUIRE(i == 0);
|
||||
throw std::exception();
|
||||
} catch (...) {
|
||||
}
|
||||
REQUIRE(i == 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user