From a794cfdba3f52d80684fd526a0def4484e61b476 Mon Sep 17 00:00:00 2001 From: Jonathan Dumaresq Date: Wed, 12 Dec 2018 14:46:17 -0500 Subject: [PATCH] refactor unit test in case of throw, the fclose will not be called. using unique_ptr with custom destructor will ensure that --- test/src/unit-testsuites.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/test/src/unit-testsuites.cpp b/test/src/unit-testsuites.cpp index 782c3f525..fe3e01750 100644 --- a/test/src/unit-testsuites.cpp +++ b/test/src/unit-testsuites.cpp @@ -386,42 +386,37 @@ TEST_CASE("json.org examples") } SECTION("FILE 1.json") { - auto f = fopen("test/data/json.org/1.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/1.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 2.json") { - auto f = fopen("test/data/json.org/2.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/2.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 3.json") { - auto f = fopen("test/data/json.org/3.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/3.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 4.json") { - auto f = fopen("test/data/json.org/4.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/4.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } SECTION("FILE 5.json") { - auto f = fopen("test/data/json.org/5.json", "r"); + std::unique_ptr f(fopen("test/data/json.org/5.json", "r"), &fclose); json j; - CHECK_NOTHROW(j.parse(f)); - fclose(f); + CHECK_NOTHROW(j.parse(f.get())); } }