Skip to content

Commit

Permalink
Add UT of ScopeGuard (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenBright committed Mar 18, 2024
1 parent 471ac5f commit d4695c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
3 changes: 2 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
39 changes: 39 additions & 0 deletions test/scope_guard_unittest.cc
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#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);
}

0 comments on commit d4695c9

Please sign in to comment.