refactor unit test in case of throw, the fclose will not be called. using unique_ptr with custom destructor will ensure that

This commit is contained in:
Jonathan Dumaresq 2018-12-12 14:46:17 -05:00
parent 91ff96a737
commit a794cfdba3

View file

@ -386,42 +386,37 @@ TEST_CASE("json.org examples")
} }
SECTION("FILE 1.json") SECTION("FILE 1.json")
{ {
auto f = fopen("test/data/json.org/1.json", "r"); std::unique_ptr<FILE, decltype(&fclose)> f(fopen("test/data/json.org/1.json", "r"), &fclose);
json j; json j;
CHECK_NOTHROW(j.parse(f)); CHECK_NOTHROW(j.parse(f.get()));
fclose(f);
} }
SECTION("FILE 2.json") SECTION("FILE 2.json")
{ {
auto f = fopen("test/data/json.org/2.json", "r"); std::unique_ptr<FILE, decltype(&fclose)> f(fopen("test/data/json.org/2.json", "r"), &fclose);
json j; json j;
CHECK_NOTHROW(j.parse(f)); CHECK_NOTHROW(j.parse(f.get()));
fclose(f);
} }
SECTION("FILE 3.json") SECTION("FILE 3.json")
{ {
auto f = fopen("test/data/json.org/3.json", "r"); std::unique_ptr<FILE, decltype(&fclose)> f(fopen("test/data/json.org/3.json", "r"), &fclose);
json j; json j;
CHECK_NOTHROW(j.parse(f)); CHECK_NOTHROW(j.parse(f.get()));
fclose(f);
} }
SECTION("FILE 4.json") SECTION("FILE 4.json")
{ {
auto f = fopen("test/data/json.org/4.json", "r"); std::unique_ptr<FILE, decltype(&fclose)> f(fopen("test/data/json.org/4.json", "r"), &fclose);
json j; json j;
CHECK_NOTHROW(j.parse(f)); CHECK_NOTHROW(j.parse(f.get()));
fclose(f);
} }
SECTION("FILE 5.json") SECTION("FILE 5.json")
{ {
auto f = fopen("test/data/json.org/5.json", "r"); std::unique_ptr<FILE, decltype(&fclose)> f(fopen("test/data/json.org/5.json", "r"), &fclose);
json j; json j;
CHECK_NOTHROW(j.parse(f)); CHECK_NOTHROW(j.parse(f.get()));
fclose(f);
} }
} }