summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2019-04-26 13:29:28 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2019-04-26 15:33:55 +0200
commit6ec6d013227e66156e00c1d5ac9d0d2ddd208fae (patch)
treeafd26ec9630d8331f1fa5804a36004d21255ac5f /comphelper
parentfb2f2e66424ff5d9d0750b98b9fa05ac3ca1ec34 (diff)
Add a unit test for comphelper's guards
Change-Id: Ia9a9c5694d3982a87b720071b74220d572ef1a78 Reviewed-on: https://gerrit.libreoffice.org/71355 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/CppunitTest_comphelper_test.mk1
-rw-r--r--comphelper/qa/unit/test_guards.cxx57
2 files changed, 58 insertions, 0 deletions
diff --git a/comphelper/CppunitTest_comphelper_test.mk b/comphelper/CppunitTest_comphelper_test.mk
index 20503e65bd06..8bb335aa24dc 100644
--- a/comphelper/CppunitTest_comphelper_test.mk
+++ b/comphelper/CppunitTest_comphelper_test.mk
@@ -15,6 +15,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,comphelper_test, \
comphelper/qa/unit/test_hash \
comphelper/qa/unit/base64_test \
comphelper/qa/unit/types_test \
+ comphelper/qa/unit/test_guards \
))
$(eval $(call gb_CppunitTest_use_sdk_api,comphelper_test))
diff --git a/comphelper/qa/unit/test_guards.cxx b/comphelper/qa/unit/test_guards.cxx
new file mode 100644
index 000000000000..39d8f80e0c68
--- /dev/null
+++ b/comphelper/qa/unit/test_guards.cxx
@@ -0,0 +1,57 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/types.h>
+#include <comphelper/flagguard.hxx>
+#include <unotest/bootstrapfixturebase.hxx>
+
+CPPUNIT_TEST_FIXTURE(CppUnit::TestFixture, test_comphelperGuards)
+{
+ bool bFlag = true;
+ {
+ // Test that comphelper::ScopeGuard executes its parameter on destruction
+ comphelper::ScopeGuard aGuard([&bFlag] { bFlag = false; });
+ CPPUNIT_ASSERT(bFlag);
+ }
+ CPPUNIT_ASSERT(!bFlag);
+
+ {
+ // Test that comphelper::FlagGuard properly sets and resets the flag
+ comphelper::FlagGuard aGuard(bFlag);
+ CPPUNIT_ASSERT(bFlag);
+ }
+ CPPUNIT_ASSERT(!bFlag);
+
+ bFlag = true;
+ {
+ // Test that comphelper::FlagGuard properly sets and resets the flag
+ comphelper::FlagGuard aGuard(bFlag);
+ CPPUNIT_ASSERT(bFlag);
+ }
+ // comphelper::FlagGuard must reset flag to false on destruction unconditionally
+ CPPUNIT_ASSERT(!bFlag);
+
+ {
+ // Test that comphelper::FlagRestorationGuard properly sets and resets the flag
+ comphelper::FlagRestorationGuard aGuard(bFlag, true);
+ CPPUNIT_ASSERT(bFlag);
+ }
+ CPPUNIT_ASSERT(!bFlag);
+
+ bFlag = true;
+ {
+ // Test that comphelper::FlagRestorationGuard properly sets and resets the flag
+ comphelper::FlagRestorationGuard aGuard(bFlag, false);
+ CPPUNIT_ASSERT(!bFlag);
+ }
+ // comphelper::FlagGuard must reset flag to initial state on destruction
+ CPPUNIT_ASSERT(bFlag);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */