add tests and examples

This commit is contained in:
BlackMATov
2020-06-05 00:05:14 +07:00
parent e986d49ed6
commit 1b208c8ef0
4 changed files with 222 additions and 7 deletions

View File

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