diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index da59abeb2d..fe9bb42497 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -173,6 +173,7 @@ SET(TEST_BUTIL_SOURCES ${PROJECT_SOURCE_DIR}/test/object_pool_unittest.cpp ${PROJECT_SOURCE_DIR}/test/test_switches.cc ${PROJECT_SOURCE_DIR}/test/scoped_locale.cc + ${PROJECT_SOURCE_DIR}/test/scope_guard_unittest.cc ${PROJECT_SOURCE_DIR}/test/butil_unittest_main.cpp ${PROJECT_SOURCE_DIR}/test/butil_unittest_main.cpp ) diff --git a/test/Makefile b/test/Makefile index 82efc38103..97bde8f534 100644 --- a/test/Makefile +++ b/test/Makefile @@ -145,7 +145,8 @@ TEST_BUTIL_SOURCES = \ scoped_locale.cc \ popen_unittest.cpp \ bounded_queue_unittest.cc \ - butil_unittest_main.cpp + butil_unittest_main.cpp \ + scope_guard_unittest.cc ifeq ($(SYSTEM), Linux) TEST_BUTIL_SOURCES += test_file_util_linux.cc \ diff --git a/test/scope_guard_unittest.cc b/test/scope_guard_unittest.cc new file mode 100644 index 0000000000..085162fdff --- /dev/null +++ b/test/scope_guard_unittest.cc @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + + +#include +#include "butil/memory/scope_guard.h" + +TEST(ScopedGuardTest, sanity) { + bool flag = false; + { + auto guard = butil::MakeScopeGuard([&flag] { + flag = true; + }); + } + ASSERT_TRUE(flag); + + flag = false; + { + auto guard = butil::MakeScopeGuard([&flag] { + flag = true; + }); + guard.dismiss(); + } + ASSERT_FALSE(flag); +}